VB.NET: Failes to assign an Action to a method
-
I require to assign an Action to a method. I can do this in C# without problem. But when I port the C# code to VB.NET I fail at one line. My company has a VB.NET application where we require this solution. I have created a small and reproducible solution regarding this in both a C# and VB. Of course this is not the real solution I have, but the problem is same. C# code
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
var foo = new Foo();
foo.Bar = Qux; // This line fails in VB.NET.
foo.Baz();
}static void Qux() { Console.WriteLine(nameof(Qux)); } } class Foo { public Action Bar { get; set; } public void Baz() { Bar(); } }
}
VB.NET code
Module Module1
Sub Main()
Dim foo As Foo = New Foo()
foo.Bar = Qux() ' This line failes.
foo.Baz()
End SubSub Qux() Console.WriteLine(NameOf(Qux)) End Sub Class Foo Private b As Action Public Property Bar() As Action Get Return b End Get Set(ByVal value As Action) b = value End Set End Property Public Sub Baz() b() End Sub End Class
End Module
The compile error message: Expression does not produce a value VB.NET is not my primary language and I'm sure I have made some very basic mistake. I don't like to ask for a solution, but I cannot find the correct way to do this. Please provide me with the correct solution to fix this.
-
I require to assign an Action to a method. I can do this in C# without problem. But when I port the C# code to VB.NET I fail at one line. My company has a VB.NET application where we require this solution. I have created a small and reproducible solution regarding this in both a C# and VB. Of course this is not the real solution I have, but the problem is same. C# code
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
var foo = new Foo();
foo.Bar = Qux; // This line fails in VB.NET.
foo.Baz();
}static void Qux() { Console.WriteLine(nameof(Qux)); } } class Foo { public Action Bar { get; set; } public void Baz() { Bar(); } }
}
VB.NET code
Module Module1
Sub Main()
Dim foo As Foo = New Foo()
foo.Bar = Qux() ' This line failes.
foo.Baz()
End SubSub Qux() Console.WriteLine(NameOf(Qux)) End Sub Class Foo Private b As Action Public Property Bar() As Action Get Return b End Get Set(ByVal value As Action) b = value End Set End Property Public Sub Baz() b() End Sub End Class
End Module
The compile error message: Expression does not produce a value VB.NET is not my primary language and I'm sure I have made some very basic mistake. I don't like to ask for a solution, but I cannot find the correct way to do this. Please provide me with the correct solution to fix this.
In your VB.NET code, you're calling the
Qux
method, and attempting to store the return value in anAction
. But the method doesn't return a value, so there's nothing to store, and you get a compiler error. You need to use theAddressOf
operator to create a delegate pointing to the required method:foo.Bar = AddressOf Qux
AddressOf Operator - Visual Basic | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
In your VB.NET code, you're calling the
Qux
method, and attempting to store the return value in anAction
. But the method doesn't return a value, so there's nothing to store, and you get a compiler error. You need to use theAddressOf
operator to create a delegate pointing to the required method:foo.Bar = AddressOf Qux
AddressOf Operator - Visual Basic | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer