Choose type of the object in run-time 
Author Message
 Choose type of the object in run-time

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

--
-------------------

-------------------



Sun, 31 Oct 2004 20:52:47 GMT  
 Choose type of the object in run-time

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????

Why do you cast it then?

--
Peter Y. Wu

"I know nothing except the fact of my ignorance." -- Socrates



Sun, 31 Oct 2004 21:24:40 GMT  
 Choose type of the object in run-time
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

> --
> -------------------

> -------------------



Sun, 31 Oct 2004 21:24:23 GMT  
 Choose type of the object in run-time
Because I have the objects type of Class1 and Class2 saved in TreeNode.Tag
object and when I take the saved object from the Tag i need to save it to
one variable. I need to work with one variable. I have a lot of code that is
similar to both classes (but the classes is not similar).

I would retype once declared object...for example:

object myObject

switch(some)
{
    case 1:
        Class1 myObject = SelectedTreeNode.Tag;
        break;
    case 2:
        Class2 myObject = SelectedTreeNode.Tag;
        break;

Quote:
}

It is not possible, I know (multiple definition of one variable)

Is it possible by any other way????.... :-(((

--
-------------------

-------------------


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????

> Why do you cast it then?

> --
> Peter Y. Wu

> "I know nothing except the fact of my ignorance." -- Socrates



Sun, 31 Oct 2004 21:35:23 GMT  
 Choose type of the object in run-time
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)

--
-------------------

-------------------


Quote:
> 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


> > 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

> > --
> > -------------------

> > -------------------



Sun, 31 Oct 2004 21:44:53 GMT  
 Choose type of the object in run-time
Another thing you can try (since you say casting doesn't work,. but it
always should) try what is called inheritance.

Make an abstract class, Abstract1 for instance and then put all of the
common functionality in here, then derive Class1 and Class2 both from here
and then make the differences in those classes.  You can also declare
abstract methods as well if the methods differ.  Then when you are passing
it out or whatever type cast it to Abstract1 and as long as the methods are
the same name and parameter type then it should work.  There should be a lot
of info online about OOP and abstract classes...

Kory

Quote:

> Because I have the objects type of Class1 and Class2 saved in TreeNode.Tag
> object and when I take the saved object from the Tag i need to save it to
> one variable. I need to work with one variable. I have a lot of code that
is
> similar to both classes (but the classes is not similar).

> I would retype once declared object...for example:

> object myObject

> switch(some)
> {
>     case 1:
>         Class1 myObject = SelectedTreeNode.Tag;
>         break;
>     case 2:
>         Class2 myObject = SelectedTreeNode.Tag;
>         break;
> }

> It is not possible, I know (multiple definition of one variable)

> Is it possible by any other way????.... :-(((

> --
> -------------------

> -------------------




> > > 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????

> > Why do you cast it then?

> > --
> > Peter Y. Wu

> > "I know nothing except the fact of my ignorance." -- Socrates



Mon, 01 Nov 2004 01:53:39 GMT  
 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




- Show quoted text -

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

> > > --
> > > -------------------

> > > -------------------



Mon, 01 Nov 2004 02:02:52 GMT  
 Choose type of the object in run-time
Endys,
In addition to the other suggestions.

Consider a couple of variations of what Kory Postma stated.

Create an Interface that both Class1 & Class2 implement.

Then instead of using a variable of type 'object' you would use a variable
the type of this new interface.

This interface would expose common methods used by the various TreeView
events you want to handle...

Further, rather than putting your classes into the Tag property of a normal
TreeNode. Create a new class that inherits from TreeNode and has a property
of this Interface. Plus possible exposes the methods of the interface
itself.  The TreeView events could call into the derived TreeNode class, the
derived TreeNode class would invoke the methods of the class being
referenced by the property...

This leads us to another method that Kory hinted at.

If possible derive Class1 & Class2 from TreeNode... or a common class
derived from TreeNode... Personally I tend to prefer to a custom TreeNode
that holds Class1 or Class2, separation of Business logic from User
Interface logic you know...

Hope this helps
Jay

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

> --
> -------------------

> -------------------



Mon, 01 Nov 2004 10:40:31 GMT  
 Choose type of the object in run-time
OK, maybe better describtion would be this:

I have a collection of different objects.

I need pick up an object from this collection and assign it to another
object.

Do I have to check type of the object Im reading from collection and then
switch between all possible object types to assign it to right object type
or can I do something like:

(collection.item[i].GetType()) myObject = collection.item[i];

I mean some calling type declaration by name
.... for example... in Visual Basic is function CallByName(string
functionName).... it is similar... I need to use type declaration by name of
the type.

Thanks
--
-------------------

-------------------

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

> --
> -------------------

> -------------------



Mon, 01 Nov 2004 21:24:35 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Choosing server at run-time

2. Determining type at run-time or compile-time

3. run time type checking

4. Bug in C# run-time type information?

5. Q: malloc and type info - fatal run-time eror

6. Data types at run time

7. Determine Column Data Type at run time

8. fast run-time type idenitification

9. Run-time type define

10. Run-time type information

11. Enabling Run-Time Type Information: how?

12. Does run-time type info work correctly?

 

 
Powered by phpBB® Forum Software