473,378 Members | 1,543 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,378 software developers and data experts.

casting question

i just can't figure out why something im doing is not
working correctly....

public interface IInterface
{
int someProperty
{
get;
set;
}
}

public abstract class II_interface : IInterface
{
private int m_someProperty;

public int someProperty
{
get
{
return m_someProperty;
}
set
{
m_someProperty = value;
}
}
}

public class myDerivedClass : II_interface
{
private int m_someOtherProperty;

public int SomeOtherProperty
{
get
{
return m_someOtherProperty;
}
set
{
m_someOtherProperty = value;
}
}

public int SomeImplementedProperty
{
get
{
return base.SomeProperty*2;
}
}
}

public class myDerivedClass2 : II_interface
{
private int m_someOtherProperty2;

public int SomeOtherProperty2
{
get
{
return m_someOtherProperty2;
}
set
{
m_someOtherProperty2 = value;
}
}
}

public class Test
{
myDerivedClass c1 = new myDerivedClass();
myDerivedClass2 c2 = (myDerivedClass2)c1; //
EXCEPTION: Specified cast is not valid.

IInterface c3 = new myDerivedClass();
myDerivedClass2 c4 = (myDerivedClass)c3;//
invalid cast
}
my question is why am i getting the invalid cast
exception. If this is not possible, how can i make it
possible.
What i'm trying to do, is
1) make one class with properties.
2) derive several other class from the first class, each
class having it's own set of properties (and since im
deriving, all the properties from the first class)

3) create an array of the first class
4) for each element in array, based on a unigue type or
enum, cast the element to the correct derived class.

This kinka like a factory pattern, but i didn't want to
implement the factory.. i just want to be able to cast,
so I can set up the first classes' properties, then use
the derived classes to access the already setup variables
in the first class. the first class will be the only
class with a public contructor. The rest of the derived
classes will be casted, to use the base classes already
created/filled properties.

Thanks
Kurt Lange
Nov 15 '05 #1
3 1660
>
public class Test
{
myDerivedClass c1 = new myDerivedClass(); my question is why am i getting the invalid cast
exception.
You can't because althought myDerivedClass and myDerivedClass2 implements
the same interface, they are not the same with each other. myDerivedClass2
is *not* myDerivedClass. myDerivedClass2 is II_interface.

Why do you want to cast on the list into different types? Can't you just
store the correct object in the list? For example:

IInterface[] array = new IInterface[] {new myDerivedClass1(), new
myDerivedClass2(), new myDerivedClass3()};

You can also substitute IInterface with a BaseClass.

HTH,

/m
If this is not possible, how can i make it
possible.
What i'm trying to do, is
1) make one class with properties.
2) derive several other class from the first class, each
class having it's own set of properties (and since im
deriving, all the properties from the first class)

3) create an array of the first class
4) for each element in array, based on a unigue type or
enum, cast the element to the correct derived class.

This kinka like a factory pattern, but i didn't want to
implement the factory.. i just want to be able to cast,
so I can set up the first classes' properties, then use
the derived classes to access the already setup variables
in the first class. the first class will be the only
class with a public contructor. The rest of the derived
classes will be casted, to use the base classes already
created/filled properties.

Thanks
Kurt Lange

Nov 15 '05 #2
no... the array is created dynamically.
and no... that defeats the purpose of what im trying todo..

encapsulate all initializing of variables in base class...
derive from it... by deriving from base class, and
casting, derived classes would already have their
variables initialiezed(cause they have already been
initialized in the base class) ....

abstract class II_interface
{
private m_var;

public int varVar
{
get
{ return m_var; }
set
{ m_var = value; }
}
}

class myDerived : II_derived
{
public myDerived(int val)
{ base.varVar = val }

public int varVarVar
{
get { return base.varVar; }
}
}

class myDerived2 : II_derived
{
public int varVarVar
{
get { return base.varVar; }
}
}

class test
{
int function
{
II_interface c1 = new myDerived(6);
II_interface c2 = (myDerived2)c1;

// this would print "6" if this function were called
Console.WriteLine("{0}",c2.varVarVar.ToString());
}
}

Notice how im initializing in the first class... and
gather the values int he second class, without using the
constructor on the second class.. im simply casting.

This way im doing the work in the first class,
initializing variables, calling the contructor, etc.

Then by casting, all the work is done for me.. and the
second class can use the vars in first class that are
already initialized.

Thanks
-----Original Message-----

public class Test
{
myDerivedClass c1 = new myDerivedClass();
my question is why am i getting the invalid cast
exception.


You can't because althought myDerivedClass and

myDerivedClass2 implementsthe same interface, they are not the same with each other. myDerivedClass2is *not* myDerivedClass. myDerivedClass2 is II_interface.

Why do you want to cast on the list into different types? Can't you juststore the correct object in the list? For example:

IInterface[] array = new IInterface[] {new myDerivedClass1 (), newmyDerivedClass2(), new myDerivedClass3()};

You can also substitute IInterface with a BaseClass.

HTH,

/m
If this is not possible, how can i make it
possible.
What i'm trying to do, is
1) make one class with properties.
2) derive several other class from the first class, each
class having it's own set of properties (and since im
deriving, all the properties from the first class)

