473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Compare class names of interface inherited classes

Let's say I have this:

interface MyInterface {}
class MyClass1 : MyInterface {...}
class MyClass2 : MyInterface {...}

class SomeClass
{
MyInterface myClassX;
public SomeFunc()
{
if (someCondition)
myClassX = new MyClass1();
else
myClassX = new MyClass2();
}

public AnotherFunc()
{
/*Here I want to see which class the myClassX variable belongs to. How
to do that? */
}
}
Apr 20 '06 #1
3 3236
On Thu, 20 Apr 2006 03:04:02 -0700, Joachim
<Jo*****@discussions.microsoft.com> wrote:
MyInterface myClassX; public AnotherFunc()
{
/*Here I want to see which class the myClassX variable belongs to. How
to do that? */


myClassX.GetType() returns a Type instance.

You can get the Type instance corresponding to a C# type with the
typeof operator, for example "typeof(MyClass1)".

However, if you need to check the type of the class implementing the
interface, you have bigger design problems. You probably need to add a
method to MyInterface which encapsulates what you're trying to do, and
call it directly, and then let polymorphism and selective
implementation in MyClass1 and MyClass2 decide the different
behaviour.

-- Barry
Apr 20 '06 #2
You can write something similar to this as per your need

string x = myClassX is MyClass1 ? "Class 1" : "Class 2";
Console.WriteLine(x);

regards,
pradeep

Joachim wrote:
Let's say I have this:

interface MyInterface {}
class MyClass1 : MyInterface {...}
class MyClass2 : MyInterface {...}

class SomeClass
{
MyInterface myClassX;
public SomeFunc()
{
if (someCondition)
myClassX = new MyClass1();
else
myClassX = new MyClass2();
}

public AnotherFunc()
{
/*Here I want to see which class the myClassX variable belongs to. How
to do that? */
}
}


--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200604/1
Apr 20 '06 #3
Hi,
What you want to do?

You use an interface to precisely avoid having to know what precise type
your instance is.
Also remember that any number of classes can implement the same interface,
what would happen if at a later time another person create a class that also
implement that interface, try to use your code (it does expect an interface
that the new class implement) and your code will not work as this new class
did not exist when you wrote your code?
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Joachim" <Jo*****@discussions.microsoft.com> wrote in message
news:B3**********************************@microsof t.com...
Let's say I have this:

interface MyInterface {}
class MyClass1 : MyInterface {...}
class MyClass2 : MyInterface {...}

class SomeClass
{
MyInterface myClassX;
public SomeFunc()
{
if (someCondition)
myClassX = new MyClass1();
else
myClassX = new MyClass2();
}

public AnotherFunc()
{
/*Here I want to see which class the myClassX variable belongs to.
How
to do that? */
}
}

Apr 20 '06 #4

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

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
0
by: Carlos Ribeiro | last post by:
I thought about this problem over the weekend, after long hours of hacking some metaclasses to allow me to express some real case data structures as Python classes. I think that this is something...
7
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
9
by: phl | last post by:
hi, I am kind of confused aobut interfaces and abstract classes. In short as I understand it, an interface is like a contract between the class and the interface, so that certain funtions must...
3
by: M Shafaat | last post by:
Hi! I want to make a base class with some base properties and then derive different other classes from that in which I want to override the base properties to get specialized behaviour unique for...
10
by: Joe | last post by:
My question is more an OOD question. I know *how* to implement both abstract classes and interfaces. Here's my question - under what circumstacnes does one use an abstract class and under what...
4
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. ...
9
by: David A. Osborn | last post by:
I have a set of classes that each have an enumeration in them, and based on dynamic input I need to access a different enumeration. For example Three classes Class_A, Class_B, and Class_C that...
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
marktang
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,...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.