Using Delegates

B

Billy Jacobs

Can someone tell me what the problem with this code is.
Specifically the line in all caps near the bottom. I am
getting an error on this line saying:
"Property Access must assingn to the property or use its
value".

When I do that I get a different error saying:
"Expression does not produce a value"

I took this code and converted it from C# to VB.

Namespace MyDelegate
Public Delegate Sub PrintCallback(ByVal number As
Int32)
Public Class Printer
Private _print As PrintCallback
Public Property PrintCallback() As PrintCallback
Get
Return _print
End Get
Set(ByVal Value As PrintCallback)
_print = Value
End Set
End Property

Public Class Driver
Private Sub PrintInteger(ByVal number As
Integer)
Console.WriteLine("From PrintInteger: The
number is {0}.", number)
End Sub
Shared Sub main()
Dim driver As New Driver()
Dim printer As New Printer()

printer.PrintCallback = New PrintCallback
(AddressOf driver.PrintInteger)
printer.PrintCallback(10)

Console.WriteLine("Press enter to exit...")
Console.ReadLine()
End Sub
End Class
End Class


End Namespace
 
K

Kairi Zikpin

What exactly are you trying to do here?
Was this written in visual studio or a plain old text editor?
I ask this second question because vs.net wont compile this code as is.

Problems I see:
1. Your delegate name and property name ARE THE SAME.
please use different names.
2. Not sure what this code is trying to do. I suggest you pst the
original code to get a proper conversion
3. the delegate is invoked on the "server side", ie, it should be
invoked in the printer class since this is where you assign the delegate
a value

try this code - it compiles and prints 10
Namespace MyDelegate
Public Delegate Sub PrintCallback(ByVal number As Int32)

Public Class Printer
Private _print As PrintCallback

Public Property pPrintCallback() As PrintCallback
Get
Return _print
End Get
Set(ByVal Value As PrintCallback)
_print = Value

_print.Invoke(10)
End Set
End Property

Public Class Driver
Private Sub PrintInteger(ByVal number As Integer)
Console.WriteLine("From PrintInteger: The number is
{0}.", number)
End Sub

Shared Sub main()
Dim driver As New driver()
Dim oPrinter As New Printer()

oPrinter.pPrintCallback = AddressOf driver.PrintInteger

Console.WriteLine("Press enter to exit...")
Console.ReadLine()
End Sub
End Class

End Class

End Namespace
 
K

Kairi Zikpin

Was this written in visual studio or a plain old text editor?
I ask this question because vs.net wont compile this code as is.

1. Not sure what this code is trying to do. I suggest you post the
original code to get a proper conversion

2. the delegate is invoked on the "server side", ie, it should be
invoked in the printer class, where it is assigned a value. you may
write a warpper method to invoke the delegate as i have done

try this code - it compiles and prints
Namespace MyDelegate
Public Delegate Sub PrintCallback(ByVal number As Integer)

Public Class Printer
Private _print As PrintCallback

Public Property PrintCallback() As PrintCallback
Get
Return _print
End Get
Set(ByVal Value As PrintCallback)
_print = Value
End Set
End Property

Public Class Driver
Private Sub PrintInteger(ByVal number As Integer)
Console.WriteLine("From PrintInteger: The number is
{0}.", number)
End Sub

Shared Sub main()
Dim oDriver As New Driver()
Dim oPrinter As New Printer()

oPrinter.PrintCallback = AddressOf oDriver.PrintInteger
oPrinter.PrintNumbers()

Console.WriteLine("Press enter to exit...")
Console.ReadLine()
End Sub
End Class

Public Sub PrintNumbers()
Dim num() As Integer = {10, 20, 30}
Dim n As Integer

For Each n In num
_print.Invoke(n)
Next
End Sub
End Class

End Namespace

Billy said:
Can someone tell me what the problem with this code is.
Specifically the line in all caps near the bottom. I am getting an
error on this line saying: "Property Access must assingn to the property
or use its value".
When I do that I get a different error saying: "Expression does not produce a value"

I took this code and converted it from C# to VB.

Namespace MyDelegate
Public Delegate Sub PrintCallback(ByVal number As Int32)
Public Class Printer
Private _print As PrintCallback
Public Property PrintCallback() As PrintCallback
Get
Return _print
End Get
Set(ByVal Value As PrintCallback)
_print = Value
End Set
End Property

Public Class Driver
Private Sub PrintInteger(ByVal number As Integer)
Console.WriteLine("From PrintInteger: The number is {0}.", number)
End Sub
Shared Sub main()
Dim driver As New Driver()
Dim printer As New Printer()

printer.PrintCallback = New PrintCallback
(AddressOf driver.PrintInteger)
printer.PrintCallback(10)

Console.WriteLine("Press enter to exit...")
Console.ReadLine()
End Sub
End Class
End Class


End Namespace
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top