2

I am trying to implement map function in VB.NET and I have tried the below.

 Function Map(a,f)
    Dim i
    for each i in a
      f(i)
    next
 End Function

 Function alert(a)
    MessageBox.Show(a)
 end function

But the above code is not working and saying alert is not declared.
Please help me on this.

Exception
  • 8,111
  • 22
  • 85
  • 136

2 Answers2

2

Your functions don't return anything. Try this:

Public Sub Map(Of T)(ByVal a As IEnumerable(Of T), ByVal f As Action(T))
    For Each i As T In a
      f(i)
    Next
End Sub

Public Sub alert(ByVal a As Object)
    MessageBox.Show(a)
End Sub

Above is based on the methods from the question. An actual traditional Map() function might look more like this:

Public Iterator Function Map(Of T)(ByVal a As IEnumerable(Of T), ByVal f As Func(Of T,T)) As IEnumerable(Of T)
    For Each i As T In a
        Yield f(i)
    Next
End Function

Or this:

Public Iterator Function Map(Of T, U)(ByVal a As IEnumerable(Of T), ByVal f As Func(Of T, U)) As IEnumerable(Of U)
    For Each i As T In a
        Yield f(i)
    Next
End Function
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
1

I am not a vb expert. But if you are using vb.net. It should be like this:

MessageBox.Show("Your Message Here")

I in vb6 it is like this:

MsgBox("Your Message Here")

The reason why i am asking is that. In vb.net you can use

Option Strict OFF

and then you don't have to declare any types. But by default in vb.net it set to

Option Strict ON
Arion
  • 31,011
  • 10
  • 70
  • 88