Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
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
Posted
Updated 30-May-11 19:55pm
v2

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) 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.
 
Share this answer
 
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:

C#
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:

C#
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; 


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?

—SA
 
Share this answer
 
Comments
Аslam Iqbal 31-May-11 2:59am    
got my 5. op can ask about adding design time property and run time adding property. run time adding property is not conceptually a dynamic property and design time property is not static property. your point of view is correct. but it was not clear.
Sergey Alexandrovich Kryukov 31-May-11 3:06am    
Thank you, Aslam.
You are right. Conceptually, design-time property is no different from any other property, let's forget about them. There are also dependency property, they are used in WPF, but this is completely different beast: http://msdn.microsoft.com/en-us/library/ms752914.aspx.

--SA

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