C# Help please!
-
Hi all, I'm a new programmer and I've come across a problem that I'll need some help in. class MyObject1{...} class MyObject2{...} class MyObject3{...} class MyObject4{...} public void test(object a){ Type a = a.GetType(); //Now what?! } //Somewhere in main...I call test and pass it an instance of one of these objects... test(new MyObject1()); test(new MyObject4()); test(new MyObject3()); test(new MyObject2()); What I'd like to know is how to convert variable 'a' into whatever object was passed into test(); This is probably a silly question but I'm learning.... Thanks in advance. Humble.
-
Hi all, I'm a new programmer and I've come across a problem that I'll need some help in. class MyObject1{...} class MyObject2{...} class MyObject3{...} class MyObject4{...} public void test(object a){ Type a = a.GetType(); //Now what?! } //Somewhere in main...I call test and pass it an instance of one of these objects... test(new MyObject1()); test(new MyObject4()); test(new MyObject3()); test(new MyObject2()); What I'd like to know is how to convert variable 'a' into whatever object was passed into test(); This is probably a silly question but I'm learning.... Thanks in advance. Humble.
Try to write a more useful subject line, that says something about your question. We're in the C# forum, so mentioning that in the subject is quite superflous. Almost everyone that starts a thread here need help, so that doesn't do anything at all to distinguish your thread from others. To get a reference of a specific type, just cast the object reference:
if (a is MyObject1) {
MyObject1 a1 = (MyObject1)a;
// do something with a1
}or:
MyObject1 a1 = a as MyObject1;
if (a1 != null) {
// do something with a1
}As the reference has to be the exact type, there is no solution that handles any type. You have to write code to handle each type separately. If the classes are supposed to work in a similar way, you might want to create a base class or an interface that defines some common methods, and make all the classes inherit the base class or interface. That way you can use a reference to the base class or the interface instead of a reference to the common base class
Object
.--- single minded; short sighted; long gone;
-
Try to write a more useful subject line, that says something about your question. We're in the C# forum, so mentioning that in the subject is quite superflous. Almost everyone that starts a thread here need help, so that doesn't do anything at all to distinguish your thread from others. To get a reference of a specific type, just cast the object reference:
if (a is MyObject1) {
MyObject1 a1 = (MyObject1)a;
// do something with a1
}or:
MyObject1 a1 = a as MyObject1;
if (a1 != null) {
// do something with a1
}As the reference has to be the exact type, there is no solution that handles any type. You have to write code to handle each type separately. If the classes are supposed to work in a similar way, you might want to create a base class or an interface that defines some common methods, and make all the classes inherit the base class or interface. That way you can use a reference to the base class or the interface instead of a reference to the common base class
Object
.--- single minded; short sighted; long gone;
Just to add - the difference between (MyObject)a and a as MyObject is that the first throws an exception and the second returns null if the cast is invalid.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Just to add - the difference between (MyObject)a and a as MyObject is that the first throws an exception and the second returns null if the cast is invalid.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
I tried to use 'as' today and the compiler slapped me in the face (ie. it told me i was wrong) It was just like this: class something { //some private variables and whatnot } static void Main(string[] args) { object test = new something(); something newSomething = test as something; } and shwam, compiler throws som error.
My current favourite word is: Waffle Cheese is still good though.
-
I tried to use 'as' today and the compiler slapped me in the face (ie. it told me i was wrong) It was just like this: class something { //some private variables and whatnot } static void Main(string[] args) { object test = new something(); something newSomething = test as something; } and shwam, compiler throws som error.
My current favourite word is: Waffle Cheese is still good though.
what was the error ?
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
what was the error ?
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
I forget, i was using some microsoft visual express thingymajig, rather than my preferred SharpDevelop. I either spelled something wrong and couldn't see it, or the compiler was having a bad day. Its working fine now (in #Develop)
My current favourite word is: Waffle Cheese is still good though.
-
I tried to use 'as' today and the compiler slapped me in the face (ie. it told me i was wrong) It was just like this: class something { //some private variables and whatnot } static void Main(string[] args) { object test = new something(); something newSomething = test as something; } and shwam, compiler throws som error.
My current favourite word is: Waffle Cheese is still good though.