XGamerX Posted January 12, 2003 Posted January 12, 2003 (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 January 12, 2003 by XGamerX Quote
*Gurus* divil Posted January 12, 2003 *Gurus* Posted January 12, 2003 You would just define the property as StructA: Public Property blah() As StructA Get Return VarA End Get Set(ByVal Value As StructA) VarA = Value End Set End Property Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
XGamerX Posted January 17, 2003 Author Posted January 17, 2003 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) Quote
*Gurus* divil Posted January 17, 2003 *Gurus* Posted January 17, 2003 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
XGamerX Posted January 18, 2003 Author Posted January 18, 2003 Wow, thats a shame thx for your help. I really think that Microsoft should look into changing that..... Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.