473,511 Members | 10,195 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delegate casting problem

PJ6
I have a generalized delegate,

Public Delegate Function DataRetreivingDelegate() as Object

with an associated property in a certain class. Almost never will I actually
be wanting to set the property of this delegate type to a method that
returns an Object - rather I want to point it to methods that will be
returning strings, integers, decimals, whatever.

So I tried this -

Public Shared Function GetSomething() as String

End Function

DirectCast(AddressOf GetSomething, DataRetreivingDelegate)

Only this doesn't work. Why doesn't the Framework allow method signature
return types to be up-casted?

Anyway, what's the best way around this without having to physically code
additional methods that return Objects? I know I can just use a raw
MethodInfo, but I don't want to complicate this any more than I have to
since performance will be a consideration.

TIA,
Paul
Jun 11 '06 #1
1 1082
Hi,

If you are using vb 2005 try this.

Public Delegate Function DataRetreivingDelegate(Of T)() As T

Public Function GetSomething() As String
Return "test"
End Function
Dim d As New DataRetreivingDelegate(Of String)(AddressOf
GetSomething)
Label1.Text = d.Invoke()
Ken
---------------------
"PJ6" <no***@nowhere.net> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
I have a generalized delegate,

Public Delegate Function DataRetreivingDelegate() as Object

with an associated property in a certain class. Almost never will I
actually be wanting to set the property of this delegate type to a method
that returns an Object - rather I want to point it to methods that will be
returning strings, integers, decimals, whatever.

So I tried this -

Public Shared Function GetSomething() as String

End Function

DirectCast(AddressOf GetSomething, DataRetreivingDelegate)

Only this doesn't work. Why doesn't the Framework allow method signature
return types to be up-casted?

Anyway, what's the best way around this without having to physically code
additional methods that return Objects? I know I can just use a raw
MethodInfo, but I don't want to complicate this any more than I have to
since performance will be a consideration.

TIA,
Paul

Jun 11 '06 #2

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

Similar topics

4
2164
by: Tim Werth | last post by:
I am trying to use reflection to add an event handler for the RowUpdated event of the OracleDataAdapter object (ODP.NET), but the same thing can be said for SqlDataAdapter if you only use...
6
4812
by: Ondrej Sevecek | last post by:
Hello, what is the difference between "event" and the only use of delegate? Why one should note events with "event" keyword when the functionality seems the same as with pure delegates? When I...
8
4529
by: Phill | last post by:
All the event handlers seem to pass an Object and an EventArgs object. If the event doesn't need this info why pass them anyway? It is inefficient.
0
1005
by: Eric Chaves | last post by:
Hi folks, Im writing a small GUI application to deploy objects that allows a user to create my objects, set it's properties, serialize it into a binary package for futher load into production...
1
1311
by: Greg Wilkerson | last post by:
I hope I can explain this in a way that can be understood. I have a custom control, I'll call it navigator, used to navigate "things". I has, among other controls, two standard buttons, previous...
4
1399
by: Joanna Carter [TeamB] | last post by:
Hi folks I think I have asked something similar before, but here goes again... I have to call a method that varies depending on the type of one of its parameters. The object that I have to...
1
1805
by: Quimbly | last post by:
I'm having some problems comparing delegates. In all sample projects I create, I can't get the problem to occur, but there is definitely a problem with my production code. I can't give all the...
7
2034
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation:...
10
2094
by: vcquestions | last post by:
Hi. Is there way to have a function pointer to a delegate in c++/cli that would allow me to pass delegates with the same signatures as parameters to a method? I'm working with managed code. ...
0
7148
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
7367
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
7517
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
5673
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,...
1
5072
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
4743
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
3230
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...
0
1581
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 ...
0
451
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...

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.