Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Help please!

C# Help please!

Scheduled Pinned Locked Moved C#
helpquestioncsharptutoriallearning
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    humblepgmr
    wrote on last edited by
    #1

    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.

    G 1 Reply Last reply
    0
    • H humblepgmr

      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.

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      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;

      C 1 Reply Last reply
      0
      • G Guffa

        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;

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        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 )

        A 1 Reply Last reply
        0
        • C Christian Graus

          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 )

          A Offline
          A Offline
          Anthony Mushrow
          wrote on last edited by
          #4

          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.

          C G 2 Replies Last reply
          0
          • A Anthony Mushrow

            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.

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            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 )

            A 1 Reply Last reply
            0
            • C Christian Graus

              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 )

              A Offline
              A Offline
              Anthony Mushrow
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • A Anthony Mushrow

                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.

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                That code compiles just fine. What does the code look like that you are actually using, and what error message do you get?

                --- single minded; short sighted; long gone;

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups