Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. VB.NET: Failes to assign an Action to a method

VB.NET: Failes to assign an Action to a method

Scheduled Pinned Locked Moved Visual Basic
helpcsharplearning
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mc_Topaz
    wrote on last edited by
    #1

    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 Sub

    Sub 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.

    Richard DeemingR 1 Reply Last reply
    0
    • M Mc_Topaz

      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 Sub

      Sub 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.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      In your VB.NET code, you're calling the Qux method, and attempting to store the return value in an Action. But the method doesn't return a value, so there's nothing to store, and you get a compiler error. You need to use the AddressOf 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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      M 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        In your VB.NET code, you're calling the Qux method, and attempting to store the return value in an Action. But the method doesn't return a value, so there's nothing to store, and you get a compiler error. You need to use the AddressOf 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

        M Offline
        M Offline
        Mc_Topaz
        wrote on last edited by
        #3

        Thanks! It I tried the AddressOf earlier, but beeing used to C# I added parentheses as well: AddressOf(Baz). Of course that doesn't work :doh:

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups