Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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:

 

Structure StructA 
   Dim UserName
   Dim Age
End Structure 

 

Now declare a variable:

 

 Dim VarA As StructA 

 

now the property:

 

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....

Edited by XGamerX
Posted

More help pls

 

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

 

 PropA.Member = <Data> 

 

I expect it to simply set

 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)

  • *Gurus*
Posted

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.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...