473,772 Members | 2,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delegates will be the end of me

I am desperately trying to figure out the following problem,
simplified for discussion.

I have a WinForm with a button, textbox, and datagrid. When I click
the button, I want to execute the method defined in the textbox, which
must return a dataset, and pass this dataset to the datasource of my
datagrid. Delegates and/or Reflection seem to be the natural way to
accomplish this.

My "Data tier" resides in another assembly which is referenced in my
WinForms project. I have in that assembly the following class
definition:

namespace Apex.Data
{
public class Class1
{
private Class1(){}

public static DataSet GetDataSet()
{
//return my dataset here...
}
}
}

So, in my attempts to figure this out, I've created the following
delegate and event handler for my button's click event:

public delegate DataSet DataProvider();

private void button1_Click(o bject sender, System.EventArg s e)
{
//1. manual binding
//this.dataGrid1. DataSource =
Apex.Data.Class 1.GetDataSet(). Tables[0];

//2. explicitly defined delegate
//DataProvider method = new
DataProvider(Ap ex.Data.Class1. GetDataSet);
//DataSet ds = method();
//this.dataGrid1. DataSource = ds.Tables[0];

//3. dynamically defined delegate
string functionName = this.textBox1.T ext;
string className = functionName.Su bstring(0,funct ionName.LastInd exOf("."));
string methodName =
functionName.Su bstring(functio nName.LastIndex Of(".")+1);

DataProvider method =
(DataProvider)S ystem.Delegate. CreateDelegate( typeof(DataProv ider),
typeof(Apex.Dat a.Class1), functionName);
DataSet ds = method();
this.dataGrid1. DataSource = ds.Tables[0];
}

The results of these 3 attempts:

1. I was just making sure the dang static method itself works. It
does.
2. Define the delegate explicitly. No problem.
3. Create the delegate at runtime. NOT HAPPENING.

The problem with #3 appears to be that System.Type.Get Type(className)
returns null. Why does it return null? Is there a better approach to
this whole problem? Thanks in advance.
Nov 16 '05 #1
2 1430
The example 3 that you showed is using typeof(Apex.Dat a.Class1) instead of
GetType(classNa me). Can I assume that the example #3 that you show works?

Now, if you replace the typeof(...) with GetType(classNa me), the reason this
is likely not working is that you're not providing the necessary assembly
information so the class loader can find the type. Here's an excerpt from
the MSDN documentation on GetType(string typeName):

"typeName can be a simple type name, a type name that includes a namespace,
or a complex name that includes an assembly name specification.
If typeName includes only the name of the Type, this method searches in the
calling object's assembly, then in the mscorlib.dll assembly. If typeName is
fully qualified with the partial or complete assembly name, this method
searches in the specified assembly."

You need to provide a fully qualified class name, which looks like this:
MyNamespace.MyC lass,MyAssembly .

Ken
"Todd" <tp****@apexcg. com> wrote in message
news:47******** *************** ***@posting.goo gle.com...
I am desperately trying to figure out the following problem,
simplified for discussion.

I have a WinForm with a button, textbox, and datagrid. When I click
the button, I want to execute the method defined in the textbox, which
must return a dataset, and pass this dataset to the datasource of my
datagrid. Delegates and/or Reflection seem to be the natural way to
accomplish this.

My "Data tier" resides in another assembly which is referenced in my
WinForms project. I have in that assembly the following class
definition:

namespace Apex.Data
{
public class Class1
{
private Class1(){}

public static DataSet GetDataSet()
{
//return my dataset here...
}
}
}

So, in my attempts to figure this out, I've created the following
delegate and event handler for my button's click event:

public delegate DataSet DataProvider();

