Jedhi Posted September 7, 2005 Posted September 7, 2005 Are there a way to have typeless delegates ? For example delegate void VoidDelegate() delegate bool BoolDelegate(bool parm1, bool parm2) Take for example you want to call a function ProcessDelegate. and the parameter you want to transfer could either be a VoidDelegate or a BoolDelegate. Instead of writing two function with exactly the same contents except for one of the function has the type VoidDelegate. Is it possible to make the delegate typeless ? void ProcessDelegate( delegate could either be VoidDelegate or BoolDelegate) { } Quote
*Experts* Nerseus Posted September 7, 2005 *Experts* Posted September 7, 2005 I'm not sure how you'd call that Delegate that's passed to ProcessDelegate? Taking your pseudo-code one step further: void ProcessDelegate( delegate could either be VoidDelegate or BoolDelegate) { // Call delegate here... but how? } If the delegate you want to call can be either VoidDelegate or BoolDelegate, how would you plan on calling it? One takes 2 params while the other takes none. To use a delegate, it must be called and it must "know" the signature. There is no "BaseClass" syntax for a Delegate, or an interface that a delegate can support. A delegate is just a signature of what a method will look like. An instance of a delegate must be of a specific type of delegate and that will always only have one signature. -nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Diesel Posted September 7, 2005 Posted September 7, 2005 Use object as the parameter for ProcessDelegate and then check the type in the function. Quote
Diesel Posted September 7, 2005 Posted September 7, 2005 private void Form1_Load(object sender, System.EventArgs e) { TestDelegate p = new TestDelegate(TestFunction); p(); TestDelegate2 p2 = new TestDelegate2(TestFunction); p2(1, 2); ProcessDel(p); ProcessDel(p2); } delegate void TestDelegate(); delegate void TestDelegate2(int param1, int param2); private void TestFunction() { Console.WriteLine("TestFunction"); } private void TestFunction(int param1, int param2) { Console.WriteLine(param1.ToString() + ", " + param2.ToString()); } private void ProcessDel(object crap) { if (crap.GetType() == typeof(TestDelegate)) { ((TestDelegate)(crap))(); } else { ((TestDelegate2)(crap))(1, 2); } } } It's ugly, but it works Quote
Administrators PlausiblyDamp Posted September 7, 2005 Administrators Posted September 7, 2005 Could you explain a bit more about why you need to do things this way? The whole purpose of a delegate is to enforce type safety when using callbacks etc. Allowing different types of delegate to be passed in violates this idea. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Jedhi Posted September 8, 2005 Author Posted September 8, 2005 The only reason to do it, is to avoid multiple functions that do the same things except for its delegate type. So be able to choose between type of delegate to transfer multiple functions could be limited to one. Quote
Administrators PlausiblyDamp Posted September 8, 2005 Administrators Posted September 8, 2005 (edited) Is there any reason you couldn't just use a single delegate for this? Just because the delegate has 1 or more parameters doesn't mean the handler at runtime needs to use the parameters. Could you either attach a simplified example of why this is needed or expand on the idea of why a single delegate wouldn't be suitable. Depending on how the problem is approached you may not need two functions entirely copied - possible a couple of overloads that call into a single core function may do the trick. Edited March 7, 2007 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Jedhi Posted September 10, 2005 Author Posted September 10, 2005 I can not use a single delegate since one delegate use 1 parameter and the other use several parameters. Quote
Administrators PlausiblyDamp Posted September 10, 2005 Administrators Posted September 10, 2005 Has this requirement been designed in by someone else or is it part of your design? If you are required to call an external function that takes one parameter could you not create a wrapper function that matches the many parameter delegate and then call the single parameter function from that? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leaders snarfblam Posted September 11, 2005 Leaders Posted September 11, 2005 I can not use a single delegate since one delegate use 1 parameter and the other use several parameters. Ultimately, performing very similar tasks with different types of data is a difficult situation to handle elegantly. If you are creating the functions that will be called through a delegate, a possible solution to this situation (one that I believe can be seen in places in the .Net framework) is to create a base class which contains the bare minimum or none of the data that needs to be passed, and then derived class(es) that can hold additional data. Instantiate the base class, or to pass more information instantiate a derived class, then pass it to the delegate which accepts your class and acts based on the type of the object. I know you are coding in C# but I'm sure that you can figure out the jist of this VB: 'This is our one and only delegate signature, MessageBoxFunction Public Delegate Sub MessageBoxFunction(ByVal Info As MessageBoxInfo) 'This class encapsulates information to be passed to a delegate 'Here we only have a message Public Class MessageBoxInfo Public Message As String Public Sub New(ByVal Message As String) Me.Message = Message End Sub End Class 'Derived classes can vary or extend the information 'Here we add a title to the message Public Class DetailedMessageBoxInfo Inherits MessageBoxInfo Public Title As String Public Sub New(ByVal Message As String, ByVal Title As String) MyBase.New(Message) Me.Title = Title End Sub End Class Class TestClass 'This is the function we will be passing different sets of data to via delegates Public Sub ShowMessageBox(ByVal Info As MessageBoxInfo) 'Act based on the type of object passed If TypeOf Info Is DetailedMessageBoxInfo Then MessageBox.Show(Info.Message, DirectCast(Info, DetailedMessageBoxInfo).Title) Else MessageBox.Show(Info.Message) End If End Sub 'Here we call the same delegate twice, sending only a message the first ' time, and a message and title the second Public Sub DoTheDelegate() Dim MyFunction As MessageBoxFunction = AddressOf ShowMessageBox Dim Info1 As New MessageBoxInfo("Passing Data!") Dim Info2 As New DetailedMessageBoxInfo("With Style!", "Passing Data") MyFunction(Info1) 'Send message MyFunction(Info2) 'Send message and title via the same delegate End Sub End Class You can use a similar solution for return types if you need to return different kinds of data through the delegate. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.