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. NameOf method?

NameOf method?

Scheduled Pinned Locked Moved C#
helpquestionannouncement
8 Posts 7 Posters 1 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
    Skippums
    wrote on last edited by
    #1

    I have had to, on a number of occasions, do something like the following:

    switch (a.GetType().FullName) {
    case "System.Int32":
    ...
    break;
    case "MyNamespace.SubNamespace.MyClass":
    ...
    break;
    ...
    }

    The problem is that if I change class name or the namespace of MyClass, the above breaks without generating a compile-time error. The way that I would like to perform the above is by using the type itself, and have "case typeof(MyClass):", but that DOES generate a compile-time error. I am wondering, then, if there is some method like typeof that gets a constant string representing the class name at compile time, so I could change the above code to...

    switch (a.GetType().FullName) {
    case nameof(int):
    ...
    break;
    case nameof(MyClass):
    ...
    break;
    ...
    }

    This way, I would get compile-time errors instead of run-time errors if I changed the classname but didn't update the above code. Thanks,

    Sounds like somebody's got a case of the Mondays -Jeff

    E T N L R 5 Replies Last reply
    0
    • S Skippums

      I have had to, on a number of occasions, do something like the following:

      switch (a.GetType().FullName) {
      case "System.Int32":
      ...
      break;
      case "MyNamespace.SubNamespace.MyClass":
      ...
      break;
      ...
      }

      The problem is that if I change class name or the namespace of MyClass, the above breaks without generating a compile-time error. The way that I would like to perform the above is by using the type itself, and have "case typeof(MyClass):", but that DOES generate a compile-time error. I am wondering, then, if there is some method like typeof that gets a constant string representing the class name at compile time, so I could change the above code to...

      switch (a.GetType().FullName) {
      case nameof(int):
      ...
      break;
      case nameof(MyClass):
      ...
      break;
      ...
      }

      This way, I would get compile-time errors instead of run-time errors if I changed the classname but didn't update the above code. Thanks,

      Sounds like somebody's got a case of the Mondays -Jeff

      E Offline
      E Offline
      Eslam Afifi
      wrote on last edited by
      #2

      switch cases take constants so you can't call a function in a switch case, so you'll use if statements if (a.GetType() == typeof(Int32)) ... else if (a.GetType() == typeof(MyClass)) ...

      Eslam Afifi

      R E 2 Replies Last reply
      0
      • S Skippums

        I have had to, on a number of occasions, do something like the following:

        switch (a.GetType().FullName) {
        case "System.Int32":
        ...
        break;
        case "MyNamespace.SubNamespace.MyClass":
        ...
        break;
        ...
        }

        The problem is that if I change class name or the namespace of MyClass, the above breaks without generating a compile-time error. The way that I would like to perform the above is by using the type itself, and have "case typeof(MyClass):", but that DOES generate a compile-time error. I am wondering, then, if there is some method like typeof that gets a constant string representing the class name at compile time, so I could change the above code to...

        switch (a.GetType().FullName) {
        case nameof(int):
        ...
        break;
        case nameof(MyClass):
        ...
        break;
        ...
        }

        This way, I would get compile-time errors instead of run-time errors if I changed the classname but didn't update the above code. Thanks,

        Sounds like somebody's got a case of the Mondays -Jeff

        T Offline
        T Offline
        TJoe
        wrote on last edited by
        #3

        1. You can use if-else/if statements, instead of a switch 2. Or you may be able to use:

        case typeof(MyClass).FullName:

        Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

        1 Reply Last reply
        0
        • S Skippums

          I have had to, on a number of occasions, do something like the following:

          switch (a.GetType().FullName) {
          case "System.Int32":
          ...
          break;
          case "MyNamespace.SubNamespace.MyClass":
          ...
          break;
          ...
          }

          The problem is that if I change class name or the namespace of MyClass, the above breaks without generating a compile-time error. The way that I would like to perform the above is by using the type itself, and have "case typeof(MyClass):", but that DOES generate a compile-time error. I am wondering, then, if there is some method like typeof that gets a constant string representing the class name at compile time, so I could change the above code to...

          switch (a.GetType().FullName) {
          case nameof(int):
          ...
          break;
          case nameof(MyClass):
          ...
          break;
          ...
          }

          This way, I would get compile-time errors instead of run-time errors if I changed the classname but didn't update the above code. Thanks,

          Sounds like somebody's got a case of the Mondays -Jeff

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          Once you start relying on the fully qualified name of your class as a string value in another class or application they are pretty much bound to one another.


          only two letters away from being an asset

          1 Reply Last reply
          0
          • S Skippums

            I have had to, on a number of occasions, do something like the following:

            switch (a.GetType().FullName) {
            case "System.Int32":
            ...
            break;
            case "MyNamespace.SubNamespace.MyClass":
            ...
            break;
            ...
            }

            The problem is that if I change class name or the namespace of MyClass, the above breaks without generating a compile-time error. The way that I would like to perform the above is by using the type itself, and have "case typeof(MyClass):", but that DOES generate a compile-time error. I am wondering, then, if there is some method like typeof that gets a constant string representing the class name at compile time, so I could change the above code to...

            switch (a.GetType().FullName) {
            case nameof(int):
            ...
            break;
            case nameof(MyClass):
            ...
            break;
            ...
            }

            This way, I would get compile-time errors instead of run-time errors if I changed the classname but didn't update the above code. Thanks,

            Sounds like somebody's got a case of the Mondays -Jeff

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi Jeff, you should try not to rely on type names, since doing so will fail for derived types. The way to go most often is using the "is" or "as" keywords, as in:

            if (obj is Font) {
                ... do someting fonty (probably will need a cast)
            } else if (obj is Panel) {
                ... deal with a Panel (probably will need a cast)
            } etc.
            

            or

            Font fnt=obj as Font;  // generates null if obj isn't a Font
            if (fnt!=null) {
               ... do someting fonty to fnt (no extra cast required)
            }
            Panel pan=obj as Panel;
            if (pan!=null) {
                ...
            }
            

            The above will also match mySpecialPanel and the like, something typename-dependent code would typically not do. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


            1 Reply Last reply
            0
            • E Eslam Afifi

              switch cases take constants so you can't call a function in a switch case, so you'll use if statements if (a.GetType() == typeof(Int32)) ... else if (a.GetType() == typeof(MyClass)) ...

              Eslam Afifi

              R Offline
              R Offline
              Ravenet
              wrote on last edited by
              #6

              Hi Guys this is correct way when we are compare own object or own defiened class object with runtime. typeof keyword is very very useful. Thanks

              Cheers RRave MCTS,MCPD

              1 Reply Last reply
              0
              • S Skippums

                I have had to, on a number of occasions, do something like the following:

                switch (a.GetType().FullName) {
                case "System.Int32":
                ...
                break;
                case "MyNamespace.SubNamespace.MyClass":
                ...
                break;
                ...
                }

                The problem is that if I change class name or the namespace of MyClass, the above breaks without generating a compile-time error. The way that I would like to perform the above is by using the type itself, and have "case typeof(MyClass):", but that DOES generate a compile-time error. I am wondering, then, if there is some method like typeof that gets a constant string representing the class name at compile time, so I could change the above code to...

                switch (a.GetType().FullName) {
                case nameof(int):
                ...
                break;
                case nameof(MyClass):
                ...
                break;
                ...
                }

                This way, I would get compile-time errors instead of run-time errors if I changed the classname but didn't update the above code. Thanks,

                Sounds like somebody's got a case of the Mondays -Jeff

                R Offline
                R Offline
                Roger Alsing 0
                wrote on last edited by
                #7

                You could also use some lookup pattern.

                setup type -> action association
                Dictionary<type, somedelegate> myLookup = new .....
                myLookup.Add(typeof(string) , () => MessageBox.Show("Its a string!"));
                myLookup.Add(typeof(int) , () => Console.WriteLine("ooh an int.."));

                and then replace your switch/if with: ------

                SomeDelegate action;
                if (myLookup.TryGetValue( a.GetType() , out action ) )
                {
                //there is a delegate associated with this type:
                action(); //execute the action
                }

                ---- So you replace your switch/if completely with the lines above... But it depends on what you actually want to accomplish. See the post above mine regarding subclasses.

                http://www.puzzleframework.com

                1 Reply Last reply
                0
                • E Eslam Afifi

                  switch cases take constants so you can't call a function in a switch case, so you'll use if statements if (a.GetType() == typeof(Int32)) ... else if (a.GetType() == typeof(MyClass)) ...

                  Eslam Afifi

                  E Offline
                  E Offline
                  Eslam Afifi
                  wrote on last edited by
                  #8

                  I just want to add that this code provide direct type comparison. If you want to compare the inheritance hierarchy use the is keyword as Luc Pattyn mentioned here.

                  Eslam Afifi

                  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