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. How to cast to a type stored in a field

How to cast to a type stored in a field

Scheduled Pinned Locked Moved C#
questionhelptutorial
5 Posts 4 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.
  • R Offline
    R Offline
    redivider
    wrote on last edited by
    #1

    How do I use a field storing Type information?

    private Type _DataType;
    private Dictionary<int, object> _BlobData;

    ...
    _DataType cow = (_DataType)_BlobData[0];
    //the previous line shows compiler error
    //._DataType' is a 'field' but is used like a 'type'

    Is "Type" not the right type?

    C N T 3 Replies Last reply
    0
    • R redivider

      How do I use a field storing Type information?

      private Type _DataType;
      private Dictionary<int, object> _BlobData;

      ...
      _DataType cow = (_DataType)_BlobData[0];
      //the previous line shows compiler error
      //._DataType' is a 'field' but is used like a 'type'

      Is "Type" not the right type?

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

      Generics is how you do stuff like that.

      Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

      1 Reply Last reply
      0
      • R redivider

        How do I use a field storing Type information?

        private Type _DataType;
        private Dictionary<int, object> _BlobData;

        ...
        _DataType cow = (_DataType)_BlobData[0];
        //the previous line shows compiler error
        //._DataType' is a 'field' but is used like a 'type'

        Is "Type" not the right type?

        N Offline
        N Offline
        Nooner
        wrote on last edited by
        #3

        redivider, to get data out of the dictionary you have to give it the key. In a dictionary, the key is the first type whatever that is. So since you are using an int, then the integer value that you entered must exist in your dictionary, else it will be null. For a list, this is completely different from a dictionary, and you can just sellect the first field or [0]. int key = 39292; String testObj = "Hello I'm a string"; object value = (object)testObj; so in your example... _BlobData.add(key, value); To get out the value... you have to put in the correct key... String myOldDefinition = (String)_BlobData[key]; Console.Writeln("My old string = " + myOldDefinition); HTH, Marc

        Life is too short to program in Visual Basic.

        1 Reply Last reply
        0
        • R redivider

          How do I use a field storing Type information?

          private Type _DataType;
          private Dictionary<int, object> _BlobData;

          ...
          _DataType cow = (_DataType)_BlobData[0];
          //the previous line shows compiler error
          //._DataType' is a 'field' but is used like a 'type'

          Is "Type" not the right type?

          T Offline
          T Offline
          Thomas Weller 0
          wrote on last edited by
          #4

          redivider wrote:

          Is "Type" not the right type?

          It is, but you are casting to _DataType, which is a variable, not a data type. Seems that you are confusing the two concepts of data type and variable (i.e. an instance of a data type). Besides that, the Object.GetType() method is the way to get type information from an object, casting will generally not work. So your code snippet should read something like this:

          private System.Type _Cow;
          private Dictionary<int, object> _BlobData;
          ...
          _Cow = _BlobData[0].GetType();

          Regards Thomas

          www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
          Programmer - an organism that turns coffee into software.

          R 1 Reply Last reply
          0
          • T Thomas Weller 0

            redivider wrote:

            Is "Type" not the right type?

            It is, but you are casting to _DataType, which is a variable, not a data type. Seems that you are confusing the two concepts of data type and variable (i.e. an instance of a data type). Besides that, the Object.GetType() method is the way to get type information from an object, casting will generally not work. So your code snippet should read something like this:

            private System.Type _Cow;
            private Dictionary<int, object> _BlobData;
            ...
            _Cow = _BlobData[0].GetType();

            Regards Thomas

            www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
            Programmer - an organism that turns coffee into software.

            R Offline
            R Offline
            redivider
            wrote on last edited by
            #5

            When i have int i = 10; i = 5 * i; it knows i'm not trying to multiply 5 * i, but rather 5* the value stored in i. So i'm not trying to cast to a variable (_DataType), but rather the value stored in that variable (int, float, string)... How would generics come into play?

            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