Class Diagram - How to use Associations & Properties correctly
Hi,
I realize I am a Class Diagram newbie, so i am hoping someone can help me with what is probably a simple problem.
I have a very simple VB project with one form and one class. The class contains a few properties and methods that I want to use in the form.
In the Class Diagram, I create an association between the two item by drawing an association line from the form (Form1) to the class (clsMain). This creates the following code in the form's declaration's section:
PublicProperty clsMain()As clsMain
GetEndGetSet(ByVal valueAs clsMain)EndSetEndPropertyI can then call the properties and methods in the class like so:
clsMain.CurrentValue =
CInt(Me.txtInput.Text)MsgBox(clsMain.DoMath)
...but I keep getting a null exception error. I realize that this is because there is nothing contained in the Get or Set areas of the clsMain declaration, but I am not sure what to put there. I could have multiple properities & events in the class, so do I need to add each one to the Get & Set areas?
Again, I realize that I am more than likely missing something "stupid" - if this has been discussed to death elsewhere, please point me in the right direction.
Thanx in advance!
[2323 byte] By [
Loki70] at [2007-12-22]
Generally you would do something like this:
Private _myPrivateVar as clsMain
Public Property clsMain() As clsMain
Get
Return _myPrivateVar
End GetSet(ByVal value As clsMain)
_myPrivateVar = value
End Set
End Propertyone quick trick within studio to generate this structure is to type in the word property and press tab. Very cool.
Hi Loki,
Drawing an association line has created a property called 'clsMain' of type clsMain. However, the problem is that this property isn't yet associated with any instantiated fields in your form class, so when the property is accessed (e.g. clsMain.CurrentValue), the application returns null.
Assuming that CurrentValue is a public field or property of the clsMain class that you want to set, you need to do the following additional things:
1) Declare a private field of type clsMain.
Private m_clsMain as clsMain
2) Write code that instantiates the field at an appropriate place. This might be when the form loads, for example:
m_clsMain = New clsMain()
3) Write code for the Form1.clsMain property to allow access to the private field, m_clsMain. In the code below I've modified the property declaration to make it a readonly property (e.g. no Set), but you can decide what is best for your situation:
Public ReadOnly Property clsMain() as clsMain
Get
Return m_clsMain
End Get
End Property
Let me know if this helps or if you have any additional questions.
Regards,
John Stallo