I'm trying to pass a VB.NET List(Of String) collection to a [managed] C++ class [constructor) but am getting an error
Error BC30657 'New' has a return type that is not supported or parameter types that are not supported.
The receiving C++ class is expecting a List parameter which I thought was the C++ equivalent - but clearly it isn't.
What am I doing wrong?
Any recommendations for reading materials for CLI/Non-CLI considerations/how-tos/gotchas?
VB.NET:
Dim myStrList As List(Of String) = New List(Of String)
'<snip> populate list
Dim myObj As MyManagedClass = New MyManagedClass(myStrList)
'ERROR: argument not recognized
C++/CLI:
public ref class MyManagedClass
{
private:
//stuff
public:
MyManagedClass(List<String^> myStringListIn);
}
Related
I am doing an VB.Net WinForm Application. It is a migration of C#.
In C# I have a variable defined like this.
private static Dictionary<string, ExportFormatType> dicExtensiones =
new Dictionary<string, ExportFormatType> {
{".pdf", ExportFormatType.PortableDocFormat},
{".doc", ExportFormatType.WordForWindows},
{".xls", ExportFormatType.Excel},
{".rtf", ExportFormatType.RichText},
{".html", ExportFormatType.HTML40},
{".txt", ExportFormatType.Text}
};
And i migrated to this..
Private Shared dicExtensiones = New Dictionary(Of String, ExportFormatType) From
{{".pdf", ExportFormatType.PortableDocFormat},
{".doc", ExportFormatType.WordForWindows},
{".xls", ExportFormatType.Excel},
{".rtf", ExportFormatType.RichText},
{".html", ExportFormatType.HTML40},
{".txt", ExportFormatType.Text}}
Now I need to Loop through all ítems and get each value...
In C# is like this.
List<String> lista = new List<string>();
foreach (var item in dicExtensiones)
{
lista.Add(Enum.GetName(typeof(ExportFormatType), item.Value));
lista.Add("*" + item.Key);
}
The problem i have is that I do know how to migrate
Enum.GetName(typeof(ExportFormatType), item.Value);
to VB.Net, because Enum.GetName does not exists in VB.Net
How can I do it?
it can be like below in VB
Dim lista As List(Of [String]) = New List(Of String)()
For Each item As var In dicExtensiones
lista.Add([Enum].GetName(GetType(ExportFormatType), item.Value))
lista.Add("*" + item.Key)
Next
In VB, Enum is a keyword, as well as a class name, so you need to escape it in your code. The escaping syntax is similar to SQL:
[Enum].GetName
By escaping it, you're telling the compiler that you are referring to the identifier by that name rather than the keyword. For instance, you might also need to escape your own class or variable names on occasion:
Dim [property] As String = "belt, wallet with $50, casio watch"
or
Public Class [Class]
Public Property Teacher As String
Public Property Students As List(Of Student)
End Class
Though, in most cases it's preferable to just avoid it by thinking of a different name to use.
I would like to start that I don't want a to hear about how expensive and terrible reflection is. That won't help—I have a very good reason to use reflection and that's not my question.
Specifically, I have a class within a class that contains several static properties of the same type.
Public Class Foo
Public Class Bar
Public Shared Property prop1 As New CustomClass()
Public Shared Property prop2 As New CustomClass()
Public Shared Property prop3 As New CustomClass()
End Class
End Class
Public Class CustomClass
Public Sub DoStuff()
End Sub
End Class
I'm looking to create a method in Foo that calls DoStuff on each of the properties contained within it. How can I do this? Here's the general idea of what I want to include in Foo, but I obviously can't convert PropertyInfo to CustomClass:
Private Sub Example()
For Each prop As PropertyInfo In GetType(Foo.Bar).GetProperties()
DirectCast(prop, CustomClass).DoStuff()
Next
End Sub
How can I get the static properties and cast them to CustomClass objects?
PropertyInfo represents the type's property get/set method pair. To evaluate the getter you simply call GetValue, like so:
(in C# because I'm a language snob)
foreach( PropertyInfo pi in typeof(Foo.Bar).GetProperties() ) {
// Use null as arguments because it's a static property without an indexer.
Object got = pi.GetValue( null, null );
CustomClass got2 = got as CustomClass;
if( got2 != null ) {
Console.WriteLine( got2.ToString() );
}
}
And to convert Dai's answer to VB because I'm not a language snob:
For Each pi As System.Reflection.PropertyInfo in Foo.Bar.GetType.GetProperties()
' Use nothing as arguments because it's a shared property without an indexer.
Dim got = pi.GetValue(Nothing, Nothing)
Dim got2 as CustomClass = DirectCast(got, CustomClass)
If Not IsNothing(got2) Then Console.WriteLine(got2.toString())
Next
huzzah for less lines and more keystrokes...
I have an existing VB.net class library which has a public property with a type of VB's Collection class. I'm exposing the class library as a COM-object to be able to use it in Progress.
When I access the Collection-property with an integer index (e.g. comObj.OutputCol.Item(1)) it works fine, but when I try to use the string indexer (e.g. comObj.OutputCol.Item("FirstCol")) I get the following error (from a VBScript I use for testing):
Error message: Class doesn't support automation
Error code: 800A01AE
Is it possible to use the string indexer in any way via COM?
Sample code, COM-object i VB.net:
<ComClass(TestClass.ClassId, TestClass.InterfaceId, TestClass.EventsId)>
Public Class TestClass
Public Const ClassId As String = "063CA388-9926-44EC-B3A6-856D5299C210"
Public Const InterfaceId As String = "094ECC57-4E84-423A-B20E-BD109AEDBC20"
Public Const EventsId As String = "038B18BD-54B4-42D3-B868-71F4C52345B0"
Private _sOutputCol As Collection = Nothing
Private Property sOutputCol() As Collection
Get
If _sOutputCol Is Nothing Then
_sOutputCol = New Collection()
End If
Return _sOutputCol
End Get
Set(ByVal Value As Collection)
_sOutputCol = Value
End Set
End Property
Public ReadOnly Property OutputCol() As Collection
Get
Return sOutputCol
End Get
End Property
Public Sub New()
sOutputCol.Add("First object", "FirstCol")
sOutputCol.Add(2, "SecondCol")
End Sub
End Class
Sample test-code in VBScript:
Set comObj = WScript.CreateObject("VbComTest.TestClass")
wscript.echo comObj.OutputCol.Item(1) ' Works
wscript.echo comObj.OutputCol.Item(CStr("FirstCol")) ' Gives the error
I have registred the dll with: >regasm "...path...\VbComTest.dll" /codebase
OK, the problem was that the indexer is overloaded and you shouldn't use that in COM-visible interfaces: https://msdn.microsoft.com/en-us/library/ms182197.aspx
Extract from the page about what happens to overloaded methods:
When overloaded methods are exposed to COM clients, only the first
method overload retains its name. Subsequent overloads are uniquely
renamed by appending to the name an underscore character '_' and an
integer that corresponds to the order of declaration of the overload.
For example, consider the following methods.
void SomeMethod(int valueOne); void SomeMethod(int valueOne, int
valueTwo, int valueThree); void SomeMethod(int valueOne, int
valueTwo);
These methods are exposed to COM clients as the following.
void SomeMethod(int valueOne); void SomeMethod_2(int valueOne,
int valueTwo, int valueThree); void SomeMethod_3(int valueOne, int
valueTwo);
Visual Basic 6 COM clients cannot implement interface methods by using
an underscore in the name.
So to use the string indexer I have to write:
wscript.echo comObj.OutputCol.Item_3("FirstCol")
(Item_2 takes an Object as parameter and will also work, if the documentation is correct).
I am using YamlDotNet library to serialize some objects in Yaml. I've met some problems with the serialization of Guid properties.
Serialization of Guid properties generates empty brackets ( ex: {} )
See code below
Dim l As New List(Of Person)
l.Add(New Person() With {.Firstname = "MyFirstName", .Lastname = "MyLastName", .Id = Guid.NewGuid()})
Using sw As New StreamWriter("output.yaml", False)
Dim serializer = New Serializer()
serializer.Serialize(sw, l)
End Using
this code will output :
- Id: {}
Firstname: MyFirstName
Lastname: MyLastName
With the class:
Public Class Person
Public Property Id As Guid
Public Property Frstname As String
Public Property Lastname As String
End Class
Am I missing something or is this an issue of the library ?
You can define a custom converter to use when you need to customize the serialization of a type. The converter needs to implement IYamlTypeConverter, and be registered on the Serializer or Deserializer. Here is an example of such a converter:
Public Class GuidConverter
Implements IYamlTypeConverter
Public Function Accepts(type As Type) As Boolean Implements IYamlTypeConverter.Accepts
Return type = GetType(Guid)
End Function
Public Function ReadYaml(parser As IParser, type As Type) As Object Implements IYamlTypeConverter.ReadYaml
Dim reader = New EventReader(parser)
Dim scalar = reader.Expect(Of Scalar)()
Return Guid.Parse(scalar.Value)
End Function
Public Sub WriteYaml(emitter As IEmitter, value As Object, type As Type) Implements IYamlTypeConverter.WriteYaml
emitter.Emit(New Scalar(value.ToString()))
End Sub
End Class
The usage is quite simple:
Dim serializer = New Serializer()
serializer.RegisterTypeConverter(New GuidConverter())
serializer.Serialize(Console.Out, New With {.id = Guid.NewGuid()})
You can see a fully working example here.
When comparing types in VB the following works as expected and enables the current instance to be compared against a specific inherited class, in this case returning False (snippet from LINQPad)
Sub Main
Dim a As New MyOtherChildClass
a.IsType().Dump()
End Sub
' Define other methods and classes here
MustInherit class MyBaseClass
Public Function IsType() As Boolean
Return TypeOf Me Is MyChildClass
End Function
End Class
Class MyChildClass
Inherits MyBaseClass
End Class
Class MyOtherChildClass
Inherits MyBaseClass
End Class
However when generics are introduced the VB compiler fails with the error Expression of type 'UserQuery.MyBaseClass(Of T)' can never be of type 'UserQuery.MyChildClass'.
' Define other methods and classes here
MustInherit class MyBaseClass(Of T)
Public Function IsType() As Boolean
Return TypeOf Me Is MyChildClass
End Function
End Class
Class MyChildClass
Inherits MyBaseClass(Of String)
End Class
Class MyOtherChildClass
Inherits MyBaseClass(Of String)
End Class
The equivalent code in C# compiles and allows the comparison, returning the correct result
void Main()
{
var a = new MyOtherChildClass();
a.IsType().Dump();
}
// Define other methods and classes here
abstract class MyBaseClass<T>
{
public bool IsType()
{
return this is MyChildClass;
}
}
class MyChildClass : MyBaseClass<string>
{
}
class MyOtherChildClass : MyBaseClass<string>
{
}
Why does the VB compiler not allow this comparison?
You raise an interesting point about VB/C# compilation that I can't really speak to. If you're looking for a solution, here's a way to do it from the question How can I recognize a generic class?
Define these functions:
Public Function IsSubclassOf(ByVal childType As Type, ByVal parentType As Type) As Boolean
Dim isParentGeneric As Boolean = parentType.IsGenericType
Return IsSubclassOf(childType, parentType, isParentGeneric)
End Function
Private Function IsSubclassOf(ByVal childType As Type, ByVal parentType As Type, ByVal isParentGeneric As Boolean) As Boolean
If childType Is Nothing Then
Return False
End If
If isParentGeneric AndAlso childType.IsGenericType Then
childType = childType.GetGenericTypeDefinition()
End If
If childType Is parentType Then
Return True
End If
Return IsSubclassOf(childType.BaseType, parentType, isParentGeneric)
End Function
Call like this:
Dim baseType As Type = GetType(MyBaseClass(Of ))
Dim childType As Type = GetType(MyOtherChildClass)
Console.WriteLine(IsSubclassOf(childType, baseType))
'Writes: True
Here's a Microsoft Connect Ticket that might deal with this issue and give some explanation as to whether this was a feature or a bug of generic typing.
Although this case doesn't seem supported by the Type Of documentation which states that for classes, typeof will return true if:
objectexpression is of type typename or inherits from typename
I'm familiar with C# but less so with VB. However, the example VB code and example C# code appear to be different. In the VB example you use Return TypeOf Me Is MyChildClass, which in C# would be return typeof(this) is MyChildClass;. But the (supposedly working) C# example just has return this is MyChildClass;.
I would expect that TypeOf Me Is MyChildClass is asking whether the instance expression on the left (which is a Type) can be assigned to a variable declared as the type on the right (MyChildClass). Since the framework class Type has no connection to your MyChildClass this is impossible and thus a likely mistake which the compiler can catch with a warning or error--possibly the one you're getting.
Instead, I would think that the VB code should be Return Me Is MyChildClass to match the C# example, which should correctly ask if the instance Me can be assigned to a variable declared as MyChildClass. Does VB still object if this syntax is used, or does that fix the error and get the correct behavior?