473,586 Members | 2,683 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(B yVal 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(By Val number As
Integer)
Console.WriteLi ne("From PrintInteger: The
number is {0}.", number)
End Sub
Shared Sub main()
Dim driver As New Driver()
Dim printer As New Printer()

printer.PrintCa llback = New PrintCallback
(AddressOf driver.PrintInt eger)
printer.PrintCa llback(10)

Console.WriteLi ne("Press enter to exit...")
Console.ReadLin e()
End Sub
End Class
End Class
End Namespace
Jul 21 '05 #1
2 1530
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(B yVal 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(1 0)
End Set
End Property

Public Class Driver
Private Sub PrintInteger(By Val number As Integer)
Console.WriteLi ne("From PrintInteger: The number is
{0}.", number)
End Sub

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

oPrinter.pPrint Callback = AddressOf driver.PrintInt eger

Console.WriteLi ne("Press enter to exit...")
Console.ReadLin e()
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(B yVal 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(By Val number As
Integer)
Console.WriteLi ne("From PrintInteger: The
number is {0}.", number)
End Sub
Shared Sub main()
Dim driver As New Driver()
Dim printer As New Printer()

printer.PrintCa llback = New PrintCallback
(AddressOf driver.PrintInt eger)
printer.PrintCa llback(10)

Console.WriteLi ne("Press enter to exit...")
Console.ReadLin e()
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(B yVal 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(By Val number As Integer)
Console.WriteLi ne("From PrintInteger: The number is
{0}.", number)
End Sub

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

oPrinter.PrintC allback = AddressOf oDriver.PrintIn teger
oPrinter.PrintN umbers()

Console.WriteLi ne("Press enter to exit...")
Console.ReadLin e()
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(B yVal 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(By Val number As Integer)
Console.WriteLi ne("From PrintInteger: The number is {0}.", number) End Sub
Shared Sub main()
Dim driver As New Driver()
Dim printer As New Printer()

printer.PrintCa llback = New PrintCallback
(AddressOf driver.PrintInt eger)
printer.PrintCa llback(10)

Console.WriteLi ne("Press enter to exit...")
Console.ReadLin e()
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(B yVal 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(By Val number As
Integer)
Console.WriteLi ne("From PrintInteger: The
number is {0}.", number)
End Sub
Shared Sub main()
Dim driver As New Driver()
Dim printer As New Printer()

printer.PrintCa llback = New PrintCallback
(AddressOf driver.PrintInt eger)
printer.PrintCa llback(10)

Console.WriteLi ne("Press enter to exit...")
Console.ReadLin e()
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
2271
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 Sun stance on lack of support for delegates: .... There are serious semantic problems with trying to add delegates to a language in a consistent...
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 custom function to be called in a provider class. My confusion is that you can already achieve this by interfaces and objects without delegates (which...
11
3775
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 noticed that if I did this, it worked (please ignore the lack of try ... catch blocks, because I cut down the code to be more brief - I have a...
2
1572
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 if() or switch() logic, and without the overhead of a function call? As an example, consider the task of resizing a bitmap from size X1,Y1 to size...
30
3626
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 it seems to me that Microsoft has developed similar functionality via two keywords. I do understand that an event offers better encapsulation as the...
2
1216
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 worthy to use in our application. I doubt what's the purpose the delegates?
69
5555
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 signature for the method (i.e., as below, int Square (int)). Here is an example to show that feature. Note class "UnAwareClass" has its methods Square...
3
4839
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 pointers (delegates). I assume I need to use the array< T keyword to allocate an array of delegates, and then initialize the array by setting each...
12
6727
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 anyone please let me know what are the disadvantages of using delegates. Thanks, James.
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 delegateResult = (AsyncResult) result --will it always return the active/completed delegates in loaded in CLR? Thanks using System;
0
7912
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8202
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
5390
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.