473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1519
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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.