473,508 Members | 2,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using Delegates

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
Jul 21 '05 #1
2 1521
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
Billy Jacobs wrote:
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


Jul 21 '05 #2
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 Jacobs wrote:
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

Billy Jacobs wrote:
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


Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2260
by: Jeffrey T. Smith | last post by:
Back when the new J2SE1.5 features were announced, there was a JavaLive community chat (http://java.sun.com/developer/community/chat/JavaLive/2003/jl0729.html) in which Neal Gafter explains the...
3
397
by: Sam | last post by:
I’m just starting to learn delegates. I’m at the very beginning. If I understand correctly, delegates are for when you want to pass a function as a parameter. For example the client provides a...
11
3760
by: Doug Thews | last post by:
I've been working on some samples that use BeginInvoke/EndInvoke. In one example, I call BeginInvoke and pass it an AsyncCallback function pointer. I was messing around with ReaderWriterLocks and...
2
1565
by: Marcos Stefanakopolus | last post by:
In C# is there any way to use the concept of delegates to have multiple implementations of a particular block of code, so that you can choose between them without the overhead of possibly expensive...
30
3608
by: Burkhard | last post by:
Hi, I am new to C# (with long year experience in C++) and I am a bit confused by the language construct of events. What is it I can do with events that I cannot do with delegates? At the moment...
2
1205
by: hangaround | last post by:
I used delegates recently, It seemed the delegates was like function_call very much, and we couldn't use delegates to mimic the system events( ex, button_click), so didn't think the delegates...
69
5516
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same...
3
4831
by: David K in San Jose | last post by:
I'm using managed (CLR) C++ in VS2005 to create a Windows app that contains a form named "MyForm". In the code for that form I'm trying to invoke some static functions by using an array of function...
12
6716
by: James | last post by:
Hello, I am new to C# programming and I am using delegates for the event- driven application and found them very useful. Are there any applications where delegates may not be useful ? Can...
1
3122
by: puzzlecracker | last post by:
Below is the example that I mercilessly copied from Jon's article. In this example, I do not understand how his AsyncResult was able to retrieve delegates. In other words, using AsyncResult...
0
7229
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7129
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7398
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.