|
-
Jul 28th, 2005, 03:49 AM
#1
Thread Starter
Lively Member
[RESOLVED] Sharing Variables
How can I share some information from Form2 with Form1?
Would I make my variable like this?:
Public myVar As String
Or how is the best way of doing this?
-
Jul 28th, 2005, 03:52 AM
#2
Re: Sharing Variables
If you are displaying the second form from the first using ShowDialog then that is the easiest, and probably best way to do it, although you may want to think about a private variable and a public property.
-
Jul 28th, 2005, 03:55 AM
#3
Re: Sharing Variables
e.g.
VB Code:
Private m_MyVar as String
Public Property MyVar() As String
Get
Return Me.m_MyVar
End Get
Set(ByVal Value As String)
Me.m_MyVar = Value
End Set
End Property
It's not really necessary in a lot of cases but can be a better option in others.
-
Jul 28th, 2005, 04:01 AM
#4
Thread Starter
Lively Member
Re: Sharing Variables
So do I make a private variable on Form2, and add the property to Form1 to grab the information, or does both go onto Form2 and I merely call m_MyVar to get the value from Form2 into Form1?
Cheers.
-
Jul 28th, 2005, 04:08 AM
#5
Re: Sharing Variables
If you do want to use the property method they both go in Form2. Form1 then simply reads the value of the property from the Form2 variable.
Reading a property does take longer than reading a variable, so if you're concerned with getting the best performance then you may want to use the straight variable. Otherwise I'd suggest using properties always. Notice that there is almost no class in the .NET Framework that exposes straight variables, called Fields in the help. It is usually only for the odd Shared constant. Everything else is exposed through properties.
Edit:
The performance difference I speak of is miniscule, though. In fact I think it's actually nanoscule, or perhaps even picoscule.
-
Jul 28th, 2005, 04:10 AM
#6
Thread Starter
Lively Member
Re: Sharing Variables
OK, and stupid question...
How do I read a property? As I've never really used one before...
Cheers.
-
Jul 28th, 2005, 04:16 AM
#7
Re: Sharing Variables
You use them all the time actually. Everthing you see in the Properties window in the designer is a property. If you were to use the code snippet I provided above, you could do something like this in Form1:
VB Code:
Dim myDialogue As New Form2
If myDialogue.ShowDialog() = DialogResult.OK Then
'The user clicked the OK button.
MessageBox.Show(myDialogue.MyVar)
End If
-
Jul 28th, 2005, 04:18 AM
#8
Thread Starter
Lively Member
Re: Sharing Variables
Doh!
OK, thanks man.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|