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

casting question (repost)

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 #1
0 1295

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

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
3
by: Adam | last post by:
I've posted about this previously, but failed to receive a satisfactory response, so have included a code sample: I am trying to receive messages from an HTML viewer control in compact.net (c#),...
14
by: Steve McLellan | last post by:
Hi, Sorry to repost, but this is becoming aggravating, and causing me a lot of wasted time. I've got a reasonably large mixed C++ project, and after a number of builds (but not a constant...
5
by: Adrian Parker | last post by:
I've got the standard SqlCacheDependency working just fine , ie. I've defined (and encrypted) the connectionStrings section in the web.config, and I've also defined an an sqlCacheDependency in the...
2
by: Gerry | last post by:
I have a combo box and I can populate it with my class of dat (the class allows me to store each userid,username called - see code below I want the user to select the dropdown and see the...
2
by: Raj | last post by:
Hi, I have the following problem. I am displaying and printing a PDF file that is generated by my Application server. The print dialogs comes up correctly for the small PDF for the larger PDFs...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
2
by: John Smith | last post by:
Hi All, (I first posted this question on the csharp.general but got no response, and since this NG is more active, so I repost it here) I have two enum definitions from two different...
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: 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: 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
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.