3) create an array of the first class
4) for each element in array, based on a unigue type or
enum, cast the element to the correct derived class.

This kinka like a factory pattern, but i didn't want to
implement the factory.. i just want to be able to cast,
so I can set up the first classes' properties, then use
the derived classes to access the already setup variables in the first class. the first class will be the only
class with a public contructor. The rest of the derived
classes will be casted, to use the base classes already
created/filled properties.

Thanks
Kurt Lange

.

Nov 15 '05 #3
> no... the array is created dynamically.
and no... that defeats the purpose of what im trying todo..

encapsulate all initializing of variables in base class...
derive from it... by deriving from base class, and
casting, derived classes would already have their
variables initialiezed(cause they have already been
initialized in the base class) ....

You can't do that. What you are trying to do is 'changing' the class type.
Casting is not to change the object type. Consider this class diagram:

A
^
|--------|
B C
^
|------------|
D E
D d = new D();
A a = (D) d; ///// OK

BUT

A a = new A();
D d = (A) a /// NOT!

Think of inheritance is an "IS A" problem. D IS A, but A IS NOT B. So you
can't cast A to B. Similary in your problem what you are trying to do is
this:

A a = new B() // OK because B IS A
A a2 = new C() // OK because C is A

A a3 = (C) a // NOT ok because B IS NOT C

What you need to do is to create a constructor in C that takes B to do the
work for you. For example:

A a = new B();
A a2 = new C(a);
HTH,

/m

abstract class II_interface
{
private m_var;

public int varVar
{
get
{ return m_var; }
set
{ m_var = value; }
}
}

class myDerived : II_derived
{
public myDerived(int val)
{ base.varVar = val }

public int varVarVar
{
get { return base.varVar; }
}
}

class myDerived2 : II_derived
{
public int varVarVar
{
get { return base.varVar; }
}
}

class test
{
int function
{
II_interface c1 = new myDerived(6);
II_interface c2 = (myDerived2)c1;

// this would print "6" if this function were called
Console.WriteLine("{0}",c2.varVarVar.ToString());
}
}

Notice how im initializing in the first class... and
gather the values int he second class, without using the
constructor on the second class.. im simply casting.

This way im doing the work in the first class,
initializing variables, calling the contructor, etc.

Then by casting, all the work is done for me.. and the
second class can use the vars in first class that are
already initialized.

Thanks
-----Original Message-----

public class Test
{
myDerivedClass c1 = new myDerivedClass();

my question is why am i getting the invalid cast
exception.


You can't because althought myDerivedClass and

myDerivedClass2 implements
the same interface, they are not the same with each

other. myDerivedClass2
is *not* myDerivedClass. myDerivedClass2 is II_interface.

Why do you want to cast on the list into different types?

Can't you just
store the correct object in the list? For example:

IInterface[] array = new IInterface[] {new myDerivedClass1

(), new
myDerivedClass2(), new myDerivedClass3()};

You can also substitute IInterface with a BaseClass.

HTH,

/m
If this is not possible, how can i make it
possible.
What i'm trying to do, is
1) make one class with properties.
2) derive several other class from the first class, each
class having it's own set of properties (and since im
deriving, all the properties from the first class)

3) create an array of the first class
4) for each element in array, based on a unigue type or
enum, cast the element to the correct derived class.

This kinka like a factory pattern, but i didn't want to
implement the factory.. i just want to be able to cast,
so I can set up the first classes' properties, then use
the derived classes to access the already setup variables in the first class. the first class will be the only
class with a public contructor. The rest of the derived
classes will be casted, to use the base classes already
created/filled properties.

Thanks
Kurt Lange

.

Nov 15 '05 #4

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

Similar topics

231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
0
by: Kurt Lange | last post by:
no... the array is created dynamically. and no... that defeats the purpose of what im trying todo.. encapsulate all initializing of variables in base class... derive from it... by deriving...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
18
by: Marco | last post by:
I need to get a iterator from any generic collection. public class .... GetIterator(Object collection) { ..... }
1
by: Remco | last post by:
Hi, Let me try to simply explain my questions. I've created a portal site with different types of users, e.g. Portal Administrators and Normal Users. One base class SessionUser (has a enum...
2
by: Enrique Bustamante | last post by:
Casting arrays that works on watch and command window but not in code. My application is casting arrays in a way it should work. To test if I was doing something invalid, I wrote a test code that...
14
by: Daniel | last post by:
Hi guys who just answered me.....it really would have helped if i had written it right. Ok i will use better names to explain my problem. I have this: InterFaceClass ^ ClassA
9
by: Naomi | last post by:
I need to make software engineering decision to do with using a derived data type in a container class. So for example, if I have an Edge class, and I want to make a Edge object which contains two...
5
by: Ronald Raygun | last post by:
If I have the following class heirarchy: class A{ protected $m_type; function type(){return $this->m_type;} } class B extends A{} class C extends B{}
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
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...
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?

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.