Accessing a property from another class

R

RSH

I havent been able to set a property from another class with out getting
some sort of error.

Can someone please tell me what I'm doing wrong here?





Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

Dim clsInline As New myTestClass

clsInline.fTest()

'Add any initialization after the InitializeComponent() call

End Sub

Public Property CustomerName() As String

Get

Return Label1.Text

End Get

Set(ByVal Value As String)

Label1.Text = Value

System.Windows.Forms.Application.DoEvents()

End Set

End Property





'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer





'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents Label1 As System.Windows.Forms.Label

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.Label1 = New System.Windows.Forms.Label

Me.SuspendLayout()

'

'Label1

'

Me.Label1.Location = New System.Drawing.Point(24, 32)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(232, 23)

Me.Label1.TabIndex = 0

Me.Label1.Text = "Label1"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 266)

Me.Controls.Add(Me.Label1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

End Class



Public Class myTestClass

Sub fTest()

Dim f1 As New Form1

f1.CustomerName = ("Ron")

System.Windows.Forms.Application.DoEvents()

End Sub

End Class
 
C

Chris

RSH said:
I havent been able to set a property from another class with out getting
some sort of error.

Can someone please tell me what I'm doing wrong here?





Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

Dim clsInline As New myTestClass

clsInline.fTest()

'Add any initialization after the InitializeComponent() call

End Sub

Public Property CustomerName() As String

Get

Return Label1.Text

End Get

Set(ByVal Value As String)

Label1.Text = Value

System.Windows.Forms.Application.DoEvents()

End Set

End Property





'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer





'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents Label1 As System.Windows.Forms.Label

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.Label1 = New System.Windows.Forms.Label

Me.SuspendLayout()

'

'Label1

'

Me.Label1.Location = New System.Drawing.Point(24, 32)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(232, 23)

Me.Label1.TabIndex = 0

Me.Label1.Text = "Label1"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 266)

Me.Controls.Add(Me.Label1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

End Class



Public Class myTestClass

Sub fTest()

Dim f1 As New Form1

f1.CustomerName = ("Ron")

System.Windows.Forms.Application.DoEvents()

End Sub

End Class

f1.CustomerName = ("Ron")
This is not a string. Try removing the ()
f1.CustomerName = "Ron"
 
R

RSH

Okay I have found out that the problem is not the property but instead the
value isn't being displayed in the textbox. i rewrote the code and the
messagebox displays with the value as it should but nothing is displayed in
the textbox...I have to be doing something very dumb here!

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

Public Overridable Property CustomerName() As String

Get

Return Label1.Text

End Get

Set(ByVal Value As String)

Label1.Text = Value

MessageBox.Show(Value)

System.Windows.Forms.Application.DoEvents()

End Set

End Property





'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer





'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents Button1 As System.Windows.Forms.Button

Friend WithEvents Label1 As System.Windows.Forms.Label

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.Label1 = New System.Windows.Forms.Label

Me.Button1 = New System.Windows.Forms.Button

Me.SuspendLayout()

'

'Label1

'

Me.Label1.Location = New System.Drawing.Point(24, 32)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(232, 23)

Me.Label1.TabIndex = 0

'

'Button1

'

Me.Button1.Location = New System.Drawing.Point(80, 136)

Me.Button1.Name = "Button1"

Me.Button1.TabIndex = 1

Me.Button1.Text = "Button1"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 266)

Me.Controls.Add(Me.Button1)

