VB usage of C# Dll Constructor and Enum

G

Guest

I know this is the VB languages new group. I have a .NET library (DLL)
written in C#. I am adding examples to my documentation for my VB users. I
have a VB example that will not compile.

C# struct and enum:

using System;

namespace VLib
{
public enum Types : sbyte
{
T1, T2, T3, T4,
}
public struct T
{
public ushort x;
public Types t;

public T( ushort x, Types t )
{
this.x = x;
this.t = t;
}
}
}

Compiles no errors.

Visual Basic Code:

Option Strict Off
Imports Vlib
Module Module1

Sub Main()

' The following errors occur on the definition below.

' Value of type 'Integer' cannot be converted to 'System.UInt16'.
' Value of type 'Byte' cannot be converted to 'VLib.Types'.
Dim q As VLib.T = New VLib.T(10, Types.T2)

End Sub

End Module

Ok What is going on here?

Do I need to cast the literal 10 to System.UInt16?
Why does VB think Types.T2 is of type BYTE and not of tyoe Types?
 
T

Tom Shelton

I know this is the VB languages new group. I have a .NET library (DLL)
written in C#. I am adding examples to my documentation for my VB users. I
have a VB example that will not compile.

C# struct and enum:

using System;

namespace VLib
{
public enum Types : sbyte
{
T1, T2, T3, T4,
}
public struct T
{
public ushort x;
public Types t;

public T( ushort x, Types t )
{
this.x = x;
this.t = t;
}
}
}

Compiles no errors.

Visual Basic Code:

Option Strict Off
Imports Vlib
Module Module1

Sub Main()

' The following errors occur on the definition below.

' Value of type 'Integer' cannot be converted to 'System.UInt16'.
' Value of type 'Byte' cannot be converted to 'VLib.Types'.
Dim q As VLib.T = New VLib.T(10, Types.T2)

End Sub

End Module

Ok What is going on here?

Do I need to cast the literal 10 to System.UInt16?
Why does VB think Types.T2 is of type BYTE and not of tyoe Types?

sbyte and UInt are not supported by vb.net. In other words, they
violate the CTS. Change the types in VLib to byte and short and you
shouldn't have any problems. If the value of T.x my exceed the limit of
a short, then you might want to promote it to an int.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top