Casting from object

This is a discussion on Casting from object within the C# Programming forums, part of the General Programming Boards category; Hallo, Is it possible to cast from an object to an other type where the type is unknown at compile ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Casting from object

    Hallo,

    Is it possible to cast from an object to an other type where the type is unknown at compile time?

    Something like this
    int orgValue = 10;
    object obj = orgValue;
    int final = (obj.GetType())obj;

    If it is possible it would save me from typing up a very long and ugly if-statement.

    Edit:
    The reason for wanting to do this is that my api has a function that can take a lot of different variables as input. I want to store the data I want to pass in, but in order to pass them to this function I need to convert the data back to its original data type(its all int, float, double, bool, string)

    The function:
    void ApiFunction(int input)
    void ApiFunction(float input)
    ...

    What I want to do:
    ApiFunction( (obj.GetType())obj )

    Thanks,
    Last edited by h3ro; 10-05-2010 at 09:28 AM.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    I do not think you can do what you want. C# checks type safety at compile-time; doing something like you describe would require type safety checks to be delayed until run-time.

    Why not store your data in memory as their real data type instead of object?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,802
    Use generics then.
    Woop?

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Use an arraylist. You can then extract the type from the arraylist. There are ways to get the type of a certain element in an arraylist.

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by prog-bman View Post
    Use generics then.
    Which part of the problem is solved with generics?
    Quote Originally Posted by Bubba View Post
    Use an arraylist. You can then extract the type from the arraylist. There are ways to get the type of a certain element in an arraylist.
    Yes, just as you can get the real type of an object by calling GetType(). The problem isn't that it's difficult to get the type, but that you can't directly cast using a Type object.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,802
    Quote Originally Posted by pianorain View Post
    Which part of the problem is solved with generics.
    I misread the post my bad.
    Woop?

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I think you need to explain your design a bit before I can help any further. There are several type-safe ways to do what you want. However your statements so far have only shown me that you want a way to pass any parameters to a function of any type and any number but the type and number of params is not known. This cannot be done in any object-oriented language because it is far too generic to be useful.

    My suggestion is this:
    • Store the types that the function expects and the order it expects them in (IE: the method signature)
    • Create an interface for your parameters so you can get their type, value, etc
    • Create an interface to the collection and implement the interface to get access to the parameters
    • Pass an instance of the interface to the collection to the API function
    • Parse the collection and type check using typeof() against the types stored in the function types collection you created in the first step. If the types don't match then you can bail or inform the caller or whatever you want to do. If they do match then you proceed.


    Code:
    bool paramTypeMismatch = false;
    int paramCount = 0;
    foreach (Param p in Collection)
    {
        if (p.GetType() != StoredFunctionTypes[paramCount])
        {
           paramTypeMismatch = true;
           break;
        }
        ++paramCount;
    }
    
    if (!paramTypeMismatch)
    {
         // Execute function using collection information (parameters)
    }
    else
    {
       // Incorrect parameter types passed or they are in the wrong order
    }
    You will still have to switch on type at some point to do the actual cast:
    Code:
    if (p.GetType() == typeof(Single))
    {
        float value = (float)p.GetValue();
    }
    Or you can do this:
    Code:
    try 
    {
        float value = (float)p.GetValue();
    }
    catch (System.InvalidCastException e)
    {
        // p.GetValue() is not a float
    }
    Last edited by VirtualAce; 10-06-2010 at 06:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheriting Linked List
    By robolee in forum C++ Programming
    Replies: 20
    Last Post: 10-17-2009, 07:50 PM
  2. Texture Management in OpenGL
    By Brafil in forum Game Programming
    Replies: 13
    Last Post: 07-16-2009, 04:32 PM
  3. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 01:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21