|
-
Feb 9th, 2010, 08:34 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Property.........Get............Set
hi,
i want to pass data from one form to another.....so i want to use the property...get....set
but i dont know how to use it
what should be in the get block and what should be in the set block?
please somebody explain in simple words(i am a newbie in vb.net)
thank you
have a nice day
-
Feb 9th, 2010, 08:49 AM
#2
Re: Property.........Get............Set
Here's a quick and dirty example, if you need more explanation/detail I'd suggest doing a google search, there are thousands of examples available.
Code:
Class YourForm
Private SomeInternalVar As SomeType
Public Property SomeName As SameType
Get
Return SomeInternalVar
End Get
Set (ByVal value As SameType)
SomeInternalVar = value
End Set
End Property
End Class
-
Feb 9th, 2010, 09:05 AM
#3
Thread Starter
Fanatic Member
Re: Property.........Get............Set
can you explain what is the need of this line:
Code:
Private SomeInternalVar As SomeType
-
Feb 9th, 2010, 09:14 AM
#4
Re: Property.........Get............Set
That's the variable that actually holds the data, the property is a pass-through to the variable. For example:
Code:
Private _NumberOfPeople As Integer
Public Property NumberOfPeople As Integer
Get
Return _NumberOfPeople
End Get
Set (ByVal value As Integer)
If value >= 0 Then
'Accept the new value
_NumberOfPeople = value
Else
'Can't have less than 0 people
Throw New ArguementOutOfRange("The number of people cannot be less than 0.")
End If
End Set
End Property
Last edited by JuggaloBrotha; Feb 9th, 2010 at 09:17 AM.
-
Feb 9th, 2010, 09:24 AM
#5
Lively Member
Re: Property.........Get............Set
SomeInternalVar stores the data.
Property Get/Set allows you to access the data, but it does not allocate memory to store the data, which is what you do when you declare a variable using either private or public.
You could also declare the variable as public and not deal with the properties.
An example would be with a string variable:
Code:
Private _MyStringData As String
Public Property MyStringData As String
Get
Return _MyStringData
End Get
Set (Byval value As String)
_MyStringData = value
End Set
End Property
Is the same as
Code:
Public MyStringData As String
A property is helpful if you want some sort of internal formatting to occur when getting or setting the data, or if you want to make the data ReadOnly for example.
Code:
Private _LastValueReturned As String
Public Readonly Property LastValueReturned As String
Get
Return _LastValueReturned
End Get
End Property
The _LastValueReturned would be set by another method somewhere else in the class. By its nature, you wouldn't want to allow the user to change this value themselves, but they may want to have access to the last data returned by another method, for example.
These are simply abstract ideas to get you started. It is common to use an _ to designate the "Data Store" for the property, while the property itself is the same as the variable name, without the _.
-
Feb 9th, 2010, 09:59 AM
#6
Thread Starter
Fanatic Member
Re: Property.........Get............Set
ok i am trying out with an example....this will be a bit more clear to me:
I have 4 textbox named txtName,txtAge,txtRoll,txtSex in form1 where i will enter some value
And i have 4 textbox in form 2 where i will get the data that i entered in the 4 textbox of form1 when the form2 loads....
how to do this using the property....get...set?
please help
thank you
-
Feb 9th, 2010, 10:05 AM
#7
Re: Property.........Get............Set
Each Textbox's Text property can be used in the properties get & set, the Data Type is String.
-
Feb 9th, 2010, 10:37 AM
#8
Thread Starter
Fanatic Member
Re: Property.........Get............Set
this is what i did in the form2:
Code:
Public Property gatValue() As String
Get
Return txtName.Text
Return txtAge.Text
Return txtRoll.Text
Return txtSex.Text
End Get
Set(ByVal value As String)
End Set
End Property
but i am confused with the form1 code where i need to set the values at the button click event......please help
-
Feb 9th, 2010, 10:42 AM
#9
Re: Property.........Get............Set
You can only return 1 value from a property Getter. This means that code you posted stops executing after the first Return statement (it will only return txtName.Text) If you need to return multiple values, you need to create multiple properties, or you need to create a structure.
However in reality, while I do encourage the use of properties where needed, all controls you add to a form are marked by the language as "friend" anyway. What this means is you can access txtName on one form from another.
If I had Form1 and Form2, and Form2 had some textboxes on it, I could just do this in Form1
Form2.txtName.Text = "My Name Here"
Messagebox.Show(Form2.txtName.Text)
Since the controls on forms are marked as friend, they are visible to any other code in the current project. The only time you REALLY need to use properties instead, is if you need validation logic on the value before setting or getting it.
-
Feb 9th, 2010, 10:49 AM
#10
Thread Starter
Fanatic Member
Re: Property.........Get............Set
but even this code is not working for me 
form1:
Code:
Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
Form2.Show()
Form2.getValue = txtName.Text
End Sub
form2:
Code:
Public Class Form2
Public Property getValue() As String
Get
Return txtName.Text
End Get
Set(ByVal value As String)
End Set
End Property
End Class
both form1 and form2 contains a textbox named txtname and form1 contains a button which on clicking i want to pass the text entered in txtname of form1 to the txtname of the form2.....thats all what i want to do
please help
-
Feb 9th, 2010, 10:51 AM
#11
Re: Property.........Get............Set
well look at the "set" portion of your property. You do nothing in it. When it is called, it simply does nothing at all.
Try this:
Code:
Public Class Form2
Public Property getValue() As String
Get
Return txtName.Text
End Get
Set(ByVal value As String)
txtName.Text = value
End Set
End Property
End Class
-
Feb 9th, 2010, 10:52 AM
#12
Re: Property.........Get............Set
also I would not call your property "getValue" as that makes no sense for a property that is both a get and set property and doesn't explain what property is being set. You should rename it to something to represent what it is you are getting/setting. If this txtName is for lets say a customer name I would call this property CustomerName.
-
Feb 9th, 2010, 10:58 AM
#13
Thread Starter
Fanatic Member
Re: Property.........Get............Set
so it means after executing this line of form1:
Code:
Form2.getValue = txtName.Text
the control moves to the set portion of the property:
Code:
Set(ByVal value As String)
txtName.Text = value
End Set
and then finally executes the get portion:
Code:
Get
Return txtName.Text
End Get
am i getting it right?
-
Feb 9th, 2010, 10:59 AM
#14
Thread Starter
Fanatic Member
Re: Property.........Get............Set
-
Feb 9th, 2010, 11:00 AM
#15
Thread Starter
Fanatic Member
Re: Property.........Get............Set
 Originally Posted by kleinma
