Results 1 to 8 of 8

Thread: [RESOLVED] Sharing Variables

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    Resolved [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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Sharing Variables

    e.g.
    VB Code:
    1. Private m_MyVar as String
    2.  
    3. Public Property MyVar() As String
    4.     Get
    5.         Return Me.m_MyVar
    6.     End Get
    7.     Set(ByVal Value As String)
    8.         Me.m_MyVar = Value
    9.     End Set
    10. End Property
    It's not really necessary in a lot of cases but can be a better option in others.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    Re: Sharing Variables

    OK, and stupid question...

    How do I read a property? As I've never really used one before...

    Cheers.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim myDialogue As New Form2
    2.  
    3. If myDialogue.ShowDialog() = DialogResult.OK Then
    4.     'The user clicked the OK button.
    5.     MessageBox.Show(myDialogue.MyVar)
    6. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    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
  •  



Click Here to Expand Forum to Full Width