Passing a Class as a method parameter?
-
I mean be an idiot for asking this question too but is there a way to pass a class as a parameter of a method i.e. I have a repeated block of code like this: class pTermsEditor; pTermsEditor = new class(); pnlViews.Controls.Add(pTermsEditor); pTermsEditor.Dock = DockStyle.Fill; This Block of code is repeated many times but the class type may and almose always does change, so I want to control it more by putting it into a method something like this: myMethod(class) { class x; x = new class(); //blah blah blah } Any help or even a better solution what be greatly appreciated
-
I mean be an idiot for asking this question too but is there a way to pass a class as a parameter of a method i.e. I have a repeated block of code like this: class pTermsEditor; pTermsEditor = new class(); pnlViews.Controls.Add(pTermsEditor); pTermsEditor.Dock = DockStyle.Fill; This Block of code is repeated many times but the class type may and almose always does change, so I want to control it more by putting it into a method something like this: myMethod(class) { class x; x = new class(); //blah blah blah } Any help or even a better solution what be greatly appreciated
You can use the object type, and it is a valid question. for example
myMethod(object obj) { if (obj.GetType() == typeof(classA)) { ClassA objA; objA = (ClassA)obj; return; } if (obj.GetType() == typeof(classB)) { ClassB objB; objB = (ClassB)obj; return; } }
how vital enterprise application are for proactive organizations leveraging collective synergy to think outside the box and formulate their key objectives into a win-win game plan with a quality-driven approach that focuses on empowering key players to drive-up their core competencies and increase expectations with an all-around initiative to drive up the bottom-line. But of course, that's all a "high level" overview of things --thedailywtf 3/21/06 -- modified at 14:58 Thursday 6th July, 2006 -
I mean be an idiot for asking this question too but is there a way to pass a class as a parameter of a method i.e. I have a repeated block of code like this: class pTermsEditor; pTermsEditor = new class(); pnlViews.Controls.Add(pTermsEditor); pTermsEditor.Dock = DockStyle.Fill; This Block of code is repeated many times but the class type may and almose always does change, so I want to control it more by putting it into a method something like this: myMethod(class) { class x; x = new class(); //blah blah blah } Any help or even a better solution what be greatly appreciated
You can pass a Type object and then use Activator.CreateInstance to create an instance via the default (parameterless) constructor. Or, if you're using .NET 2, you can make a generic method whose type parameter is the type that you want to create. Then just do T obj = new T(); where T is your type parameter name. If all else fails, you can always do the magical type parameter dance and everything should work out fine. ;) :josh: My WPF Blog[^]
-
I mean be an idiot for asking this question too but is there a way to pass a class as a parameter of a method i.e. I have a repeated block of code like this: class pTermsEditor; pTermsEditor = new class(); pnlViews.Controls.Add(pTermsEditor); pTermsEditor.Dock = DockStyle.Fill; This Block of code is repeated many times but the class type may and almose always does change, so I want to control it more by putting it into a method something like this: myMethod(class) { class x; x = new class(); //blah blah blah } Any help or even a better solution what be greatly appreciated
-
You can pass a Type object and then use Activator.CreateInstance to create an instance via the default (parameterless) constructor. Or, if you're using .NET 2, you can make a generic method whose type parameter is the type that you want to create. Then just do T obj = new T(); where T is your type parameter name. If all else fails, you can always do the magical type parameter dance and everything should work out fine. ;) :josh: My WPF Blog[^]
That'll work, so long as you put a new() (that is, default constructor) constraint on the method:
void CreateNew<T>() where T : new()
{
T instance = new T();
...
}Tech, life, family, faith: Give me a visit. I'm currently blogging about: Messianic Instrumentals (with audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
That'll work, so long as you put a new() (that is, default constructor) constraint on the method:
void CreateNew<T>() where T : new()
{
T instance = new T();
...
}Tech, life, family, faith: Give me a visit. I'm currently blogging about: Messianic Instrumentals (with audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango