Defining/Using Properties - Please Help

XGamerX

Member
Joined
Jan 12, 2003
Messages
5
Alright, I am using VB.NET and I like using properties but there better be a way to do what I need or its a serious issue. Heres my dilemma:

I have a Structure, lets call it StructA;

Ok lets pretend its been defined.

Now I make a property....

Property ForStructA()

How would I define the property to effectively be able to use it with the struct.....

ex:

[VB]
Structure StructA
Dim UserName
Dim Age
End Structure [/VB]

Now declare a variable:

[VB] Dim VarA As StructA [/VB]

now the property:

[VB]
Property PropA As ?????(StructA)
Get
Return VarA.??????
End Get

Set
????????
End Set
End Property
[/VB]
.....


I want to be able to go like this:

PropA.UserName = Bob
PropA.Age = 14

and for it to set the variable VarA (thru the prop) to:

VarA.UserName = Bob
VarA.Age = 14


.....



i hope that makes sense i tried hard to explain it now pls someone help me out thx....
 
Last edited by a moderator:
You would just define the property as StructA:

Code:
Public Property blah() As StructA
  Get
    Return VarA
  End Get
  Set(ByVal Value As StructA)
    VarA = Value
  End Set
End Property
 
More help pls

Ok I followed you advice and did what you said to define the property but when ever I try to write:

[VB] PropA.Member = <Data> [/VB]

I expect it to simply set [VB] VarA.Member = Data [/VB]

instead it gives me an error saying "Expression is a Value and therefore cannot be the target of an assignment!"

this is stupid, I followed your advice exactly and then I get an error like this, am I doing something wrong, pls try and help me out.... (Im programming in VB, by the way)
 
Ah, that had not occured to me. Structures are value types, and therefore when you make a reference to them as members of another object (as you are doing here) their value is put on the stack and any change to them would not affect the original one stored in your object. This is why youre not allowed to do this.

Take the Size structure used extensively in Windows Forms, for example. You cannot do Me.Size.Width = 500 because of this exact reason. The only way to do this (as far as I know) is to use a class instead of a structure. This wont add much overhead compared to a structure so there isnt much to lose.
 
Wow, thats a shame thx for your help. I really think that Microsoft should look into changing that.....
 
Back
Top