Converting object to class

D

DDE

Hi all,

I have defined a meththod supposed to do some treatment with the class it
receives as argument. This method can receive different type of classes so
it's argument is defined as an object, see sample beloz

public DoSomething(object theClass) {

}

public class Myclass1 {
int myfield1;
string myfield2;
}

public class Myclass2 {
int thefield1;
string thefield2;
datetime thedate
}

If I do DoSomething(Myclass1), I can acces MemebrInfo, FIeldInfo and so one
about theClass, but I cannot use theClass.myfield1, because although
theClass has a type Myclass1, it does not have a definition for the field
myfield1. How can I "convert" theClass to a real Myclass1 to be able to use
theClass.myfield1 ? I tried different methods, but none of the worked

Thanks

Dominique
 
N

Nicholas Paldino [.NET/C# MVP]

DDE,

You have a few options here:

Reflection - This would be tedious, but would work. You would have to have
the class identify in some manner what the fields are that you seek. You
could have an attribute placed on the class to indicate this, and then use
reflection to get the values and modify them.

Interface/Base Class - This is a better way to go. Basically, have your
classes derive from a base class, and then set the parameter type of
DoSomething to the base class, or have them all implement an interface which
will provide what you need to access the implementation behind the
interface.

Hope this helps.
 
D

DDE

Hi,

thanks for your answer. As you can imagine, I simplified my problem in order
to explain it. I investigated both ways you are talking about, but couldn't
succeed. In fact my classes are indeed derived from a base class. In my
method DoSomething I can than acces fields and properties, but the real
probelm is that I have to use this method recursively, and I do not find the
way to pass a memeber of the first level class to the next level.

Any idea ?

Thanks,

Dominique


Nicholas Paldino said:
DDE,

You have a few options here:

Reflection - This would be tedious, but would work. You would have to have
the class identify in some manner what the fields are that you seek. You
could have an attribute placed on the class to indicate this, and then use
reflection to get the values and modify them.

Interface/Base Class - This is a better way to go. Basically, have your
classes derive from a base class, and then set the parameter type of
DoSomething to the base class, or have them all implement an interface which
will provide what you need to access the implementation behind the
interface.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

DDE said:
Hi all,

I have defined a meththod supposed to do some treatment with the class it
receives as argument. This method can receive different type of classes so
it's argument is defined as an object, see sample beloz

public DoSomething(object theClass) {

}

public class Myclass1 {
int myfield1;
string myfield2;
}

public class Myclass2 {
int thefield1;
string thefield2;
datetime thedate
}

If I do DoSomething(Myclass1), I can acces MemebrInfo, FIeldInfo and so one
about theClass, but I cannot use theClass.myfield1, because although
theClass has a type Myclass1, it does not have a definition for the field
myfield1. How can I "convert" theClass to a real Myclass1 to be able to use
theClass.myfield1 ? I tried different methods, but none of the worked

Thanks

Dominique
 
N

Nicholas Paldino [.NET/C# MVP]

Dominique,

Just like the base can share method implementations, it can share field
members as well. You should have these members as part of your base class,
and then set them in the derived classes as appropriate.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

DDE said:
Hi,

thanks for your answer. As you can imagine, I simplified my problem in order
to explain it. I investigated both ways you are talking about, but couldn't
succeed. In fact my classes are indeed derived from a base class. In my
method DoSomething I can than acces fields and properties, but the real
probelm is that I have to use this method recursively, and I do not find the
way to pass a memeber of the first level class to the next level.

Any idea ?

Thanks,

Dominique


message news:#[email protected]...
DDE,

You have a few options here:

Reflection - This would be tedious, but would work. You would have to have
the class identify in some manner what the fields are that you seek. You
could have an attribute placed on the class to indicate this, and then use
reflection to get the values and modify them.

Interface/Base Class - This is a better way to go. Basically, have your
classes derive from a base class, and then set the parameter type of
DoSomething to the base class, or have them all implement an interface which
will provide what you need to access the implementation behind the
interface.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

DDE said:
Hi all,

I have defined a meththod supposed to do some treatment with the class it
receives as argument. This method can receive different type of
classes
so so
one to
use
 

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

Top