VagabondSW Posted October 3, 2005 Posted October 3, 2005 I am trying to convert the following C# code to Visual Basic.NET, but I am having a hard time: private IntPtr m_context; public object UserData { get { return (object)((GCHandle)m_context).Target; } } When I try to convert this to Visual Basic.NET, I came up with the following code: Private m_context As IntPtr Public ReadOnly Property UserContext() As Object Get Return CObj(CType(m_context, GCHandle).Target) End Get End PropertyUnfortunately, where the C# version will build without errors, the Visual Basic.NET version reports the following error in the Task List: Value of type 'System.IntPtr' cannot be converted to 'System.Runtime.InteropServices.GCHandle'. I figure I must be doing something wrong. Any help or suggestions would be greatly appreciated. Quote "Never ascribe to malice that which can adequately be explained by incompetence." -- Napolean Bonaparte
Leaders snarfblam Posted October 4, 2005 Leaders Posted October 4, 2005 I don't know what the code is supposed to do but it looks like your difficulty lies in converting from an IntPtr to a GCHandle. Try this: Private m_context As IntPtr Public ReadOnly Property UserContext() As Object Get Return GCHandle.op_Explicit(m_context).Target End Get End Property Quote [sIGPIC]e[/sIGPIC]
VagabondSW Posted October 5, 2005 Author Posted October 5, 2005 Thanks. That did the trick. Quote "Never ascribe to malice that which can adequately be explained by incompetence." -- Napolean Bonaparte
Leaders snarfblam Posted October 5, 2005 Leaders Posted October 5, 2005 You might want to read up in operator overloading. It isn't supported in Vb.Net 2003, but you can explicitly call the function that an operator is mapped to. A cast would be op_Explicit, Addtion would be op_Addition, etc. Quote [sIGPIC]e[/sIGPIC]
bri189a Posted October 6, 2005 Posted October 6, 2005 You know that's a good point marble - I've not even thought to do that in VB and really I should've on some things... however the only way I've ever been able to call those is through reflection in VB - it's a syntax that I can't get to work now that you got me trying it...(I use Option Strict On btw) - can you show me an example of using op_Addition and op_Explicit (where your casting from custom base types to custom derived types). Whatever it is I know it's lack of brain cells on my end; but I've had a 40 hour week already and it's only Wed... so that may be part of it. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.