Custome Control Problem
-
I am creating a Custom Server Control, where one of the properties should be the data type of another property for example: for an instance
x
of my control, I need the user of my control to be able to write something like:x.property1 = "abc";
x.property1Type = System.String;So, how to define
property1Type
within the classforeach(Minute m in MyLife) myExperience++;
-
I am creating a Custom Server Control, where one of the properties should be the data type of another property for example: for an instance
x
of my control, I need the user of my control to be able to write something like:x.property1 = "abc";
x.property1Type = System.String;So, how to define
property1Type
within the classforeach(Minute m in MyLife) myExperience++;
Best way to do it using
Generic
in C#cheers, Abhijit My Latest Article : IIS 7.0 and Deploying Asp.Net WebSites on IIS 7.0
-
Best way to do it using
Generic
in C#cheers, Abhijit My Latest Article : IIS 7.0 and Deploying Asp.Net WebSites on IIS 7.0
I am familiar with Generic BUT, I can not create a custome control from a generic class (I think)
foreach(Minute m in MyLife) myExperience++;
-
I am familiar with Generic BUT, I can not create a custome control from a generic class (I think)
foreach(Minute m in MyLife) myExperience++;
Mohammed Gouda wrote:
BUT, I can not create a custome control from a generic class (I think)
why ? you can create separate class and create object of that class and pass the value and data type.
cheers, Abhijit My Latest Article : IIS 7.0 and Deploying Asp.Net WebSites on IIS 7.0
-
I am creating a Custom Server Control, where one of the properties should be the data type of another property for example: for an instance
x
of my control, I need the user of my control to be able to write something like:x.property1 = "abc";
x.property1Type = System.String;So, how to define
property1Type
within the classforeach(Minute m in MyLife) myExperience++;
AFAIK, you can't use generics here as you won't be able to view this control in the designer if you have a generic type with your class declaration. But you could do something like this
class YourClass
{
public Type Property1Type { get; set; }object property1; public object Property1 { get { return property1; } set { if (value.GetType() == this.Property1Type) property1 = value; else throw new Exception("Invalid type"); } } }
YourClass a = new YourClass();
a.Property1Type = typeof(System.String);
a.Property1 = "Hello";Above code allows to set only the specified type to "Property1". I am not sure this is the correct approach though. If you could tell me why you need this, there may be better ways to do this. If you don't want designer support for this control you can use generic parameter with the class declaration. Then the code will be pretty nice.
class YourClassInGenerics<Property1Type>
{
public Property1Type Property1 { get; set; }
}YourClassInGenerics<string> yourClass = new YourClassInGenerics<string>();
yourClass.Property1 = "Hello";All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
Mohammed Gouda wrote:
BUT, I can not create a custome control from a generic class (I think)
why ? you can create separate class and create object of that class and pass the value and data type.
cheers, Abhijit My Latest Article : IIS 7.0 and Deploying Asp.Net WebSites on IIS 7.0
Abhijit Jana wrote:
why ?
Creating generic control classes won't get designer support as designer don't know which type to be specified. If there is no designer support required, I believe it is possible.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
AFAIK, you can't use generics here as you won't be able to view this control in the designer if you have a generic type with your class declaration. But you could do something like this
class YourClass
{
public Type Property1Type { get; set; }object property1; public object Property1 { get { return property1; } set { if (value.GetType() == this.Property1Type) property1 = value; else throw new Exception("Invalid type"); } } }
YourClass a = new YourClass();
a.Property1Type = typeof(System.String);
a.Property1 = "Hello";Above code allows to set only the specified type to "Property1". I am not sure this is the correct approach though. If you could tell me why you need this, there may be better ways to do this. If you don't want designer support for this control you can use generic parameter with the class declaration. Then the code will be pretty nice.
class YourClassInGenerics<Property1Type>
{
public Property1Type Property1 { get; set; }
}YourClassInGenerics<string> yourClass = new YourClassInGenerics<string>();
yourClass.Property1 = "Hello";All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
Thanks for Correcting Me !!!
cheers, Abhijit My Latest Article : IIS 7.0 and Deploying Asp.Net WebSites on IIS 7.0
-
Thanks for Correcting Me !!!
cheers, Abhijit My Latest Article : IIS 7.0 and Deploying Asp.Net WebSites on IIS 7.0
You are welcome :)
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
You are welcome :)
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
did you check my recent article ?
cheers, Abhijit My Latest Article : IIS 7.0 and Deploying Asp.Net WebSites on IIS 7.0
-
I am creating a Custom Server Control, where one of the properties should be the data type of another property for example: for an instance
x
of my control, I need the user of my control to be able to write something like:x.property1 = "abc";
x.property1Type = System.String;So, how to define
property1Type
within the classforeach(Minute m in MyLife) myExperience++;
Thanks to all participants I got a safe solution Instead of Generic or unknown datatype. I created an abstract class from which I inherited all other classes of which objects may be bound to my new datasource It works well :-\
foreach(Minute m in MyLife) myExperience++;
-
AFAIK, you can't use generics here as you won't be able to view this control in the designer if you have a generic type with your class declaration. But you could do something like this
class YourClass
{
public Type Property1Type { get; set; }object property1; public object Property1 { get { return property1; } set { if (value.GetType() == this.Property1Type) property1 = value; else throw new Exception("Invalid type"); } } }
YourClass a = new YourClass();
a.Property1Type = typeof(System.String);
a.Property1 = "Hello";Above code allows to set only the specified type to "Property1". I am not sure this is the correct approach though. If you could tell me why you need this, there may be better ways to do this. If you don't want designer support for this control you can use generic parameter with the class declaration. Then the code will be pretty nice.
class YourClassInGenerics<Property1Type>
{
public Property1Type Property1 { get; set; }
}YourClassInGenerics<string> yourClass = new YourClassInGenerics<string>();
yourClass.Property1 = "Hello";All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
Thanks for good effort
N a v a n e e t h wrote:
If you could tell me why you need this, there may be better ways to do this.
Exactly, I want to bind business objects to TreeView which does not support this sort of binding. So, I created my own datasource which is supposed to support a variety of business objects I got a safe solution Instead of Generic or unknown datatype. I created an abstract class from which I inherited all other classes of which objects may be bound to my new datasource It works well
N a v a n e e t h wrote:
If you don't want designer support for this control you can use generic parameter with the class declaration. Then the code will be pretty nice.
Yes, I need a designer support However, Thanks N a v a n e e t h for your great help. You are one of the best of CP :-D
foreach(Minute m in MyLife) myExperience++;
-
Thanks for good effort
N a v a n e e t h wrote:
If you could tell me why you need this, there may be better ways to do this.
Exactly, I want to bind business objects to TreeView which does not support this sort of binding. So, I created my own datasource which is supposed to support a variety of business objects I got a safe solution Instead of Generic or unknown datatype. I created an abstract class from which I inherited all other classes of which objects may be bound to my new datasource It works well
N a v a n e e t h wrote:
If you don't want designer support for this control you can use generic parameter with the class declaration. Then the code will be pretty nice.
Yes, I need a designer support However, Thanks N a v a n e e t h for your great help. You are one of the best of CP :-D
foreach(Minute m in MyLife) myExperience++;
Mohammed Gouda wrote:
I created an abstract class from which I inherited all other classes of which objects may be bound to my new datasource
Good. Is this abstract class has some default functionality ? or it is just acting as a base class ? If it is acting as a base class, you should consider moving to an interface. Controls like GridView/Repeater supports data binding objects which has
IEnumerable
implemented. Your control also could do something similar.Mohammed Gouda wrote:
You are one of the best of CP
Thanks. I am delighted :)
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
Mohammed Gouda wrote:
I created an abstract class from which I inherited all other classes of which objects may be bound to my new datasource
Good. Is this abstract class has some default functionality ? or it is just acting as a base class ? If it is acting as a base class, you should consider moving to an interface. Controls like GridView/Repeater supports data binding objects which has
IEnumerable
implemented. Your control also could do something similar.Mohammed Gouda wrote:
You are one of the best of CP
Thanks. I am delighted :)
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
N a v a n e e t h wrote:
Controls like GridView/Repeater supports data binding objects which has IEnumerable implemented. Your control also could do something similar.
Yes, My class alreay implement IHierarchicalEnumerable cause I deal with TreeView control. Thanks for the hint
foreach(Minute m in MyLife) myExperience++;