Me.Controls.Add(Me.Label1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim clsInline As New myTestClass

clsInline.fTest("Thomas Dolby")

End Sub





End Class



Public Class myTestClass

Public Shared Sub fTest(ByVal strTemp)

Dim f1 As New Form1

f1.CustomerName = strTemp

End Sub

End Class
 
C

Chris

RSH said:
Okay I have found out that the problem is not the property but instead the
value isn't being displayed in the textbox. i rewrote the code and the
messagebox displays with the value as it should but nothing is displayed in
the textbox...I have to be doing something very dumb here!

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

Public Overridable Property CustomerName() As String

Get

Return Label1.Text

End Get

Set(ByVal Value As String)

Label1.Text = Value

MessageBox.Show(Value)

System.Windows.Forms.Application.DoEvents()

End Set

End Property





'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer





'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents Button1 As System.Windows.Forms.Button

Friend WithEvents Label1 As System.Windows.Forms.Label

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.Label1 = New System.Windows.Forms.Label

Me.Button1 = New System.Windows.Forms.Button

Me.SuspendLayout()

'

'Label1

'

Me.Label1.Location = New System.Drawing.Point(24, 32)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(232, 23)

Me.Label1.TabIndex = 0

'

'Button1

'

Me.Button1.Location = New System.Drawing.Point(80, 136)

Me.Button1.Name = "Button1"

Me.Button1.TabIndex = 1

Me.Button1.Text = "Button1"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 266)

Me.Controls.Add(Me.Button1)

Me.Controls.Add(Me.Label1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim clsInline As New myTestClass

clsInline.fTest("Thomas Dolby")

End Sub





End Class



Public Class myTestClass

Public Shared Sub fTest(ByVal strTemp)

Dim f1 As New Form1

f1.CustomerName = strTemp

End Sub

End Class


I think I understand what you are doing now. My assumption is that you
are starting your application as Form1. If I'm right then what your
problem is that this line:

Dim f1 As New Form1

Create a new instatnce of Form1, it does not access the one you are
currently seeing. Try something like:

Public Class myTestClass
Sub fTest(Main as Form1)
Main.CustomerName = ("Ron")
End Sub
End Class

Chris
 
R

RSH

Chris,

This is great! I have one problem though...you are passing reference to the
main form which when i pass "me" it works great. But the problem is the
class I'm using is part of an interface and so i cant pass parameters to
it...so when i try to pass the form to it i get a new error. Here is the
actual class:




Public Class PackageEventsSink

Inherits System.Windows.Forms.Form

Implements DTS.PackageEvents

Public fParentForm As DTSConvertor725

Overridable Overloads Sub OnError(ByVal EventSource As String, _

ByVal ErrorCode As Integer, ByVal Source As String, _

ByVal Description As String, ByVal HelpFile As String, _

ByVal HelpContext As Integer, ByVal IDofInterfaceWithError As String, _

ByRef pbCancel As Boolean) Implements DTS.PackageEvents.OnError

fParentForm.strErrorLog += ("Event: " & EventSource & vbCrLf & "Error Code:
" & ErrorCode & vbCrLf & "Source: " & Source & vbCrLf & "Description: " &
Description & vbCrLf & vbCrLf)

End Sub

Overridable Overloads Sub OnFinish(ByVal EventSource As String) _

Implements DTS.PackageEvents.OnFinish

End Sub

Overridable Overloads Sub OnProgress(ByVal EventSource As String, _

ByVal ProgressDescription As String, ByVal PercentComplete As Integer, _

ByVal ProgressCountLow As Integer, ByVal ProgressCountHigh As Integer) _

Implements DTS.PackageEvents.OnProgress

fParentForm.StatusArea.Text = "Step " & fParentForm.StepCount & " " &
EventSource & " " & ProgressDescription

fParentForm.ProgressBar2.Value = fParentForm.StepCount

System.Windows.Forms.Application.DoEvents()

End Sub

Overridable Overloads Sub OnQueryCancel(ByVal EventSource As String, ByRef
pbCancel As Boolean) Implements DTS.PackageEvents.OnQueryCancel

End Sub

Overridable Overloads Sub OnStart(ByVal EventSource As String) _

Implements DTS.PackageEvents.OnStart

fParentForm.StepCount = fParentForm.StepCount + 1

fParentForm.StatusArea.Text = "Step " & fParentForm.StepCount & " " &
EventSource

System.Windows.Forms.Application.DoEvents()

End Sub

End Class





I have a form that is displaying and it is called DTSConvertor725. Right
now I have the progressbar2 and statusarea richtextbox declared on the main
form as Public Shared. This works but everytime I shut down Visual Studio
it resets the declarations to friend withevents...so obviosly it doesnt like
my attempt to change the declaration. But it runs with no errors and the
main form is updated as expected.

Thanks,

Ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top