Results 1 to 4 of 4

Thread: [RESOLVED] Late Binding - Casting

  1. #1

    Thread Starter
    Addicted Member silentthread's Avatar
    Join Date
    Jun 2006
    Location
    Miami, Florida
    Posts
    143

    Resolved [RESOLVED] Late Binding - Casting

    Hi folks, I recently moved from vb.net to c#. I normally store my dataview in a session object for sorting and paging purposes. (asp.net apps)

    I'm curious to know what is the difference between the following....

    Code:
    Type T = Session["SessionDataViewTable"].GetType();
            /*Above, I get the type of the Session variable that contains
              a dataview  */
            object[] param = new object[1];// creating an object array with only 1 element in size
            param[0] = e.SortExpression + " " + Session["SortOrder"];
            //Above, I set the element zero to the initial sort order string
            T.InvokeMember("Sort", BindingFlags.SetProperty, null, Session["SessionDataViewTable"], param);
            //Above, I Invoke the "sort" member of the dataview object.
            /* I had to check before what type of member it is, and that is
            why I specify in the binding flags that it is a SetProperty */

    and....

    Code:
    ((DataView)Session["SessionDataViewTable"]).Sort = "id" + " " + Session["SortOrder"];

    Originally, when I first moved from vb.net to c#(last week), I tried to do this...

    Code:
    Session["SessionDataViewTable"].Sort = "id" + " " + Session["SortOrder"];
    I got a compiler error stating something to do with Sort not being a member of that session variable. So I did the research and found out that vb.net allows this late binding that I got used to. I read online that I need to do my own late binding. Then I find out that I could just cast it to get the method.
    Is there a difference beside the fact that one of my options requires only 1 line of code?

    Thx
    Watch media as you download it! Excellent tool!
    FREE CUBA!
    MyBlog
    If you feel my post has helped, please rate it.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Late Binding - Casting

    The first method uses reflection and is thus horribly inefficient and nearly unreadable. The second method should always be preferred. Ditto in VB.NET; you should enable Option Strict, which disallows late binding without explicit casting.
    Unless you have a very good reason to use reflection, don't.

    As well as the parenthesised (cast) operator, you can also use the 'as' operator. The difference between these is that whilst (T)O will throw an 'invalid cast' exception if O cannot be cast to T, (O as T) will return null. As well as that point I often use the the latter simply because I find it more readable.

  3. #3

    Thread Starter
    Addicted Member silentthread's Avatar
    Join Date
    Jun 2006
    Location
    Miami, Florida
    Posts
    143

    Re: Late Binding - Casting

    thanks
    Watch media as you download it! Excellent tool!
    FREE CUBA!
    MyBlog
    If you feel my post has helped, please rate it.

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: [RESOLVED] Late Binding - Casting

    One important difference is that (O as T) will not use any custom-written conversion code you may have implemented, whereas (T) will...







    //code culled and butchered from MSDN...

    class ClassT
    {
    public static explicit operator ClassT(int ClassO)
    {
    ClassT temp = new ClassT();

    //....blah....

    return temp;
    }
    }



    class ClassO
    {
    public static implicit operator int(ClassO m)
    {
    //....blah....
    }
    }

    I don't live here any more.

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