Results 1 to 8 of 8

Thread: [RESOLVED] Passing variables - Best Practice

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Resolved [RESOLVED] Passing variables - Best Practice

    This is actually a multi question. It goes for both VB.NET (winforms) and ASP.NET (webforms). I've seen many posts of how to pass variables between forms. This may be more of an opinion question, but I'm just curious... which is the best practice for passing variables between .net winforms and which is the best practice for passing variables between .net webforms?

    Thanks,

    Strick

  2. #2
    Fanatic Member
    Join Date
    Aug 2006
    Location
    Chicago, IL
    Posts
    514

    Re: Passing variables - Best Practice

    I tend to pass variables inside of a class I create to hold all of that data. So in essence I have fields in a class that stores that data.
    Warren Ayen
    Senior C# Developer
    DLS Software Studios (http://www.dlssoftwarestudios.com/)

    I use Microsoft Visual Studio 2005, 2008, working with Visual Basic and Visual C#
    Hey! If you like my post, or I solve your issue, please Rate Me!

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Passing variables - Best Practice

    For WinForms, I usually use public properties to read/write the information you want to get/set...

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Re: Passing variables - Best Practice

    Thanks Gigemboy

    Would you happen to have a sample usage of public property?

    Thanks,

    Strick

  5. #5
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Passing variables - Best Practice

    Well the idea is you only expose the variables or objects you need, keeping everything else internal. Suppose You want to pass an integer to another form, so in the other form you have a Private variable to hold the integer, and expose this private variable through a public property to set/get the value as you need it... something like below...
    VB Code:
    1. 'class level, to hold the integer
    2.     Private _SomeNumber As Integer = 0
    3.    
    4.     'property to get/set the value in the form
    5.     Public Property SomeNumber() As Integer
    6.         Get
    7.             Return _SomeNumber
    8.         End Get
    9.         Set(ByVal value As Integer)
    10.             _SomeNumber = value
    11.         End Set
    12.     End Property
    When you have a reference to the form, you should see a MyNumber property in the IDE, which exposes the private variable holding the actual value. To get/set the value in the form, you just refer to that property via the reference to the form that contains it...
    VB Code:
    1. 'Assuming a reference called "MyForm", which has the above mentioned property
    2.  
    3. MyForm.SomeNumber = 100 'sets the value to 100
    4.  
    5. Dim SomeNumber As Integer = MyForm.SomeNumber 'gets the value, assigns it to an integer in the current form
    You also have the ReadOnly (to just get), and WriteOnly (to just set) kewords (as well as others) that you can add to the property depending on your needs...

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Re: Passing variables - Best Practice

    Thanks for your quick response! I'm gonna try this out. Now should I put this property in a class by itself or can I put it in one of my existing classes?

    Strick

  7. #7
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Passing variables - Best Practice

    The property will be available for whatever class you want it in...

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Re: [RESOLVED] Passing variables - Best Practice

    Thanks for your help, Gig. I put the property in the second form's class. Form 1 loads everything it needs and "sets" the values that form 2 needs. It then opens form 2. Form 2 now has the variables from form 1.

    This worked great. Marked resolved.

    Strick

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