pendragon Posted August 2, 2004 Posted August 2, 2004 Hi I have a class that I use for all my data access procedures, What I want to do is, when the user decides to amend the current record, the program will call a routine in my data access class that will re-fresh the row and then display it. The problem I have is as the routine will be called from lots of different forms I have no form name which means I can't program the form name at design time and I can't get at the forms routine to show the new data. Does anyone know of a way round this. thanks Quote
Administrators PlausiblyDamp Posted August 2, 2004 Administrators Posted August 2, 2004 When you call a function in the class you probably want to pass a callback function in as a parameter - search MSDN for delegate to get the general idea. Alternatiively if you post a sample function from your class I'm sure we can knock up a sample. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Joe Mamma Posted August 2, 2004 Posted August 2, 2004 Hi I have a class that I use for all my data access procedures, What I want to do is, when the user decides to amend the current record, the program will call a routine in my data access class that will re-fresh the row and then display it. The problem I have is as the routine will be called from lots of different forms I have no form name which means I can't program the form name at design time and I can't get at the forms routine to show the new data. Does anyone know of a way round this. thanks delegation. will write more shortly. Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Administrators PlausiblyDamp Posted August 2, 2004 Administrators Posted August 2, 2004 Had some spare time ;) given the following worker class Public Class WorkerClass Public Delegate Sub FormCallback(ByVal info As String) Public Sub DoSomeWork(ByVal data As String, ByVal CallBack As FormCallback) 'do things here Dim s As String = data.ToUpper 'and when finished CallBack(s) End Sub End Class it firstly defines a delegate - this can be thought of as a type-safe function pointer, it can contain the address (or point to) any function that matches it's signature. In this case the delegate defines a sub routine that expects a single string as an argument. The function that does the work (my example is a fairly loose definition of work ;)) now accepts two parameters - one that is the data to work with and the other is a function that is compatable with the previously declared delegate. This function does whatever it needs to do and at the end it calls the delegate function (actually calls whatever function the delegate points to). To use this from a form we have the following code Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim c As New WorkerClass c.DoSomeWork(TextBox1.Text, AddressOf WorkDone) End Sub Private Sub WorkDone(ByVal result As String) MessageBox.Show(result) End Sub Here we define a WorkDone method that is compatible with the classes' FormCallback delegate (simply displays the string in a messagebox). In the button click we define and instantiate a new instance of the worker class, and then call it's method - passing a string into the first parameter and then the address of our Workdone method as the second. When the application is run the class will call this form's method - if other forms define their own implementation then they can pass those in at runtime and have them called correctly. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
VBAHole22 Posted August 2, 2004 Posted August 2, 2004 Thank you for the explanation, that helps a lot. This stuff is kind of confusing. I'm going to print that one out so that I have the framework to start with when I attempt to do this. I'm not sure I understand how this solves the original problem. Couldn't he just use the name of the current form or get it from the Click event somehow and then write the data access code to be 'form-independent'? Quote Wanna-Be C# Superstar
Administrators PlausiblyDamp Posted August 2, 2004 Administrators Posted August 2, 2004 You could just pass the form in as a parameter but that would require knowledge of the calling forms structures (or impose a standard) - if one form required a listbox and another a grid then things could get messy. You could use a standard interface and require one or more functions to be present - but this means forms have to provide all the interfaces methods and if you require one form to have multiple implementations of the interface it can get really confusing. Using a delegate simple means a single form can implement the callback how it sees fit (even have multiple implementations) without imposing too many restrictions. The only drawback is that delegates currently incur a slight performance hit when compared to interfaces / virtual functions but this is being addressed in the next version and the speed is now comparable. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
pendragon Posted August 2, 2004 Author Posted August 2, 2004 Thanks for that PlausiblyDamp it does exactly what I need :cool: I converted it to c# and it ended up slightly different to what you put in VB, in dataaccess class public delegate void FormCallBack(); . . . . public static void RefreshRecord(string row, FormCallBack CallBack) { MessageBox.Show(row); CallBack(); } Then in calling program private void button1_Click(object sender, System.EventArgs e) { dataaccess.FormCallBack c = new Library.dataaccess.FormCallBack(ShowFields); dataaccess.RefreshRecord("His is in the row", c); } private void ShowFields() { MessageBox.Show("This is from ShowFields"); } I think this is the way it should be done but if you can see something that can be done better then please let me know. I am going to have to read up on delegates to fully understand what can be done with them but this has given me a good start. :D Thanks Quote
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.