473,793 Members | 2,742 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting object to class

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(obj ect theClass) {

}

public class Myclass1 {
int myfield1;
string myfield2;
}

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

If I do DoSomething(Myc lass1), I can acces MemebrInfo, FIeldInfo and so one
about theClass, but I cannot use theClass.myfiel d1, 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.myfiel d1 ? I tried different methods, but none of the worked

Thanks

Dominique
Nov 16 '05 #1
3 2404
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.co m

"DDE" <dd*@aireinfo.c om> wrote in message
news:Ot******** ********@TK2MSF TNGP10.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(obj ect theClass) {

}

public class Myclass1 {
int myfield1;
string myfield2;
}

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

If I do DoSomething(Myc lass1), I can acces MemebrInfo, FIeldInfo and so one about theClass, but I cannot use theClass.myfiel d1, 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.myfiel d1 ? I tried different methods, but none of the worked

Thanks

Dominique

Nov 16 '05 #2
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 [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:#3******** ******@tk2msftn gp13.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.co m

"DDE" <dd*@aireinfo.c om> wrote in message
news:Ot******** ********@TK2MSF TNGP10.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(obj ect theClass) {

}

public class Myclass1 {
int myfield1;
string myfield2;
}

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

If I do DoSomething(Myc lass1), I can acces MemebrInfo, FIeldInfo and so

one
about theClass, but I cannot use theClass.myfiel d1, 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.myfiel d1 ? I tried different methods, but none of the worked

Thanks

Dominique


Nov 16 '05 #3
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.co m

"DDE" <dd*@aireinfo.c om> wrote in message
news:ea******** ******@TK2MSFTN GP12.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.c om> wrote in message news:#3******** ******@tk2msftn gp13.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.co m

"DDE" <dd*@aireinfo.c om> wrote in message
news:Ot******** ********@TK2MSF TNGP10.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(obj ect theClass) {

}

public class Myclass1 {
int myfield1;
string myfield2;
}

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

If I do DoSomething(Myc lass1), I can acces MemebrInfo, FIeldInfo and
so one
about theClass, but I cannot use theClass.myfiel d1, 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.myfiel d1 ? I tried different methods, but none of the worked

Thanks

Dominique



Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
3339
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 instance!
0
3835
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 programmer, but this is my first .NET app... Original Code (what I have been doing and works) VB6 (extra stuff removed for clarity) Set objWbem = CreateObject("WbemScripting.SWbemLocator") Set objSvc = objWbem.ConnectServer(ComputerName, "root")
3
2473
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 System.Xml.Serialization, i think that can solve my problem. But i tried using the Deserialize method for converting the XML to the Corresponding Object, neither i get error nor i get any luck for converting it to Object.
4
3196
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 get public members but not protected, private, static members"
11
1415
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 used a lot of methods in the VisualBasic Namespace. These methods are really valuable and it seems they might be the reason some say VB.Net is more productive that C# albeit less safe with option strict turned off. My question is: Is there any...
9
2579
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) example: In Project RoObjDefs: RoPerson.cls file: Public Property Get FirstName() as String Public Property Get LastName() as String <end of file RoPerson.cls> RoPersons.cls file Public Function Count() as Integer
2
1439
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 (IDataProviderBase)(((ConstructorInfo)cache).Invoke(null) );
5
9631
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 class to produce values in the system. For instance I could create class MsgGenericValue<UInt16>() which would represent an unsigned value on the interface.
2
4010
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 functions strtol, strtod, and strtoul, defined in <cstdlib>, convert a null- terminated character string to a long int, double, or unsigned long. You can use them to convert numeric strings of any base to a numeric
25
4395
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 opened it in Visual Studio 2005. I've cleared up most errors, but have one that beats the heck out of me. I have a page: sales/Item.aspx that has several datagrids in it.
0
9518
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10433
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10212
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9035
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.