private void button1_Click(o bject sender, System.EventArg s e)
{
//1. manual binding
//this.dataGrid1. DataSource =
Apex.Data.Class 1.GetDataSet(). Tables[0];

//2. explicitly defined delegate
//DataProvider method = new
DataProvider(Ap ex.Data.Class1. GetDataSet);
//DataSet ds = method();
//this.dataGrid1. DataSource = ds.Tables[0];

//3. dynamically defined delegate
string functionName = this.textBox1.T ext;
string className = functionName.Su bstring(0,funct ionName.LastInd exOf(".")); string methodName =
functionName.Su bstring(functio nName.LastIndex Of(".")+1);

DataProvider method =
(DataProvider)S ystem.Delegate. CreateDelegate( typeof(DataProv ider),
typeof(Apex.Dat a.Class1), functionName);
DataSet ds = method();
this.dataGrid1. DataSource = ds.Tables[0];
}

The results of these 3 attempts:

1. I was just making sure the dang static method itself works. It
does.
2. Define the delegate explicitly. No problem.
3. Create the delegate at runtime. NOT HAPPENING.

The problem with #3 appears to be that System.Type.Get Type(className)
returns null. Why does it return null? Is there a better approach to
this whole problem? Thanks in advance.

Nov 16 '05 #2
Thanks Ken!

That worked, especially after I got the method name correct in my
code! :~|

I put a second textbox on my form in which I enter the assembly name.
Here's the code now, which uses a different constructor for the
CreateDelegate( ) method than I was using before only because the IL is
slightly leaner with this code:

string functionName = this.textBox1.T ext;
string className = functionName.Su bstring(0,funct ionName.LastInd exOf("."));
string methodName = functionName.Su bstring(functio nName.LastIndex Of(".")+1);
string assemblyName = this.textBox2.T ext;

DataSet ds = ((DataProvider) System.Delegate .CreateDelegate (typeof(DataPro vider),
Assembly.Load(a ssemblyName).Ge tType(className ).GetMethod(met hodName)))();

this.dataGrid1. DataSource = ds.Tables[0];

"Ken Kolda" <ke*******@elli emae-nospamplease.co m> wrote in message news:<#6******* *******@TK2MSFT NGP09.phx.gbl>. ..
The example 3 that you showed is using typeof(Apex.Dat a.Class1) instead of
GetType(classNa me). Can I assume that the example #3 that you show works?

Now, if you replace the typeof(...) with GetType(classNa me), the reason this
is likely not working is that you're not providing the necessary assembly
information so the class loader can find the type. Here's an excerpt from
the MSDN documentation on GetType(string typeName):

"typeName can be a simple type name, a type name that includes a namespace,
or a complex name that includes an assembly name specification.
If typeName includes only the name of the Type, this method searches in the
calling object's assembly, then in the mscorlib.dll assembly. If typeName is
fully qualified with the partial or complete assembly name, this method
searches in the specified assembly."

You need to provide a fully qualified class name, which looks like this:
MyNamespace.MyC lass,MyAssembly .

Ken
"Todd" <tp****@apexcg. com> wrote in message
news:47******** *************** ***@posting.goo gle.com...
I am desperately trying to figure out the following problem,
simplified for discussion.

I have a WinForm with a button, textbox, and datagrid. When I click
the button, I want to execute the method defined in the textbox, which
must return a dataset, and pass this dataset to the datasource of my
datagrid. Delegates and/or Reflection seem to be the natural way to
accomplish this.

My "Data tier" resides in another assembly which is referenced in my
WinForms project. I have in that assembly the following class
definition:

namespace Apex.Data
{
public class Class1
{
private Class1(){}

public static DataSet GetDataSet()
{
//return my dataset here...
}
}
}

So, in my attempts to figure this out, I've created the following
delegate and event handler for my button's click event:

public delegate DataSet DataProvider();

private void button1_Click(o bject sender, System.EventArg s e)
{
//1. manual binding
//this.dataGrid1. DataSource =
Apex.Data.Class 1.GetDataSet(). Tables[0];

//2. explicitly defined delegate
//DataProvider method = new
DataProvider(Ap ex.Data.Class1. GetDataSet);
//DataSet ds = method();
//this.dataGrid1. DataSource = ds.Tables[0];

//3. dynamically defined delegate
string functionName = this.textBox1.T ext;
string className =

functionName.Su bstring(0,funct ionName.LastInd exOf("."));
string methodName =
functionName.Su bstring(functio nName.LastIndex Of(".")+1);

DataProvider method =
(DataProvider)S ystem.Delegate. CreateDelegate( typeof(DataProv ider),
typeof(Apex.Dat a.Class1), functionName);
DataSet ds = method();
this.dataGrid1. DataSource = ds.Tables[0];
}

The results of these 3 attempts:

1. I was just making sure the dang static method itself works. It
does.
2. Define the delegate explicitly. No problem.
3. Create the delegate at runtime. NOT HAPPENING.

The problem with #3 appears to be that System.Type.Get Type(className)
returns null. Why does it return null? Is there a better approach to
this whole problem? Thanks in advance.

Nov 16 '05 #3

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

Similar topics

4
22891
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on when to use one over another? If anyone could provide any additional info, your comments, best practices, any good articles, specific examples, etc. Thank you
14
3265
by: Lior Amar | last post by:
Quick question about threads and delegates. I have the following scenario Thread A (CLASSA) spawns Thread B (CLASSB) and passes it a DelegateA to a callback Thread B Invokes a DelegateB asynchronously (could be a timer but then we get Thread C) Upon completion of DelegateB, Thread B would like to callback ThreadA using DelegateA but as we all know the call to DelegateA is running in ThreadB. Is
8
1776
by: Nicky Smith | last post by:
Hello, I'm reading Mike Gunderloy's Mcad Vb.net book, and I've also read the MS press Mcad book for the same topic ".. Windows based applications with VB.net" for exam 70-306. In the sections in both books that try to teach the use of delagates and events, I'm really lost, and to make matters worse, I've written a user-control that fires events for the host form, and this works without delegates!
2
6216
by: Viet | last post by:
I have a couple of questions that hopefully someone could clarify for me. I have an app that uses the threading.timer to constantly poll a scanner to scan in documents. I understand that Async delegates can also be used for polling purposes. Would it be more efficient to use async delegates in this case? Are their any examples on how to use this under vb.net forms? Thanks, Jonathan
5
3879
by: GVN | last post by:
Hi All, I recently worked on delegates. But I have a question regarding Multicast delegates. The scenario is as follows: I have a delegate myDelegate with object dlgt, and two methods myMethod1(), and myMethod2(). Using multicast delegates definition I can write as follows to add the two methods to my multicast delegate:
6
2637
by: =?Utf-8?B?Sko=?= | last post by:
I have a logger component that logs to multiple sources, ie textfile, eventlog etc. and I have two methods that depending on where I call up my logger comp. one of them will be called. For ex. if I throw an exception I want to call one method and if I dont, I am just logging some info to eventlog, I will call the other. Now i'm wondering would it make sense to use delegates, one for each method to call methods in my window service? How...
0
4775
by: bharathreddy | last post by:
Delegates Here in this article I will explain about delegates in brief. Some important points about delegates. This article is meant to only those who already know delegates, it will be a quick review not a detailed one. Delegates quite simply are special type of object, a delegate just contains the details of a method. One good way to understanding delegates is by thinking of delegates as something that gives a name to a method...
6
2662
by: =?Utf-8?B?T2xkQ2FEb2c=?= | last post by:
My question is regarding the use of delegates in C#. I see how .Net uses delegates to wire event handlers to events. It’s an object created by a single line of code by the system and that makes perfect sense to me. I understand that there is a lot of code underneath that the system has created that makes it all work, thereby making it pretty efficient for the programmer. Outside of the use of delegates to wire event handlers, you can...
9
3118
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying to store multicast delegates in a hash table, and then fire the delegates one of two ways (after registering/ creating the delegates, etc).
4
1794
by: samadams_2006 | last post by:
Hello, I'm trying to figure out why delegates are such a great thing. For example, I came across the following line of code: "This ability to refer to a method as a parameter makes delegates ideal for defining callback methods. For example, a sort algorithm could be passed a reference to the method that compares two objects. Separating the comparison code allows the algorithm to be written in a more general way."
0
9454
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10104
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7460
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
3
2850
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.