1

Suppose I have this simple class:

Public Class Person
    Public Property Name() As String
    Public Property Married() As Boolean
End Class

I want to create a delegate to its property getters. After a bit of search, including this, I've written this code in VB.NET:

Dim p As New Person()
p.Name = "Joe"
p.Married = True

Dim propGetter1 As Func(Of Boolean) = Function() p.Married
Dim propGetter2 As Func(Of String) = Function() p.Name

And retrieve the value of the properties as follows:

Dim married as Boolean = propGetter1.Invoke
Dim name as String = propGetter2.Invoke

It works fine, but contrary to the example in C# from the link above, it does not check at compile-time the type returned by the Function, so I could just write this code:

Dim propGetter1 As Func(Of Boolean) = Function() p.Name

Getting an error at runtime.

I've also tried this code:

Dim propGetter3 As Func(Of String) = Function(p As Person) p.Name

But I get the following error:

Nested function does not have a signature that is compatible with delegate 'System.Func(Of Boolean)'

So, how can I write this delegates with type-checking at compile-time, just like in C#??

EDIT:

As I've been asked to do so, I explain what I'm trying to do with this approach. I have an application that reads the state of a system (probe mesures, flows, preassures...) each few seconds, and stores this information in some properties of the objects that model the system.

I want to offer the user a list of all the objects defined which have properties that get their values updated this way. Therefore, the user can set alarm rules, so when a property reaches a certain value, an alarm is fired.

In orden to do so, I need to save delegates to the chosen properties, so I can check its values each few seconds, and compare them to the expected ones. Does it make sense?

Community
  • 1
  • 1
Tao Gómez Gil
  • 2,228
  • 20
  • 36
  • **1)** Unless your name is "True" or "False", how can a name ever be a boolean value? `Dim propGetter1 As Func(Of Boolean) = Function() p.Name` **2)** `Func(Of String)` should be `Func(Of Person, String)` – Bjørn-Roger Kringsjå Sep 29 '14 at 15:14
  • 1) I want to learn a syntax that protects me from making that kind of mistakes, I mean, assigning a Function which returns some type (i.e. String) to a delegate that expects a Function returning some other type (i.e. Boolean). 2) I want to obtain a delegate to a property setter of an instance of an object, which I can use without that object. If I declare it as you suggest, I will need to pass the object as an argument, am I rigth? – Tao Gómez Gil Sep 29 '14 at 15:21
  • What are you trying to achieve with it? Yes, it is possible to build a spherical horse in vacuum, theoretically... Please state the original problem you are having with your code. And then point how this approach would address it. – Victor Zakharov Sep 29 '14 at 15:32
  • You can use reflection to retrieve the getter/setter methods. And if you really need to get a *pointer* then you should use `Marshal.GetFunctionPointerForDelegate`. – Bjørn-Roger Kringsjå Sep 29 '14 at 15:32

1 Answers1

4

This is VB.NET's in/famous dynamic typing at work:

Option Strict Off

Module Module1
    Sub Main()
        Dim p As New Person With {.Name = "True"}
        Dim propGetter1 As Func(Of Boolean) = Function() p.Name
        Dim result = propGetter1()    '' Fine :)
    End Sub
End Module

If you don't like surprises like this then just use Option Strict On and the compiler will tell you that you got it wrong at build time. Use Tools + Options, Projects and Solutions, VB Defaults to change the default setting. Do beware that you might have some trouble with older projects ;)

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536