
Choose type of the object in run-time
If you didn't get the other message it should be in this thread. Anyhow,
are you trying to do this??
You have Class1 and Class2, then you want to get Func() that is common to
both of them, right?
The Tag saves it as an object, and you need to typecast it back to Class1 or
Class2 depending on what it is right? If that is the case you would have to
know what it is and use like an if statement: (I'm not totally sure on my
syntax of C# though, still learning)
if (o instanceof Class1) {
((Class1)o).Func();
Quote:
} else if (o instanceof Class2) {
((Class2)o.)Func();
Quote:
}
//(I think 'instanceof' in Java is similar to 'is' in C#)
But if you read my other message and then you make an abstract class and
then derive Class 1 and 2 from the abstract class then you only need one
call as long as Func() is defined in the abstract class:
((Abstract)o).Func();
Then no matter what it is Class1 or Class2 they are both of type Abstract
and will call the method or property or whatever...
Hope that helps,
Kory
Quote:
> Thanks but!!!
> It still says that 'object' does not contain a definition for 'Func()'
> (i dont wanna use function, but property. it is the same problem I think)
> --
> -------------------
> -------------------
> > What you need to do is something called casting, or type casting.
> > If you have:
> > Class1 c = new Class1();
> > object o = c;
> > and say that 'c' has a method called 'Func()', then if you want to use
'o'
> > and call 'Func()' it would look like this:
> > (Class1)o.Func();
> > Notice the cast in front of 'o', you could also type:
> > ((Class1)o).Func()
> > Hope that helps,
> > Kory
Quote:
> > > I have the problem in C#:
> > > I have my own classes and I want to declare another object as one of
> these
> > > classes during run-time of my prog.
> > > Class1 object1 = new Class1();
> > > Class2 object2 = new Class2();
> > > object myObject = object1
> > > Now I cannot use the methods of the Class1 type, because myObject is
> type
> > of
> > > object. I want myObject to knows methods of the Class1 type.... how to
> do
> > > it???? I dont wanna to switch my program and check type of object1 and
> > > create Class1 myObject1 and Class2 myObject2 depends on type of object
i
> > > want assign into.
> > > Thanks
> > > --
> > > -------------------
> > > -------------------