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. type object

type object

Scheduled Pinned Locked Moved C#
tutorial
3 Posts 2 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.
  • T Offline
    T Offline
    tek 2009
    wrote on last edited by
    #1

    hello everyone I want to know how to specify the type of a variable declared as Object better: I have a variable type object, and I want to test whether this object is string it will make me so and so instrution and if this object and int, and so on. thank you Here is a snippet incomplet:

    public static string Format(string format, object val,int length)
    {
    LineDefinition LD;
    string var;
    if (val.GetType() )

            var = string.Format(LD.Format , val).PadRight (LD .Length ,' ');
            var = string.Format(LD.Format, val).PadLeft (LD.Length, '0');
            return var;
    
    P 1 Reply Last reply
    0
    • T tek 2009

      hello everyone I want to know how to specify the type of a variable declared as Object better: I have a variable type object, and I want to test whether this object is string it will make me so and so instrution and if this object and int, and so on. thank you Here is a snippet incomplet:

      public static string Format(string format, object val,int length)
      {
      LineDefinition LD;
      string var;
      if (val.GetType() )

              var = string.Format(LD.Format , val).PadRight (LD .Length ,' ');
              var = string.Format(LD.Format, val).PadLeft (LD.Length, '0');
              return var;
      
      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      So you have a type that is an object and you want to determine whether or not it can be cast to a particular type. Is that what you are asking? If so (and if it is possible that the value comes in looking like a string), you can use the TryParse method on a particular type to determine whether or not it is of that type. You need to be careful with this approach because you need to carefully order the sequence of tests; this means that if you have the following:

      object o = 10;

      then o could be a short, an int or a long. If the variable looks like this example, then you can use the is operator to determine the type, e.g.

      if (o is int)
      {
      }

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      T 1 Reply Last reply
      0
      • P Pete OHanlon

        So you have a type that is an object and you want to determine whether or not it can be cast to a particular type. Is that what you are asking? If so (and if it is possible that the value comes in looking like a string), you can use the TryParse method on a particular type to determine whether or not it is of that type. You need to be careful with this approach because you need to carefully order the sequence of tests; this means that if you have the following:

        object o = 10;

        then o could be a short, an int or a long. If the variable looks like this example, then you can use the is operator to determine the type, e.g.

        if (o is int)
        {
        }

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        T Offline
        T Offline
        tek 2009
        wrote on last edited by
        #3

        in my case, I will use the second example, thank you very much for your help, I really need it :)

        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