HOWTO: cast data-type to another, which is only known at runtime

Hi there,

I'm writing my first bigger program with VS2005 (C#) and
CF 2.0 (means I'm a newbee to .NET!).

I have a problem which I can describe
in simple words like so:

I wish to convert the Returnvalue of the
System.Data.DataRow Indexer (e.g. object outValue = row[index])
(which is generic System.Object) to the specific Data-Type of
the DataColumn at RUNTIME!

Is this possible and how can I acomplish that

Any help is VERY appreciated!

Thanks in advance and greetings

Reiner



Answer this question

HOWTO: cast data-type to another, which is only known at runtime

  • Aiwa

    Then don’t use code generation tool for stored procedure B, code it manually using DSA.



  • marco.ragogna

    The answer is code generation tool. The code generation tool creates the type dataset by scanning the stored procedure.

    when I run tool on storedprocA, it creates DSA and for storedProcB, DSB is generated but both are same as I return all the attributes of the table.


  • Arshvinder Bhatia

    Well, I am not sure when these super methods will be available but I have similar issue:

    I am also using typed dataset and dataset are generated with code generation tool :-). So the tool generates two dataset, which are really same as it's returning all the columns of the table.

    Now I want to typecast one to another so I can use it in my class after calling the procedure.

    I tried to typecast but getting compile time error.

    Code:

    DSA DSCommon;

    DSCommon = ProcA(); //uses DSA

    DSCommon = ProcB(); //uses DSB

    also

    DSCommon = (DSA)ProcB(); //uses DSB

    Anybody

    Thanks,


  • Bharti Reddy

    Why would you need that I mean, if you intended to call some method on that object, you know the type at compile time. If you intended to assign it to variable of specific type, you also know the type at compile type. What is your scenario



  • benbino

    Ilya,

    I try to write some kind of a "generic" Method that reads from a DataSet. This Method should be used with several tables. I want to avoid writing code like this:

    foreach(DataColumn colData in rowResult.Columns)

    {

    if (colData.DataType == typeof(System.Int32))

    {

    recResult.SetFieldValue(nIndex, colData.DataType, (int)rowResult[nIndex++]);

    }

    else if ...

    }

    and so on. My Intention is to write a "SetFieldValue()"-Method that detect the data-type of the column at Runtime and cast the value of the column to the appropriate type (at Runtime).

    regards

    Reiner


  • ducati1212

    Well, I know that and I can update template to use same name but looking for some out of box solution.
  • Prze

    Your code makes sense to me and that's the only way to go if you want to do something particular for particular types and that type would only be known at runtime:

    if (somethingType = typeof(int)) {

    // It's int, cast it to int and do whatever needs to be done for int

    int integer = (int)something;

    }...

    Let's say there's hypothetical function SuperCast(Object whatever, Type toType). How would you use it

    If you think about it would become obvious this function is indeed hypothetical as return type is unknown so it could only be declared as object. Since it's returning objects it could not really cast anything, you already have object, right Should such function be possible we’ll just cast objects to specific types right in the dataset so you won’t have to.

    There's another function which looks just like our impossible SuperCast, Convert.ChangeType() which converts (or at least tries to) whatever to specified type. Again, it returns objects, so you’d need to cast it to particular type anyway.



  • NikiR

    Please clarify why exactly you have two identical typed DataSets and if they are indeed identical why you can’t eliminate one if them.

    In any case you can do this:

    DataSet DSCommon; // Untyped DataSet – can accept any typed or untyped DataSet.

    DSCommon = ProcA(); //uses DSA

    DSCommon = ProcB(); //uses DSB



  • HOWTO: cast data-type to another, which is only known at runtime