also I would not call your property "getValue" as that makes no sense for a property that is both a get and set property and doesn't explain what property is being set. You should rename it to something to represent what it is you are getting/setting. If this txtName is for lets say a customer name I would call this property CustomerName.
then how would you do this?
-
Feb 9th, 2010, 11:02 AM
#16
Thread Starter
Fanatic Member
Re: Property.........Get............Set
even if i comment out this portion of form2:
Code:
Get
' Return txtName.Text
End Get
then also my code works......why is it so?
-
Feb 9th, 2010, 11:03 AM
#17
Re: Property.........Get............Set
Nope, but close.
The SET portion of a property is called when you SET the property to a given value, and the GET portion of the property is called when you GET the value.
Forget the whole form passing data thing for a second and just think about this property
Code:
Private _CustomerName as String
Property CustomerName() as String
Get
Return _CustomerName
End Get
Set(Value as string)
_CustomerName = Value
End Set
End Property
If I assign a value to this property (as in SET the value), the SET portion is called, as in
CustomerName = "Bob Jones"
if I try to retrieve the value (as in GET the value), the GET portion is called, as in:
Messagebox.Show(CustomerName)
does that make any more sense?
-
Feb 9th, 2010, 11:04 AM
#18
Re: Property.........Get............Set
I would call the txtName property 'CustName' so you end up with:
Code:
Public Property CustName As String
Get
Return txtName.Text
End Get
Set(ByVal value As String)
txtName.Text = value
End Set
End Property
Form2.Name
Last edited by JuggaloBrotha; Feb 9th, 2010 at 11:10 AM.
-
Feb 9th, 2010, 11:05 AM
#19
Re: Property.........Get............Set
Juggalo, the only issue with that is if the property is defined in a form, a form already has a "Name" property, and will conflict.
-
Feb 9th, 2010, 11:11 AM
#20
Thread Starter
Fanatic Member
Re: Property.........Get............Set
[QUOTE=kleinma;3725186 Nope, but close.
The SET portion of a property is called when you SET the property to a given value, and the GET portion of the property is called when you GET the value.
Forget the whole form passing data thing for a second and just think about this property
[CODE]Private _CustomerName as String
Property CustomerName() as String
Get
Return _CustomerName
End Get
Set(Value as string)
_CustomerName = Value
End Set
End Property[/CODE]QUOTE]
now i get your point i think.....in this case i am setting a value to the txtname of the form2,hence only the set part is executed......if i would have again retrieved the value of the txtName of form2 in some other form then get portion of the property will be executed..
did i get it correctly this time?
-
Feb 9th, 2010, 11:14 AM
#21
Re: Property.........Get............Set
yup, that sounds like you are starting to get the idea of properties.
-
Feb 9th, 2010, 11:23 AM
#22
Thread Starter
Fanatic Member
Re: Property.........Get............Set
well i want to exchange data between two forms and no need to validate the data before passing it from one form to other.......so do i stick with this:
Code:
me.txtName.text=form1.txtName.text
-
Feb 9th, 2010, 11:32 AM
#23
Thread Starter
Fanatic Member
Re: Property.........Get............Set
one more thing:
if i leave the get portion empty then i am getting a warning.......is there a way to remove it?
i dont need the get portion of the code then why to write it?
-
Feb 9th, 2010, 11:36 AM
#24
Fanatic Member
Re: Property.........Get............Set
 Originally Posted by Tommy.net
