Reputation: 5262
I'm trying to give a default value to the register property method. This required a function, but passed as an object(delegate?). Here's the code:
protected static propertydata registerproperty(string name, Type type, Func<object> createDefaultValue)
{
return RegisterProperty(name, type, createDefaultValue, false, null);
}
I want to call that registerproperty method, but I don't know how I can do that in VB.net. I just need to pass along a new Person object and I thought this was the way to go:
Public Shared ReadOnly ItemsProperty As PropertyData = RegisterProperty("Items", GetType(IEnumerable(Of Person)), Function() new Person())
This is a function passed as a function, but I need it to pass as an object.
Any thoughts on this?
Upvotes: 1
Views: 193
Reputation: 51
Sometimes, working with a sub instead of a function resolves the issue, we've solved some problems that way.
Public Shared ReadOnly ItemsProperty As PropertyData = RegisterProperty("Items", GetType(IEnumerable(Of Person)), Sub() new Person())
Upvotes: 1
Reputation: 112269
The parameter Func<object> createDefaultValue
means that you have to pass a function which returns an object. You don't have to pass an object.
Function() new Person()
is a lambda expression, which represents such a function in VB.
() => new Person()
is the same in C#.
Your ItemsProperty As PropertyData
will automatically call this function when a default value is required.
Upvotes: 0
Reputation: 6017
This should work even for older versions of the framework:
Public Shared Function whatever() As propertyData
registerproperty("item", GetType(IEnumerable(Of Person)), AddressOf GetObject)
End Function
Public Shared Function GetObject() As Person
return New Person
End Function
with VB 2008 or higher you can use what you have:
registerproperty("Item", GetType(IEnumerable(Of Person)), Function() New Person)
Upvotes: 1