Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 Property

Unfortunately, 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.

"Never ascribe to malice that which can adequately be explained by incompetence." -- Napolean Bonaparte
  • Leaders
Posted

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

[sIGPIC]e[/sIGPIC]
  • Leaders
Posted
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.
[sIGPIC]e[/sIGPIC]
Posted
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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...