Translate Code: Convert of type

I have the follow in VB:

pt = CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
 


I want make in C#, I make it:

tv = (TreeView)Convert.ChangeType(sender,typeof(TreeView));
pt = tv.PointToClient(
new Point(e.X,e.Y));

 


I can make it, just in a one line


Answer this question

Translate Code: Convert of type

  • Valdimar

    Thanks, David, is good the code!.

    Smile, Well, Code in VB and C#, all traslate a MSIL, for this I wanted to make in one line. I see the MSIL code, is different:
    (it is of my initial code, my first post)
    VB -> MSIL
     IL_0012:  brfalse    IL_00a4
      IL_0017:  ldarg.1
      IL_0018:  castclass  [System.Windows.Forms]System.Windows.Forms.TreeView
      IL_001d:  ldloca.s   _Vb_t_record_0
      IL_001f:  ldarg.2
      IL_0020:  callvirt   instance int32 [System.Windows.Forms]System.Windows.Forms.DragEventArgs::get_X()
      IL_0025:  ldarg.2
      IL_0026:  callvirt   instance int32 [System.Windows.Forms]System.Windows.Forms.DragEventArgs::get_Y()
      IL_002b:  call       instance void [System.Drawing]System.Drawing.Point::.ctor(int32,
                                                                                     int32)
      IL_0030:  nop
      IL_0031:  ldloc.s    _Vb_t_record_0
      IL_0033:  callvirt   instance valuetype [System.Drawing]System.Drawing.Point [System.Windows.Forms]System.Windows.Forms.Control::PointToClient(valuetype [System.Drawing]System.Drawing.Point)
      IL_0038:  stloc.2
      IL_0039:  ldarg.1

    C# -> MSIL
    IL_0098
      IL_0016:  ldarg.1
      IL_0017:  ldtoken    [System.Windows.Forms]System.Windows.Forms.TreeView
      IL_001c:  call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
      IL_0021:  call       object [mscorlib]System.Convert::ChangeType(object,
                                                                       class [mscorlib]System.Type)
      IL_0026:  castclass  [System.Windows.Forms]System.Windows.Forms.TreeView
      IL_002b:  stloc.3
      IL_002c:  ldloc.3
      IL_002d:  ldarg.2
      IL_002e:  callvirt   instance int32 [System.Windows.Forms]System.Windows.Forms.DragEventArgs::get_X()
      IL_0033:  ldarg.2
      IL_0034:  callvirt   instance int32 [System.Windows.Forms]System.Windows.Forms.DragEventArgs::get_Y()
      IL_0039:  newobj     instance void [System.Drawing]System.Drawing.Point::.ctor(int32,
                                                                                     int32)
      IL_003e:  callvirt   instance valuetype [System.Drawing]System.Drawing.Point [System.Windows.Forms]System.Windows.Forms.Control::PointToClient(valuetype [System.Drawing]System.Drawing.Point)


  • Lada Sobr

    David with your code, don't appear the follow lines:

      IL_0017:  ldtoken    [System.Windows.Forms]System.Windows.Forms.TreeView
      IL_001c:  call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
      IL_0021:  call       object [mscorlib]System.Convert::ChangeType(object,
                                                                       class [mscorlib]System.Type)


    it's was deleted.

  • MiniSong

    Why don't you just do this:


    pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
     


    You don't need the Convert.ChangeType.



  • Jeff Beranek

     serghio wrote:


    tv = (TreeView)Convert.ChangeType(sender,typeof(TreeView));
    pt = tv.PointToClient(
    new Point(e.X,e.Y));

     

    I can make it, just in a one line


    Yes, just wrap the whole of the first line in parentheses before adding the meat of the second line.


    pt = ((TreeView)Convert.ChangeType (sender, typeof(TreeView))).PointToClient (new Point (e.X, e.Y));
     


    But why try to crush everything into one line   I think your two lines of code are much more readable.

  • Translate Code: Convert of type