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. typedef in c#

typedef in c#

Scheduled Pinned Locked Moved C#
questioncsharp
7 Posts 6 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.
  • S Offline
    S Offline
    Sasuko
    wrote on last edited by
    #1

    i need to understand what kind of object is just one.. i have myOwn it could be of Car or Bike. How can i understand it? i thought about: if( typedef(myOwn) == Car) but typedef doesn't exist in c# 1.1 I don't want to use try catch, tnx

    T J 2 Replies Last reply
    0
    • S Sasuko

      i need to understand what kind of object is just one.. i have myOwn it could be of Car or Bike. How can i understand it? i thought about: if( typedef(myOwn) == Car) but typedef doesn't exist in c# 1.1 I don't want to use try catch, tnx

      T Offline
      T Offline
      ThaddParker
      wrote on last edited by
      #2

      your best bet is to use typeof() if(typeof(myOwn) == Car) DoSomethingWithCar(myOwn as Car) else DoSomethingWithBike(myOwn as Bike) The reason why a poor man will always be poor and the rich man will be rich, is because the poor always maximizes his expenditures and the rich man always maximizes his potential. --T.Parker

      1 Reply Last reply
      0
      • S Sasuko

        i need to understand what kind of object is just one.. i have myOwn it could be of Car or Bike. How can i understand it? i thought about: if( typedef(myOwn) == Car) but typedef doesn't exist in c# 1.1 I don't want to use try catch, tnx

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #3

        if(myOwn is Car) { } or if(myOwn.GetType() == typeof(Car)) { } They are the same in essence.

        C 1 Reply Last reply
        0
        • J J4amieC

          if(myOwn is Car) { } or if(myOwn.GetType() == typeof(Car)) { } They are the same in essence.

          C Offline
          C Offline
          Curtis Schlak
          wrote on last edited by
          #4

          Just to add to J4amieC's suggestion, if you need to actually use the methods of your specific class, you could use the as keyword to reduce the number of casts that you'll use. For example:

          Car c = someObject as Car;
          if( c != null )
          {
          // Do Car-specific stuff in here.
          }

          "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

          M 1 Reply Last reply
          0
          • C Curtis Schlak

            Just to add to J4amieC's suggestion, if you need to actually use the methods of your specific class, you could use the as keyword to reduce the number of casts that you'll use. For example:

            Car c = someObject as Car;
            if( c != null )
            {
            // Do Car-specific stuff in here.
            }

            "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

            M Offline
            M Offline
            mav northwind
            wrote on last edited by
            #5

            There's a small difference between using is and as. If you use as and then check for null you don't know if someObject actually is a Bike or if it's a Car with value null. With is you can really test for the correct type. Usually this won't matter, but it's a different meaning nevertheless... Regards, mav

            D C 2 Replies Last reply
            0
            • M mav northwind

              There's a small difference between using is and as. If you use as and then check for null you don't know if someObject actually is a Bike or if it's a Car with value null. With is you can really test for the correct type. Usually this won't matter, but it's a different meaning nevertheless... Regards, mav

              D Offline
              D Offline
              dnewmon
              wrote on last edited by
              #6

              Well the idea is: Car car = someObj as Car; Boat boat = someObj as Boat; Plane plane = someObj as Plane; if (car != null) { } else if (boat != null) { } else if (plane != null) { } The assumption is that your testing classes on the same level in the class hierarchy. If your not testing classes on the same level, then the difference between is and as matters. But you could also write your if-else statement so that it tests the more specific class types to the more broad class types.... lol Anyways, David

              1 Reply Last reply
              0
              • M mav northwind

                There's a small difference between using is and as. If you use as and then check for null you don't know if someObject actually is a Bike or if it's a Car with value null. With is you can really test for the correct type. Usually this won't matter, but it's a different meaning nevertheless... Regards, mav

                C Offline
                C Offline
                Curtis Schlak
                wrote on last edited by
                #7

                I agree with you about most of that. If I use typeof on a null value, then I still not get what I need in terms of runtime type information. Here, a use of as prevents an extra cast later on. For example, in the following code, we pass null values and with both is and as, we cannot retrieve runtime type information:

                public class Foo {}
                public class Goo {}
                public class Test
                {
                [STAThread]
                static void Main( string[] args )
                {
                Foo f = null;
                Goo g = null;
                Foo ff = new Foo();
                Goo gg = new Goo();
                Console.WriteLine( Form1.TestMeAsFoo( f ) ); // False
                Console.WriteLine( Form1.TestMeAsFoo( g ) ); // False
                Console.WriteLine( Form1.TestMeAsFoo( ff ) ); // True
                Console.WriteLine( Form1.TestMeAsFoo( gg ) ); // False
                Console.WriteLine( Form1.TestMeIsFoo( f ) ); // False
                Console.WriteLine( Form1.TestMeIsFoo( g ) ); // False
                Console.WriteLine( Form1.TestMeIsFoo( ff ) ); // True
                Console.WriteLine( Form1.TestMeIsFoo( gg ) ); // False
                }
                public static bool TestMeIsFoo( object o )
                {
                return ( o is Foo );
                }
                public static bool TestMeAsFoo( object o )
                {
                return ( o as Foo ) != null;
                }
                }

                "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty -- modified at 15:24 Monday 5th December, 2005

                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