Defining/Using Properties - Please Help

XGamerX

Newcomer
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:

Visual Basic:
Structure StructA 
    Dim UserName
    Dim Age
End Structure

Now declare a variable:

Visual Basic:
 Dim VarA As StructA

now the property:

Visual Basic:
Property PropA As ?????(StructA)
  Get 
     Return VarA.??????
  End Get

  Set
     ????????
  End Set
End Property
.....


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:
You would just define the property as StructA:

Visual Basic:
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:

Visual Basic:
 PropA.Member = <Data>

I expect it to simply set
Visual Basic:
 VarA.Member = Data

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 you're 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 won't add much overhead compared to a structure so there isn't much to lose.
 
Wow, thats a shame thx for your help. I really think that Microsoft should look into changing that.....
 
Back
Top