so it means after executing this line of form1:
Code:
Form2.getValue = txtName.Text
the control moves to the set portion of the property:
Code:
Set(ByVal value As String)
txtName.Text = value
End Set
and then finally executes the get portion:
Code:
Get
Return txtName.Text
End Get
am i getting it right?
well yes and no. what you are doing is setting the property to hold the value contained in the txtname TextBox. If you need to set the TxtBox to the getValue Property you need to use
Code:
txtName.Text = form2.getValue
an example of a use might help. Form1 contains a button to add a first Name and a Last name and places the result in a property in form2 then when form2 is loaded it takes this value and places it into the textbox on form2
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim firstName As String
Dim lastName As String
firstName = txtFirstName.Text
lastName = txtLastName.Text
Form2.WholeName = firstName + " " + lastName
Form2.Show()
End Sub
End Class
Code:
Public Class Form2
Private _wholeName As String
Public Property WholeName() As String
Get
Return _wholeName
End Get
Set(ByVal value As String)
_wholeName = value
End Set
End Property
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
txtWholeName.Text = WholeName
End Sub
End Class
Note though this is just an example to show some of the concepts.
If debugging is the process of removing bugs, then programming must be the process of putting them in.
-
Feb 9th, 2010, 12:41 PM
#25
Re: Property.........Get............Set
 Originally Posted by Tommy.net
one more thing:
if i leave the get portion empty then i am getting a warning.......is there a way to remove it?
i dont need the get portion of the code then why to write it?
I can't imagine many situations where you never need a Getter, but if you think it is justified you can make your property a WriteOnly property. Then you don't need the Get (in fact, you can't have a Get) portion. The other way around is a ReadOnly property; then you don't have a Set portion. ReadOnly properties are much more common than WriteOnly properties. In fact I don't think I have ever seen a WriteOnly property before.
Simply specify WriteOnly in the property identifier:
Code:
Public WriteOnly Property ...
-
Feb 9th, 2010, 12:59 PM
#26
Re: Property.........Get............Set
Or just don't use a property at all, and use a sub routine.
-
Feb 9th, 2010, 01:48 PM
#27
Frenzied Member
Re: Property.........Get............Set
It took me a while to get the hang of properties & I still refrain from using them if possible. To pass data to a form I find it easiest to use the forms Tag property. Just set the value in form1 & retrieve it in form2's load event. I'm not sure that that's the way the "experts" would do it, but it works well for my needs.
-
Feb 9th, 2010, 02:06 PM
#28
Thread Starter
Fanatic Member
Re: Property.........Get............Set
 Originally Posted by nbrege
It took me a while to get the hang of properties & I still refrain from using them if possible. To pass data to a form I find it easiest to use the forms Tag property. Just set the value in form1 & retrieve it in form2's load event. I'm not sure that that's the way the "experts" would do it, but it works well for my needs.
can you give an example?
-
Feb 9th, 2010, 02:10 PM
#29
Re: Property.........Get............Set
 Originally Posted by nbrege
It took me a while to get the hang of properties & I still refrain from using them if possible. To pass data to a form I find it easiest to use the forms Tag property. Just set the value in form1 & retrieve it in form2's load event. I'm not sure that that's the way the "experts" would do it, but it works well for my needs.
It may be easiest, but it is far from the "best" way to do it. I can think of at multiple ways to do it before I would consider using the tag property of a form, especially if you need multiple values.
-
Feb 9th, 2010, 02:12 PM
#30
Frenzied Member
Re: Property.........Get............Set
In Form1:
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Using frm As New Form2
frm.Tag = "some text to pass to form2"
frm.ShowDialog()
End Using
End Sub
End Class
In Form2:
Code:
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myString As String = Me.Tag.ToString
Me.TextBox1.Text = myString
End Sub
End Class
-
Feb 9th, 2010, 02:14 PM
#31
Frenzied Member
Re: Property.........Get............Set
 Originally Posted by kleinma
It may be easiest, but it is far from the "best" way to do it. I can think of at multiple ways to do it before I would consider using the tag property of a form, especially if you need multiple values.
Agreed ... I never claimed it was "best" way, but it does work. And you can pass multiple values if you pass an array. (or any object for that matter).
-
Feb 9th, 2010, 02:18 PM
#32
Re: Property.........Get............Set
well by multiple values I meant of different data types, but even so, you greatly lose type safety and compile time checking if you use this method. It is way more susceptible to runtime failure.
-
Feb 9th, 2010, 02:18 PM
#33
Fanatic Member
Re: Property.........Get............Set
 Originally Posted by nbrege
Agreed ... I never claimed it was "best" way, but it does work. And you can pass multiple values if you pass an array. (or any object for that matter).
can you give an example of passing multiple values in array
-
Feb 9th, 2010, 02:25 PM
#34
Frenzied Member
Re: Property.........Get............Set
To pass multiple strings:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strings As String() = {"one", "two", "three"}
Using frm As New Form2
frm.Tag = strings
frm.ShowDialog()
End Using
End Sub
Code:
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myString As String() = DirectCast(Me.Tag, String())
Me.TextBox1.Text = myString(0) & " " & myString(1) & " " & myString(2)
End Sub
End Class
I would take kleinma's advice & learn the correct way to use properties though, as I don't want to pass along any bad habits.
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
|