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. checking an object is compatible with another object

checking an object is compatible with another object

Scheduled Pinned Locked Moved C#
question
13 Posts 5 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.
  • C Christian Graus

    typeof takes the type, I thought, as in typeof(int). I think you need hello.GetType()

    Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

    M Offline
    M Offline
    Mike Bentzen
    wrote on last edited by
    #4

    This isn't exactly doing what i thought it was doing sorry. variable.GetType() == expectedType is checking whether Object is equal to Type blah. Its always returning false. I just want it to return if it is compatible with casting it to another object, not if it is the same type.

    C 1 Reply Last reply
    0
    • M Mike Bentzen

      This isn't exactly doing what i thought it was doing sorry. variable.GetType() == expectedType is checking whether Object is equal to Type blah. Its always returning false. I just want it to return if it is compatible with casting it to another object, not if it is the same type.

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

      OK, perhaps generics will help. Because what you need is access to the type to call 'is' as you were trying to do. Passing a Type doesn't sound like it worked, but if the type is specified and your generic method says something like return (myvar is T);

      Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      M 1 Reply Last reply
      0
      • C Christian Graus

        OK, perhaps generics will help. Because what you need is access to the type to call 'is' as you were trying to do. Passing a Type doesn't sound like it worked, but if the type is specified and your generic method says something like return (myvar is T);

        Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        M Offline
        M Offline
        Mike Bentzen
        wrote on last edited by
        #6

        Christian Graus wrote:

        return (myvar is T);

        where T is my parameter of type? bool CheckConfigVariable(object variable, Type expectedType) { return (variable is expectedType); } i get the error: The type or namespace name 'expectedType' could not be found (are you missing a using directive or an assembly reference?) (CS0246)

        C 1 Reply Last reply
        0
        • M Mike Bentzen

          Christian Graus wrote:

          return (myvar is T);

          where T is my parameter of type? bool CheckConfigVariable(object variable, Type expectedType) { return (variable is expectedType); } i get the error: The type or namespace name 'expectedType' could not be found (are you missing a using directive or an assembly reference?) (CS0246)

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

          Yeah, but you write a generic method instead. static bool CheckType<T>(object o) { return (o is T); } Then you call it like this: DateTime dt = DateTime.Now; bool b = CheckType<int>(dt);

          Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

          M 1 Reply Last reply
          0
          • C Christian Graus

            Yeah, but you write a generic method instead. static bool CheckType<T>(object o) { return (o is T); } Then you call it like this: DateTime dt = DateTime.Now; bool b = CheckType<int>(dt);

            Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

            M Offline
            M Offline
            Mike Bentzen
            wrote on last edited by
            #8

            wow, I didn't know how that worked. Now I'll have to go and do some more indepth reading on Generics. Just another question on comparing types. How do enums fit into the picture? Is enum a type? Can I compare a string to an enum (using the method above (using is keyword)), or one of the values in the enumto see if it can be converted?

            C 1 Reply Last reply
            0
            • M Mike Bentzen

              wow, I didn't know how that worked. Now I'll have to go and do some more indepth reading on Generics. Just another question on comparing types. How do enums fit into the picture? Is enum a type? Can I compare a string to an enum (using the method above (using is keyword)), or one of the values in the enumto see if it can be converted?

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

              You use Enum.Parse to convert a string to an enum. An enum is a type.

              Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

              M 1 Reply Last reply
              0
              • C Christian Graus

                You use Enum.Parse to convert a string to an enum. An enum is a type.

                Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                M Offline
                M Offline
                Mike Bentzen
                wrote on last edited by
                #10

                Thank you so much for your help, it is greatly appreciated. :)

                C 1 Reply Last reply
                0
                • M Mike Bentzen

                  Thank you so much for your help, it is greatly appreciated. :)

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

                  Glad to help

                  Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                  1 Reply Last reply
                  0
                  • M Mike Bentzen

                    i am trying to write a method that checks whether an object can be cast to a specific type: that is, something like this:

                    bool CheckConfigVariable(object variable, Type expectedType) {
                    if (variable is expectedType) {
                    return true;
                    }
                    return false;
                    }

                    but that doesn't seem to work. I've also tried:

                    object hello;
                    hello = (object)variable;
                    if (typeof(hello) == Type.GetTypeCode(typething)) {
                    return true;
                    }
                    return false;
                    }

                    which sort of works, but it can't find the hello variable in the typeof function. How would I go about checking whether the object is a certain type from what is provided in the parameters of the called method? Mike

                    L Offline
                    L Offline
                    leppie
                    wrote on last edited by
                    #12

                    Type.IsInstanceOfType()

                    xacc.ide - now with TabsToSpaces support
                    IronScheme - 1.0 alpha 4a out now (29 May 2008)

                    1 Reply Last reply
                    0
                    • M Mike Bentzen

                      i am trying to write a method that checks whether an object can be cast to a specific type: that is, something like this:

                      bool CheckConfigVariable(object variable, Type expectedType) {
                      if (variable is expectedType) {
                      return true;
                      }
                      return false;
                      }

                      but that doesn't seem to work. I've also tried:

                      object hello;
                      hello = (object)variable;
                      if (typeof(hello) == Type.GetTypeCode(typething)) {
                      return true;
                      }
                      return false;
                      }

                      which sort of works, but it can't find the hello variable in the typeof function. How would I go about checking whether the object is a certain type from what is provided in the parameters of the called method? Mike

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #13

                      private static bool
                      F
                      (
                      object o
                      ,
                      System.Type t
                      )
                      {
                      if ( o == null )
                      {
                      throw ( new System.ArgumentNullException() ) ;
                      }

                      return ( t.IsAssignableFrom ( o.GetType() ) ) ;
                      

                      }

                      And may I humbly suggest my TypeTransmogrifier[^] ?

                      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