Click here to Skip to main content
15,889,827 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am having a problem using get{} and set{} in C# (it's for a custom control). Generally, if I declare a variable, it shows up in the property box as expected, and can be set accordingly. If, however, I create a struct, it simply refuses to let me edit the fields. My code is as follows:

C#
public struct myInts
        {
            private int myX, myY, myZ;

            public myInts(int x, int y, int z)
            {
                myX = x;
                myY = y;
                myZ = z;
            }

            public int X
            {
                get { return myX; }
                set { myX = value; }
            }
            public int Y
            {
                get { return myY; }
                set { myY = value; }
            }
            public int Z
            {
                get { return myZ; }
                set { myZ = value; }
            }

            public override string ToString()
            {
                return (myX + ", " + myY + ", " + myZ);
            }
        }

        private myInts integers = new myInts(1, 2, 3);
        public myInts MyIntegers
        {
            get { return integers; }
            set { integers = value; }
        }

It is displayed in the property box, but cannot be expanded and cannot be edited. Any help will be much appreciated
Posted
Updated 25-Nov-10 21:11pm
v2

Hi Kyle,

You'd need a custom property editor for that I'd say.
Look here:
http://msdn.microsoft.com/en-us/library/ms171839.aspx

I would go for the TypeConverter method, since it shouldn't be too
hard to convert a struct to string and back, but that's just my
personal opinion.

Cheers

Manfred
 
Share this answer
 
v3
Comments
Dalek Dave 26-Nov-10 3:54am    
Good Call.
KyleCF89 26-Nov-10 6:44am    
Hi Manfred

Thanks for the link, it explains exactly what I wanted to know with regards to structs.
Manfred Rudolf Bihy 26-Nov-10 6:48am    
Have you decided yet which approach to follow (TypeConverter, Editor, ...) ?
KyleCF89 26-Nov-10 7:04am    
Actually, johannesnestler asked if classes would suffice, or if structs are an absolute must. For what I'm going to be using it for, classes will be perfect. Thanks again though, I was looking for a site like that on msdn but only found related ones that didn't quite tell me what I wanted to know. Maybe I just wasn't looking properly :)
Not realy an answer - sorry

I just added the [TypeConverter(typeof(ExpandableObjectConverter))] attribute to the MyIntegers property and it showed me the values in the property grid, but I couldn't change them, they were reset to the original ones after pressing enter. I realized the designer couldn't serialize it this way. If I changed the struct to a class it worked (after adding a default ctor. Do you realy need to hold it as struct?
 
Share this answer
 
Comments
KyleCF89 26-Nov-10 6:45am    
Hi Johannesnestler

While Mafred's link is definitely the best (and most correct) way to go with structs, you made a very good point: classes work just as well. And (in my opinion) will be the quickest and easiest way to go. Thank you for your help, I think your answer might just be the best.
Why do you necessarily want to make a property of your type which already has properties?

Just use your type as such...
Your can remove your public myInts MyIntegers property.

C#
static void Main(string[] args)
{
  myInts integer = new myInts(5,5,5);
  Console.WriteLine(integer.ToString());
  integer.X = 10;
  integer.Y = 10;
  integer.Z = 10;
  Console.WriteLine(integer.ToString());
  Console.ReadKey();
}


Writes to console:
"5, 5, 5"

"10, 10, 10"
 
Share this answer
 
v2
Comments
KyleCF89 26-Nov-10 6:41am    
Hi Rudolf

Unfortunately doing that defeats the purpose. I want the property box in the design view to show my variables, and expand to allow its values to be edited (in a similar way that fonts are editable in design view when selecting a text box, for example). But thanks anyway, all responses are appreciated.
R. Erasmus 26-Nov-10 6:43am    
I see. np :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900