Dynamic Instantiation

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Sorry if this sounds like a newbie question.

I have two classes in my app, A & B. each class has a public method called
"EchoTime()". In my UI ,user chooses wether to call EchoTime() from A or B
and then I have to dynamically instantiate the appropriate class and call tis
method.

1) How can I do that?

2) Is it the concept of late binding?

Thanks
 
One way to do this would be to have a base class or interface that has
EchoTime() in it:

public class TimeEcho
{
public void EchoTime()
{
// Do something
}
}

Then you could have classes A & B defined inheriting from TimeEcho:

public class A : EchoTime
{

}

public class B : EchoTime
{

}

Then depending on what the user selects:

public void DoUsersChoice(string className)
{
EchoTime userSelectedClass =
(EchoTime)Type.GetType(className).GetConstructor(new Type[] {
}).Invoke(new object[] { });
userSelectedClass.EchoTime();
}
 
Sorry those classes should have been defined as:

public class A : TimeEcho
{

}

public class B : TimeEcho
{

}

One way to do this would be to have a base class or interface that has
EchoTime() in it:

public class TimeEcho
{
public void EchoTime()
{
// Do something
}
}

Then you could have classes A & B defined inheriting from TimeEcho:

public class A : EchoTime
{

}

public class B : EchoTime
{

}

Then depending on what the user selects:

public void DoUsersChoice(string className)
{
EchoTime userSelectedClass =
(EchoTime)Type.GetType(className).GetConstructor(new Type[] {
}).Invoke(new object[] { });
userSelectedClass.EchoTime();
}
Sorry if this sounds like a newbie question.

I have two classes in my app, A & B. each class has a public method called
"EchoTime()". In my UI ,user chooses wether to call EchoTime() from A or B
and then I have to dynamically instantiate the appropriate class and call tis
method.

1) How can I do that?

2) Is it the concept of late binding?

Thanks
 
Thanks for your reply,

Which concept of oo this approach is refering to?

Thanks

gmiley said:
Sorry those classes should have been defined as:

public class A : TimeEcho
{

}

public class B : TimeEcho
{

}

One way to do this would be to have a base class or interface that has
EchoTime() in it:

public class TimeEcho
{
public void EchoTime()
{
// Do something
}
}

Then you could have classes A & B defined inheriting from TimeEcho:

public class A : EchoTime
{

}

public class B : EchoTime
{

}

Then depending on what the user selects:

public void DoUsersChoice(string className)
{
EchoTime userSelectedClass =
(EchoTime)Type.GetType(className).GetConstructor(new Type[] {
}).Invoke(new object[] { });
userSelectedClass.EchoTime();
}
Sorry if this sounds like a newbie question.

I have two classes in my app, A & B. each class has a public method called
"EchoTime()". In my UI ,user chooses wether to call EchoTime() from A or B
and then I have to dynamically instantiate the appropriate class and call tis
method.

1) How can I do that?

2) Is it the concept of late binding?

Thanks
 
I created an Interface called IEchoTime and made both classes implement the
interface. When I use your code I get "object reference not set to an
instance of an object"

Your help is apppreciated.

Thanks

gmiley said:
Sorry those classes should have been defined as:

public class A : TimeEcho
{

}

public class B : TimeEcho
{

}

One way to do this would be to have a base class or interface that has
EchoTime() in it:

public class TimeEcho
{
public void EchoTime()
{
// Do something
}
}

Then you could have classes A & B defined inheriting from TimeEcho:

public class A : EchoTime
{

}

public class B : EchoTime
{

}

Then depending on what the user selects:

public void DoUsersChoice(string className)
{
EchoTime userSelectedClass =
(EchoTime)Type.GetType(className).GetConstructor(new Type[] {
}).Invoke(new object[] { });
userSelectedClass.EchoTime();
}
Sorry if this sounds like a newbie question.

I have two classes in my app, A & B. each class has a public method called
"EchoTime()". In my UI ,user chooses wether to call EchoTime() from A or B
and then I have to dynamically instantiate the appropriate class and call tis
method.

1) How can I do that?

2) Is it the concept of late binding?

Thanks
 
Why use reflection for such a small thing?
Cant the code be written like this?

if (classname == "A")
{
A obj = new A();
obj.EchoTime();
}
else
{
B obj = new B();
obj.EchoTime();
}

HTH
Kalpesh
 
It is not small thing.I made it small so people can answer me.If I follow
what you suggest (Like what to came to my mind at the beggining) , I have to
duplicate lines of code for each condition ,don't you think it is not as
pretty as you do it dynamically?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top