I am coding a library for my own personal use, so I can create other projects faster and more conveniently. Most of the library is written in VB.NET (a simplified version of C#), but since VB.NET includes no capability for unsafe coding, I have written a small portion in C# to handle tasks requiring pointers. Why I mention the VB.NET library will become apparent later in this question.
One of the static classes in my accompanying C# library is a relatively simple function for converting between unmanaged structures. It is as follows:
public static unsafe class SurroundingClass
{
public static T1 ValueToValueBitwise<T0, T1>(T0 Value)
where T0 : unmanaged
where T1 : unmanaged
{
if (sizeof(T0) != sizeof(T1)) throw new ArgumentException();
return *(T1*)&Value;
}
}
I apologize if my style of coding is unconventional; I am self-taught.
The function generates no compiler errors in C#, and works magnificently when used by other C# functions. However, events do not progress as smoothly when I reference the function from a VB.NET project. Here is an example of a call to my function in VB.NET:
Dim myInt As Integer = ValueToValueBitwise(Of Single, Integer)(13.5!)
When I attempt to compile this function with the VB.NET compiler, it fails, providing the rather uninformative excuse "'' is an unsupported type." Usually, when I get this sort of "'' is invalid" error, it is because I have not specified a name or type parameter to a declaration or type argument, respectively. However, this is obviously not the case, as the equivalent function call works flawlessly in C#. When you hover over a function name with your cursor, Visual Studio gives you a popup with the syntax for calling the function. Interestingly, when I hover over the function name with my cursor, it pops up with the following: Visual Studio Popup
At the present time, my best speculation is that VB.NET has no equivalent to C#'s "unmanaged" keyword, and as a result, the VB.NET compiler dies a miserable death. I am looking for either a patch, a workaround, or some other solution. I am using Visual Studio 2022 version 17.6.3, with the .NET framework in both projects set at version 4.8. Any assistance would be greatly appreciated. Thank you!
As user dbc explained, VB.NET has no support for unmanaged types. As a result, the types did not qualify, because they were required to inherit from a type that didn't exist in VB.NET. This caused the compiler to throw an error when it attempted compilation.
Thank you for your help!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With