Results 1 to 4 of 4

Thread: [2.0] Get a type and coerce value to that type

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Arrow [2.0] Get a type and coerce value to that type

    I have the ff. 'ugly' code:

    Code:
    private void InsertWhereParameter(WhereParameter whereParameter)
            {
                Type t = whereParameter.FieldValue.GetType();
                switch (t.ToString())
                {
                    case "System.String":
                        _dataAccess.AddWhereParameter(whereParameter.FieldName, (String)whereParameter.FieldValue);
                        break;
                    case "System.DateTime":
                        _dataAccess.AddWhereParameter(whereParameter.FieldName, (DateTime)whereParameter.FieldValue);
                        break;
    Is there something I am missing that would make it work like...
    Code:
    Type t = whereParameter.FieldValue.GetType();
    _dataAccess.AddWhereParameter(whereParameter.FieldName, (t)whereParameter.FieldValue);
    TIA
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Get a type and coerce value to that type

    What you're missing is that there's a difference between a data type and a Type object. A Type object is an instance of the Type class that represents a data type. It's not a data type itself. In order to cast a refernce as a particular type you MUST know the type you're casting too at compile time. The point of casting is to tell the compiler that an object is a specific type. If you don't know the type until run time then it's a bit late to be telling the compiler anything.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: [2.0] Get a type and coerce value to that type

    So there is really no other way to achieve it other than what I already have?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Get a type and coerce value to that type

    There are other ways to do it but they'd all involve some sort of test, unless you actually pass the type in too. You could do that with a DbType value.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width