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 3 2388
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]
- mv*@spam.guard.caspershouse.com
"DDE" <dd*@aireinfo.com> wrote in message
news:Ot****************@TK2MSFTNGP10.phx.gbl... 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
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:#3**************@tk2msftngp13.phx.gbl... 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] - mv*@spam.guard.caspershouse.com
"DDE" <dd*@aireinfo.com> wrote in message news:Ot****************@TK2MSFTNGP10.phx.gbl... 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
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]
- mv*@spam.guard.caspershouse.com
"DDE" <dd*@aireinfo.com> wrote in message
news:ea**************@TK2MSFTNGP12.phx.gbl... 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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:#3**************@tk2msftngp13.phx.gbl... 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] - mv*@spam.guard.caspershouse.com
"DDE" <dd*@aireinfo.com> wrote in message news:Ot****************@TK2MSFTNGP10.phx.gbl... 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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sridhar R |
last post by:
Consider the code below,
class Base(object):
pass
class Derived(object):
def __new__(cls, *args, **kwds):
# some_factory returns an instance of Base
# and I have to derive from this...
|
by: ChrisWoodruff |
last post by:
I have a C++ function in a COM object that I am trying to implement in
VB.NET (the functionality, NOT the COM object, I want to remove the
requirement for the COM DLL)
I am an experienced VB...
|
by: Parvesh |
last post by:
hi,
I am using a webservice which Returns the Result in an XML string,
The XML response i get i svery cumbersome to parse,
But if i could convert it to the Corresponding Class using the...
|
by: gg9h0st |
last post by:
i'm a newbie studying php.
i was into array part on tutorial and it says
i'll get an array having keys that from member variable's name
by converting an object to array.
i guessed "i can...
|
by: Tina |
last post by:
I'm learning C# by converting a large asp.net project to C# that was written
in vb.net and option strict was turned off so it's a nice challenge and it
is really teaching me C#.
This project...
|
by: Terry |
last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces.
One of the major uses is to be able to split out Read-only access to an
obect. Let me give you a simple (contrived)...
|
by: TheLongshot |
last post by:
Ok, let's try this again.
I have an ASP.NET 1.1 application that I'm working to convert to 2.0,
but I've run into a snag.
The problem is with this line:
return...
|
by: vtjumper |
last post by:
I'm building a C# interface to an existing messaging system. The
messaging system allows values of several types to be sent/recieved
over the interface.
What I want to do is use a generic...
|
by: CoreyWhite |
last post by:
Problem:
You have numbers in string format, but you need to convert them to a
numeric type, such as an int or float.
Solution:
You can do this with the standard library functions. The...
|
by: Blasting Cap |
last post by:
I keep getting errors that pop up when I am trying to convert an
application from dotnet framework 1.1 to framework 2.0. The old project
was saved in sourcesafe from Visual Studio 2003, and I have...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |