"object" type problem
-
In vb.net when i declare a variable as object type then i can set any property for that object that is not assigned at design time but but will be assigned at runtime. for example: dim a as object if typeof a is textbox then a.name="test1" end if The above code will not give any error at design time in vb.NET but in C# it will give an error during design time only - "'object' does not contain a definition for 'name'". Please help me,how do i solve this problem. Thanks in advance Sheel Sheel Gohe
-
In vb.net when i declare a variable as object type then i can set any property for that object that is not assigned at design time but but will be assigned at runtime. for example: dim a as object if typeof a is textbox then a.name="test1" end if The above code will not give any error at design time in vb.NET but in C# it will give an error during design time only - "'object' does not contain a definition for 'name'". Please help me,how do i solve this problem. Thanks in advance Sheel Sheel Gohe
The following should work:
if (typeof(a) is TextBox)
{
((TextBox) a).name="test1";
}
-
The following should work:
if (typeof(a) is TextBox)
{
((TextBox) a).name="test1";
}
Thanks Stefan. Sheel Gohe
-
The following should work:
if (typeof(a) is TextBox)
{
((TextBox) a).name="test1";
}
-
Actually the codes should be: object a = new object(); if( a is TextBox ) { ((TextBox) a).Name = "test1"; } Or: object a = new object(); if( a.GetType() is TextBox ) { ((TextBox) a).Name = "test1"; }
Actually, your code is just as incorrect. LdqxYY wrote: object a = new object(); if( a is TextBox ) { ((TextBox) a).Name = "test1"; } If a has been instantiated as an object, then the line if(a is textBox) will always evaluate false then in your second example: LdqxYY wrote: object a = new object(); if( a.GetType() is TextBox ) { ((TextBox) a).Name = "test1"; } you are testing to see whether an instance of System.Type is-a TextBox - which it clearly isnt it is System.Type! To correct these two examples assume that there is a TextBox on the form with the instance name myTextBox. Now assume that you have a variable named myVar which is of type object, but is set to myTextBox
// assumes the lines "object myVar;" and "myVar=myTextBox" if(myVar is TextBox) { ((TextBox)myVar).Text = "Hello World"; }
or in the second example above
if(myVar.GetType() == typeof(TextBox)) { ((TextBox)myVar).Text = "Hello World"; }
-
Actually, your code is just as incorrect. LdqxYY wrote: object a = new object(); if( a is TextBox ) { ((TextBox) a).Name = "test1"; } If a has been instantiated as an object, then the line if(a is textBox) will always evaluate false then in your second example: LdqxYY wrote: object a = new object(); if( a.GetType() is TextBox ) { ((TextBox) a).Name = "test1"; } you are testing to see whether an instance of System.Type is-a TextBox - which it clearly isnt it is System.Type! To correct these two examples assume that there is a TextBox on the form with the instance name myTextBox. Now assume that you have a variable named myVar which is of type object, but is set to myTextBox
// assumes the lines "object myVar;" and "myVar=myTextBox" if(myVar is TextBox) { ((TextBox)myVar).Text = "Hello World"; }
or in the second example above
if(myVar.GetType() == typeof(TextBox)) { ((TextBox)myVar).Text = "Hello World"; }
J4amieC,you are right!And I had realized my second example were incorrect after submitted.By the way,what exact mean in my first example is: object a = new object(); // //some codes handle with the "a" // if( a.GetType() is TextBox ) { ((TextBox) a).Name = "test1"; } I forgot writing something,hehe... Thanks again!
-
J4amieC,you are right!And I had realized my second example were incorrect after submitted.By the way,what exact mean in my first example is: object a = new object(); // //some codes handle with the "a" // if( a.GetType() is TextBox ) { ((TextBox) a).Name = "test1"; } I forgot writing something,hehe... Thanks again!
-
J4amieC,you are right!And I had realized my second example were incorrect after submitted.By the way,what exact mean in my first example is: object a = new object(); // //some codes handle with the "a" // if( a.GetType() is TextBox ) { ((TextBox) a).Name = "test1"; } I forgot writing something,hehe... Thanks again!