使用结构帮助C#get {}和set {} [英] Help with C# get{} and set{} using structs
本文介绍了使用结构帮助C#get {}和set {}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在C#中使用get{}
和set{}
时遇到问题(用于自定义控件).通常,如果我声明一个变量,它会按预期显示在属性框中,并且可以相应地进行设置.但是,如果我创建一个结构,它只是拒绝让我编辑字段.我的代码如下:
public struct myInts
{
私有 int myX,myY,myZ;
public myInts( int x, int y , int z)
{
myX = x;
myY = y;
myZ = z;
}
公共 int X
{
获取 {返回 myX; }
set {myX = value ; }
}
公共 int 是
{
获取 {返回 myY; }
set {myY = value ; }
}
公共 int Z
{
获取 {返回 myZ; }
set {myZ = value ; }
}
公共 覆盖 字符串 ToString()
{
返回(myX + " + myY + " + myZ);
}
}
私有 myInts整数= 新 myInts( 1 , 2 , 3 );
公共 myInts MyIntegers
{
获取 {返回整数; }
set {整数= 值; }
}
它显示在属性框中,但是不能展开和编辑.任何帮助将不胜感激
解决方案
凯尔,
您需要一个自定义的属性编辑器.
看这里:
http://msdn.microsoft.com/en-us/library/ms171839.aspx
我会选择TypeConverter方法,因为它不应该太
很难将结构转换为字符串然后返回,但这只是我的
个人意见.
干杯
曼弗雷德(Manfred)
不是真正的答案-抱歉
我刚刚在MyIntegers属性中添加了[TypeConverter(typeof(ExpandableObjectConverter))]属性,它向我显示了属性网格中的值,但是我无法更改它们,按Enter后它们将重置为原始值.我意识到设计师无法以这种方式序列化它.如果我将结构更改为可以工作的类(添加默认ctor之后.您是否真的需要将其保留为结构?
为什么一定要创建您类型的属性?
哪些已经具有属性?
就这样使用您的类型...
您可以删除公共myInts MyIntegers属性.
静态 void Main(字符串 []参数) { myInts整数= 新 myInts( 5 , 5 , 5 ); Console.WriteLine(integer.ToString()); integer.X = 10 ; Y = 10 ; integer.Z = 10 ; Console.WriteLine(integer.ToString()); Console.ReadKey(); }
写入控制台:
"5, 5, 5"
"10, 10, 10"
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:
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
解决方案
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
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?
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.
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"
这篇关于使用结构帮助C#get {}和set {}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文