|
-
Jan 17th, 2007, 04:27 PM
#1
Thread Starter
Fanatic Member
[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
-
Jan 17th, 2007, 05:12 PM
#2
Fanatic Member
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!
-
Jan 17th, 2007, 06:10 PM
#3
Re: Passing variables - Best Practice
For WinForms, I usually use public properties to read/write the information you want to get/set...
-
Jan 17th, 2007, 07:22 PM
#4
Thread Starter
Fanatic Member
Re: Passing variables - Best Practice
Thanks Gigemboy
Would you happen to have a sample usage of public property?
Thanks,
Strick
-
Jan 17th, 2007, 07:37 PM
#5
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:
'class level, to hold the integer
Private _SomeNumber As Integer = 0
'property to get/set the value in the form
Public Property SomeNumber() As Integer
Get
Return _SomeNumber
End Get
Set(ByVal value As Integer)
_SomeNumber = value
End Set
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:
'Assuming a reference called "MyForm", which has the above mentioned property
MyForm.SomeNumber = 100 'sets the value to 100
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...
-
Jan 17th, 2007, 07:59 PM
#6
Thread Starter
Fanatic Member
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
-
Jan 17th, 2007, 10:15 PM
#7
Re: Passing variables - Best Practice
The property will be available for whatever class you want it in...
-
Jan 18th, 2007, 09:47 AM
#8
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|