Create variable of base types

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nick Weekes

    Create variable of base types

    Im trying to create a variable that can store all possible simple C#
    types. This is so I can assign the type to a database column,
    regardless of whether its string, int, bool etc. But, Id rather not
    cast a string and switch the value, that seems a bit redundant.

    So an example would be:

    //
    SimpleBaseTypes MyType;
    MyType = bool;
    MyDatabaseColum n.Type = MyType;
    //

    Is this possible or am I going about this in the wrong way?

    Cheers all,

    Nick
  • Salvador

    #2
    RE: Create variable of base types

    Hi,

    The father of all the types is the "object" type, so you can assign whatever
    you like to it. The problem is that after the assignation the variable is
    cast to object and lost the original type, so if you ask for the type it will
    say "object".

    C# version 2.0 includes templates that will allow you to do this like C++.

    Best regards
    Salva



    "Nick Weekes" wrote:
    [color=blue]
    > Im trying to create a variable that can store all possible simple C#
    > types. This is so I can assign the type to a database column,
    > regardless of whether its string, int, bool etc. But, Id rather not
    > cast a string and switch the value, that seems a bit redundant.
    >
    > So an example would be:
    >
    > //
    > SimpleBaseTypes MyType;
    > MyType = bool;
    > MyDatabaseColum n.Type = MyType;
    > //
    >
    > Is this possible or am I going about this in the wrong way?
    >
    > Cheers all,
    >
    > Nick
    >[/color]

    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: Create variable of base types

      Hi,

      What you want is done alreayd, Object is the ancestor of ALL the types,
      in fact DataTable.Rows[x][ColumnName] is an object this allow you to assign
      it no matter the real type fo the column.

      cheers,

      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation


      "Nick Weekes" <nickjunkinbox@ yahoo.co.uk> wrote in message
      news:3c9fc6a5.0 504220216.601ac 7b4@posting.goo gle.com...[color=blue]
      > Im trying to create a variable that can store all possible simple C#
      > types. This is so I can assign the type to a database column,
      > regardless of whether its string, int, bool etc. But, Id rather not
      > cast a string and switch the value, that seems a bit redundant.
      >
      > So an example would be:
      >
      > //
      > SimpleBaseTypes MyType;
      > MyType = bool;
      > MyDatabaseColum n.Type = MyType;
      > //
      >
      > Is this possible or am I going about this in the wrong way?
      >
      > Cheers all,
      >
      > Nick[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        RE: Create variable of base types

        Salvador <Salvador@discu ssions.microsof t.com> wrote:[color=blue]
        > The father of all the types is the "object" type, so you can assign
        > whatever you like to it. The problem is that after the assignation
        > the variable is cast to object and lost the original type, so if you
        > ask for the type it will say "object".[/color]

        No, no type information is lost. For instance:

        using System;

        class Test
        {
        static void Main()
        {
        int i = 5;
        object o = i;

        Console.WriteLi ne (o.GetType().Fu llName);
        }
        }

        will print "System.Int 32", not "System.Object" .

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        Working...