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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Explicit casting with Type variable

Explicit casting with Type variable

Scheduled Pinned Locked Moved C#
question
9 Posts 4 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.
  • K Offline
    K Offline
    Kaikus
    wrote on last edited by
    #1

    Is it possible to make a explicit casting using a variable? I'm passig a boxed object, and it's Type, as arguments. Inside the method I would like to unbox and use the content private void myMethod ( Object boxedObject, Type dataType) { .... ... = (dataType)boxedObject; .... } I know it doesn't compile this way. By the moment i'm using this kind of code: switch (dataType.ToString()) { case "int": ... = (int)boxedObject; case "string" ... = (string)boxedObject; ... } But... is there a way to make something similar to "(dataType)boxedObjec)" that doesn't need using 'if' or 'case' for every dataType? Thanks!

    OriginalGriffO P 2 Replies Last reply
    0
    • K Kaikus

      Is it possible to make a explicit casting using a variable? I'm passig a boxed object, and it's Type, as arguments. Inside the method I would like to unbox and use the content private void myMethod ( Object boxedObject, Type dataType) { .... ... = (dataType)boxedObject; .... } I know it doesn't compile this way. By the moment i'm using this kind of code: switch (dataType.ToString()) { case "int": ... = (int)boxedObject; case "string" ... = (string)boxedObject; ... } But... is there a way to make something similar to "(dataType)boxedObjec)" that doesn't need using 'if' or 'case' for every dataType? Thanks!

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      You can probably do it via reflection, but what are you going to do with it then? If (for example) "as" worked that way, so you could do " = boxedObject as dataType;" what are you going to assign it to? Other than another object? You can't pass it to another method, because any type you haven't covered will either no compile or will throw an exception. You can't use it's properties, as you can't be sure it has those properties (other than ToString and the other really basic ones). What are you trying to do, that you need this?

      All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      K 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        You can probably do it via reflection, but what are you going to do with it then? If (for example) "as" worked that way, so you could do " = boxedObject as dataType;" what are you going to assign it to? Other than another object? You can't pass it to another method, because any type you haven't covered will either no compile or will throw an exception. You can't use it's properties, as you can't be sure it has those properties (other than ToString and the other really basic ones). What are you trying to do, that you need this?

        All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!

        K Offline
        K Offline
        Kaikus
        wrote on last edited by
        #3

        'as' works the same way than normal casting... Doesn't work... :( What I want is compare the given value with data values from a dataset. So I first get the needed column DataType, and then I compare whith the properly unboxed value. The basic code would be: public int SeekRow (string colName, object value) { Type dataType = myDataSet.MyTable.Columns[colName].DataType; for (row=0; ...) { if (myDataSet.myTable[row][ColName]==(dataType)value) { return row; } } return -1; } If I can't, I must use a lot of untidy 'if' ot 'case'...

        OriginalGriffO G 2 Replies Last reply
        0
        • K Kaikus

          'as' works the same way than normal casting... Doesn't work... :( What I want is compare the given value with data values from a dataset. So I first get the needed column DataType, and then I compare whith the properly unboxed value. The basic code would be: public int SeekRow (string colName, object value) { Type dataType = myDataSet.MyTable.Columns[colName].DataType; for (row=0; ...) { if (myDataSet.myTable[row][ColName]==(dataType)value) { return row; } } return -1; } If I can't, I must use a lot of untidy 'if' ot 'case'...

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          I know "as" doesn't work - that's why I said 'If (for example) "as" worked that way' rather than 'Use "as" - it will do what you want' I can't help thinking you are overdoing this a little, but... Do you really need to know what the datatype is? Since your are returning only the row number rather than the value, either compare it directly

          myDataSet.myTable[row][ColName] == value

          or use ToString and compare that way.

          myDataSet.myTable[row][ColName].ToString() == value.ToString()

          Otherwise you have to be sure that all the objects you may pass the routine implement the == operator... I would still think about why I was doing this in this (rather odd) way in the first place.

          All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          K 1 Reply Last reply
          0
          • K Kaikus

            'as' works the same way than normal casting... Doesn't work... :( What I want is compare the given value with data values from a dataset. So I first get the needed column DataType, and then I compare whith the properly unboxed value. The basic code would be: public int SeekRow (string colName, object value) { Type dataType = myDataSet.MyTable.Columns[colName].DataType; for (row=0; ...) { if (myDataSet.myTable[row][ColName]==(dataType)value) { return row; } } return -1; } If I can't, I must use a lot of untidy 'if' ot 'case'...

            G Offline
            G Offline
            Gideon Engelberth
            wrote on last edited by
            #5

            Assuming that you are not actually passing objects in, you might be able to use generics here:

            public int SeekRow<T>(string colName, T value)
            {
            for (row = 0; ...)
            {
            if ((T)myDataSet.myTable[row][ColName] == value)
            {
            return row;
            }
            }
            return -1;
            }

            int row1 = SeekRow("Age", 25); //calls SeekRow<int>
            int row2 = SeekRow("Name", "John Doe"); //calls SeekRow<string>

            K 1 Reply Last reply
            0
            • K Kaikus

              Is it possible to make a explicit casting using a variable? I'm passig a boxed object, and it's Type, as arguments. Inside the method I would like to unbox and use the content private void myMethod ( Object boxedObject, Type dataType) { .... ... = (dataType)boxedObject; .... } I know it doesn't compile this way. By the moment i'm using this kind of code: switch (dataType.ToString()) { case "int": ... = (int)boxedObject; case "string" ... = (string)boxedObject; ... } But... is there a way to make something similar to "(dataType)boxedObjec)" that doesn't need using 'if' or 'case' for every dataType? Thanks!

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

              System.Convert.ChangeType ? The only useful member of Convert.

              1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                I know "as" doesn't work - that's why I said 'If (for example) "as" worked that way' rather than 'Use "as" - it will do what you want' I can't help thinking you are overdoing this a little, but... Do you really need to know what the datatype is? Since your are returning only the row number rather than the value, either compare it directly

                myDataSet.myTable[row][ColName] == value

                or use ToString and compare that way.

                myDataSet.myTable[row][ColName].ToString() == value.ToString()

                Otherwise you have to be sure that all the objects you may pass the routine implement the == operator... I would still think about why I was doing this in this (rather odd) way in the first place.

                All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!

                K Offline
                K Offline
                Kaikus
                wrote on last edited by
                #7

                This is the temporal solution I have implemented, converting ToString. As you say, otherwise I should be sure to implement "==" operator for every object. Thanks for your reply!

                1 Reply Last reply
                0
                • G Gideon Engelberth

                  Assuming that you are not actually passing objects in, you might be able to use generics here:

                  public int SeekRow<T>(string colName, T value)
                  {
                  for (row = 0; ...)
                  {
                  if ((T)myDataSet.myTable[row][ColName] == value)
                  {
                  return row;
                  }
                  }
                  return -1;
                  }

                  int row1 = SeekRow("Age", 25); //calls SeekRow<int>
                  int row2 = SeekRow("Name", "John Doe"); //calls SeekRow<string>

                  K Offline
                  K Offline
                  Kaikus
                  wrote on last edited by
                  #8

                  Nice! I didn't know that. Just becouse I am returning to c# programmin after... 6 years without programming at all!! And I have only installed .NET Framework 1, do I wasn't aware of Generics. I will try! Thanks!

                  G 1 Reply Last reply
                  0
                  • K Kaikus

                    Nice! I didn't know that. Just becouse I am returning to c# programmin after... 6 years without programming at all!! And I have only installed .NET Framework 1, do I wasn't aware of Generics. I will try! Thanks!

                    G Offline
                    G Offline
                    Gideon Engelberth
                    wrote on last edited by
                    #9

                    If you want to use generics, you will need at least .NET 2.0 (might as well just get 3.5 since it's the newest and contains 2.0)

                    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