- void Whatever(Type ClassType, ArrayList myArrayList)
-
{
-
ClassType myClass = new ClassType();
-
myClass = (ClassType)myArrayList[0];
-
}
-
How can I make this work? ClassType is the type of an homemade class.
17 3910
A) Who says that doesn't work? You haven't said it produces an error.
B) What is in your ArrayList? We have no idea if you are trying to convert a Cat to a Dog or a String to an int
How about making a constructor in your custom class that takes the information stored in your array elements?
It doesnt work, else I would not be asking for help. It just doesnt want to compile. It refuse to take a variable Type for a cast.
In my arraylist, like I said, are homemade class.
The syntax for the cast is legal.
I'm sure you realize this because you know you can cast a float to an int, in this way for example
float Yogi = 0.15f;
int bear = (int)Yogi;
But the compiler doesn't have a crystal ball. If you have a custom class of "Widget" and an arraylist containing "Gadget"s - The compiler just doesn't know *how* it should cast that.
You may need to write a custom cast for your different classes. http://msdn.microsoft.com/en-us/libr...05(VS.80).aspx
Usually I'm working with classes that inherit from a common class, so VIsual Studio can generally do that impliticly (no custom cast function)
class Mammal
class Dog : Mammal
class Cat : Mammal
Dog myDoberman = new Dog();
Cat myTabby = (Cat)myDoberman;// Legal since they are equal classes of same parent
I think you want Invoke: eg http://msdn.microsoft.com/en-us/libr...(v=VS.71).aspx
.. I don't understand why you're creating an object and then shortly thereafter overwriting it. - ConstructorInfo constructorMethod = ClassType.GetConstructor(new Type[0]);
-
object c = constructorMethod.Invoke(new object[0]);
//Assumes you have a public constructor with no arguments.
If you're going to run methods on it, you need to use Type.GetMethod(s)() to get the methods you want to run. Is this really necessary, or could you just use some sort of polymorphism instead, as suggested above?
This was just an example. I would not write you the whole thing because it does 400 lines long. But I need to dynamically cast something as a type I receive as parameter. Your thing for constructor is nice, but I need to cast something that already have variables saved into it.
about:
class Mammal
class Dog : Mammal
class Cat : Mammal
Dog myDoberman = new Dog();
Cat myTabby = (Cat)myDoberman;// Legal since they are equal classes of same parent
That's not it. I dont know that it is a Cat. It could be a Cat, a Dog, an Elephant, an Alien. I dont know beforehand what the cast will be.
It would be more:
class Mammal
class Dog : Mammal
class Cat : Mammal
Type myType = typeof(Cat);
MyType myTabby = (myType)myDoberman;
Which doesnt compile.
Why do you need the cast? What are you doing that afterwards that needs myTabby to be treated as a dynamic type? Any other function can call typeof on the object to determine what type it is anyways.
There's probably an easier way to do it.
Because I essentially do the same treatment to 3 different class data, so I do not want to have to write 3 times 400 lines of code when only 1 cast need to be changed.
If you have the following:
MyType myTabby = (myType)myDoberman;
what are you going to do with myTabby?
If you don't know the type of object you're getting,
how would you manipulate it's data or execute it's methods?
(e.g. Cat can do other things than Dog can do and has other Attributes
Both could do the action Walk() sinc they are both mammals but a dog would be able to do the action ClimbUpTree()
)
So if you just want to do basic things to this objects, you can cast it to their base-class (e.g. (Mammal)myObject; )
Maybe you can tell what you want to achieve with your program and speak more special about your classes and data and not only about mammals which I think isn't the subject of your program.
@Wildhorn
I would say you got a lot of possibilities.
1. You can do an interface ISomething and then make a helper which works with ISomething and inherit all those classes from ISomething.
2. Make an abstract class, inherit all those classes from abstract class. Which is not very different from interface but could help because you put all the code which is common in abstract class.
3. Add to constructor of those classes a parameter which will indicate the helper which should work with all those classes.
Dunno, I just think you should really know what you need to do.
Modularization is a little tough when you're starting out.
1. Make a list of all the things that you plan to do with all 3 of your classes. Eg maybe there's a toString() method, some sort of descriptor methods, like length or size or colour.
2. Write a class that has all of these common functions.
At this point I am not sure if you even need more than 1 class.
Are your three different classes all inherited from the same type?
I do this with UserControls for industrial scanners.
Class ScannerUI : UserControl
Class BrandAlpha: ScannerUI
Class BrandBravo: ScannerUI
In this way I have no problems doing a case exactly as you have described.
No they do not inherit from the same type, but they all have string variables that need to be treated the same way.
Anyway, I got a very "dirty" solution that I do not like much but that will do it for the moment. I made a function that receive the class object and with a switch case return an object of the good type and it works now, but it piss me off that I cant on the fly cast stuff when language like LUA allow such task (you dont even need to cast anything, LUA knows what you want).
A) Change it so they do all inherit from the same type
B) Write a custom cast for your class as recommended earlier
Maybe make a 4th 'utility' class that handles the strings? -
Object ob;
-
String str;
-
-
switch (TypeOf(ob)){
-
case TypeOf(Dog):
-
str = (Dog)ob.Tag;
-
break;
-
case TypeOf(Cat):
-
str = (Cat)ob.Tray;
-
break;
-
case TypeOf(Moose):
-
str = (Moose)ob.Antler;
-
break;
-
}
-
-
Utility.HandleString(str);
-
Actually, not sure if you can use switch on Types, but just an if-else if chain.
@Wildhorn
In .NET 4.0 you can do dynamic cast. So please see the specs for dynamic keyword.
Also please do notice that C# is a static/strongly-typed language and by adding DLR (Dynamic Language Runtime) it hasn't changed into a dynamic language.
To see why is C# statically typed please visit the link "Why is C# statically typed?"
If you have to cast then your design is probably wrong. From what I understand of your problem is that you want to store multiple classes in the same array and then cast them out to use them. Even if you get this to work you will probably run into run time problems due to improper casting. Like others have suggested you should define an interface if you want multiple classes to have the same behavior even if they don't derive from the same type.
Could you post sample LUA code that does what you want?
This is mostly for interest, but also to see if there is another way to do it.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: adrian |
last post by:
hi all
this is my first post to this group, so pls bear with me while i try
to make some sense.
i am trying to create a sproc which uses dynamic sql to target a
particuar table eg. '.' and...
|
by: MD |
last post by:
I am trying to create a dynamic SQL statement to create a view.
I have a stored procedure, which based on the parameters passed calls
different stored procedures. Each of this sub stored procedure...
|
by: Bane |
last post by:
Hi all
In the SP below im (trying to) do some dynamic sql. As you can see the table
to use is set as a variable and the 'exec' method used to run the
sqlstatements.
My problem is that the 'if...
|
by: raca |
last post by:
I am trying to create a generic SOA ServiceInvoker that will accept an
XML string that will be used to deserialize an object generated by
XSDObjectGen. The hierarchy goes like this:...
|
by: Gibby Koldenhof |
last post by:
Hiya,
Terrible subject but I haven't got a better term at the moment.
I've been building up my own library of functionality (all nice conforming
ISO C) for over 6 years and decided to adopt a...
|
by: Martin Hart - Memory Soft, S.L. |
last post by:
Hi all:
I still very new to the .NET world and don't know if what I am asking is due
to an over-imaginative imagination or the fact that I have read too many
fiction books!!
Let me show you a...
|
by: Philipp Schumann |
last post by:
Hi,
I have a need for "dynamic type casting": in other words, in a "MyConvert"
method I get passed an Object "value" and a Type "type" and the method
should attempt to convert value into type.
...
|
by: DaTurk |
last post by:
Hi,
I have three interfaces lets call them parent, child-A, and child-B. I
want to have one variable, that can be either child-A, or child-B at
run time. I just can't seem to figure out how to...
|
by: DaTurk |
last post by:
Hi,
This is a question brought about by a solution I came up with to
another question I had, which was "Dynamic object creation". So, I'm
curious if you can dynamically cast an object. If you...
|
by: Pacific Fox |
last post by:
Hi all,
I have a SQL statement that allows paging and dynamic sorting of the
columns, but what I can't figure out without making the SQL a dynamic
string and executing it, or duplicating the SQL...
|
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: 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...
|
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: 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,...
| |