C#.net中的动态属性和静态属性
朋友,
请告诉我. C#.net中的动态属性和静态属性有什么区别?还有一个疑问,我用两种方法创建了一个ComboBox.一种是静态的,另一种是动态的.
在静态"中,我从属性窗口中拖动一个组合框,并添加了一个类似"cmbname"的名称.
现在,我创建了2个Dynamic Combobox.我在Loop语句中命名为"Combo" + i.tostring.
我会收到像这样的cmbname.selecteditems.tostring()或cmbname.Text这样的静态Combobox值.但是如何在运行时获取动态Combobox值,请告诉我.
问候,
Lakshmi Narayanan.S
Hi friends ,
please tell me. What is the Difference between Dynamic Property and Static Property in C#.net ? One more doubt, I Created a ComboBox in two ways .One is Static and another one is Dynamic.
In Static ,i drag a Combobox from property window and i added a name like "cmbname".
Now i created 2 Dynamic Combobox.I give a name "Combo" +i.tostring in Loop statement.
I get static Combobox value like this cmbname.selecteditems.tostring() Or cmbname.Text.But how can i get Dynamic Combobox value at run time Please tell me.
Regards,
Lakshmi Narayanan.S
有两种方法:
1)创建它时,可以在变量或数组/列表中保留对刚创建的ComboBox的引用-如果在循环中创建它,则更可能是后者.
There are a couple of ways:
1) When you create it, you can keep a reference to the ComboBox you just created, either in a variable, or an array / List - the latter being more likely if you created it in a loop.
ComboBox cb = myArrayOfComboBoxes[2];
2)您可以遍历添加了ComboBox的控件(要显示它,必须将其添加到控件列表中)并通过该控件获得它.
2) You could loop through the Controls that you added the ComboBox to (in order to display it, you have to add it to a Controls List) and get at it via that.
foreach (Control c in Controls)
{
ComboBox cb = c as ComboBox)
if (cb != null)
{
if (cb.Name == nameOfComboBoxIWantToPlayWith)
{
...
}
}
}
我更喜欢前者:它更明显.
I prefer the former: it is more obvious.
没有相对于静态属性的动态"属性的概念.问题"{0}和{1}之间的区别是什么"在原则上是无效的."Apple和Apple之间的区别是什么".
方法,属性和字段可以是静态和实例.静态对象无法访问类型的实例,因此它们是在给定的应用程序域"中按类创建的.从技术上讲,区别在于对实例的引用"this"的访问.
让我们从方法开始:
There is not concept of "dynamic" property as opposed to static property. The question "what''s the difference between {0} and {1} is invalid in principle. "What''s the difference between apple and Apple".
Methods, properties and fields can be static and instance. Static ones have no access to the instance of a type, so they are created per class in a given Application Domain. Technically the difference is access to "this", a reference to the instance.
Let''s start from methods:
class MyClass {
internal static void StaticMethod(int parameter) {}
internal void InstanceMethod(int parameter) { A = parameter; this.B = parameter; }
int A, B;
}
MyClass.StaticMethod(3); // by class, instance is not needed and cannot be used
MyClass myInstance = new MyClass();
// "this" is passed to MyClass.InstanceMethod, so it can access A and B;
// in this case, it will modify myInstance.A and myInstance.B
myInstance.InstanceMethod(3);
现在,属性本质上是用于读取和设置属性值"(获取/设置)的特殊方法.当一个属性是静态的时,它只能访问静态成员.但是实例属性也可以访问实例成员:
Now, properties are essentially special methods for reading and setting property "value" (getter/setter). When a property is static, it can access only static members; but instance property can also access instance members:
class MyClass {
internal static int StaticProperty { get { return A; } set { A = value; } }
internal int InstanceProperty { get { return B; } set { B = value; } }
static int A;
int B;
}
//will modify static field MyClass.A:
MyClass.StaticProperty = 3; // by class, instance is not needed and cannot be used
MyClass myInstance = new MyClass();
// "this" is passed to MyClass.InstanceProperty, so it can access A and B;
// in this case, it can modify myInstance.A and myInstance.B; will modify myInstance.B
myInstance.InstanceProperty = 4;
当然,属性不必返回并分配值.它们可以执行任何计算,具有任何副作用等.副作用实际上是该属性的主要目的:允许在类似Assign的接口(获取或设置)后面产生副作用.
现在清楚了吗?
Of course, property does not have to return and assign a value; they can do any calculations, have any side effects, etc. Side effects is actually a main purpose of the property: allow a side effect behind the assign-like interface (get or set).
Is is clear now?