VB6 Modules =? in C# ???
-
Hi, Remember the bad old days of VB6? You wanted a global variable, you added a module, and that was it. I know its bad programming... :p Now, my app loads one Form only. But, the app has a lot of classes that are created to deal with network data. How can I have the app's main Form(the ui) get modified by those other classes? How do you modify a form's progress bar from another class? ARgh.. Thanks for your help :) Antoine This by our hands that dream, "I shall find a way or make one!"
-
Hi, Remember the bad old days of VB6? You wanted a global variable, you added a module, and that was it. I know its bad programming... :p Now, my app loads one Form only. But, the app has a lot of classes that are created to deal with network data. How can I have the app's main Form(the ui) get modified by those other classes? How do you modify a form's progress bar from another class? ARgh.. Thanks for your help :) Antoine This by our hands that dream, "I shall find a way or make one!"
Pass a reference of the Form instance to the class you want to modify. For example,
// in the Form class, MyForm.cs
OtherClass theOtherClass = new OtherClass(this);// in the other class
public class OtherClass
{
MyForm myFormInstance = null;public OtherClass(MyForm frmInst)
{
this.myFormInstance = frmInst;this.myFormInstance.DoSomething();
}
}Of course you can always make your Form class have a public static variable to make it accessible from other classes, like so:
public class MyForm : Form
{
public static MyForm TheFormInstance = null;public MyForm()
{
TheFormInstance = this;
}
}// in some other class (note that MyForm is the actual name of the class, not a class instance)
MyForm.TheFormInstance.DoSomething();Now as far as using the singleton pattern above, Heath or some other very talented developer will chide me for not mentioning the correct way to do it (when using multiple threads and such). But the above code should suffice if you're just using one Form instance. :-) #include "witty_sig.h"
-
Pass a reference of the Form instance to the class you want to modify. For example,
// in the Form class, MyForm.cs
OtherClass theOtherClass = new OtherClass(this);// in the other class
public class OtherClass
{
MyForm myFormInstance = null;public OtherClass(MyForm frmInst)
{
this.myFormInstance = frmInst;this.myFormInstance.DoSomething();
}
}Of course you can always make your Form class have a public static variable to make it accessible from other classes, like so:
public class MyForm : Form
{
public static MyForm TheFormInstance = null;public MyForm()
{
TheFormInstance = this;
}
}// in some other class (note that MyForm is the actual name of the class, not a class instance)
MyForm.TheFormInstance.DoSomething();Now as far as using the singleton pattern above, Heath or some other very talented developer will chide me for not mentioning the correct way to do it (when using multiple threads and such). But the above code should suffice if you're just using one Form instance. :-) #include "witty_sig.h"
hi! What is the correct way to do ti when using multiple threads? Thanks Antoine This by our hands that dream, "I shall find a way or make one!"
-
hi! What is the correct way to do ti when using multiple threads? Thanks Antoine This by our hands that dream, "I shall find a way or make one!"
-
Hi, Remember the bad old days of VB6? You wanted a global variable, you added a module, and that was it. I know its bad programming... :p Now, my app loads one Form only. But, the app has a lot of classes that are created to deal with network data. How can I have the app's main Form(the ui) get modified by those other classes? How do you modify a form's progress bar from another class? ARgh.. Thanks for your help :) Antoine This by our hands that dream, "I shall find a way or make one!"
antoine@orchus-tech wrote: How can I have the app's main Form(the ui) get modified by those other classes? How do you modify a form's progress bar from another class? Your classes are not getting instantiated by the form??? The form could grab this info from the classes after they are created. If you need info established in a class and passed around then you could make that class using the Singleton pattern which involves alot of static methods and getter/setters. If the form must be modified from outside, then you expose public accessors (getter and setter) for internal fields and pass reference to that form to everyone from the central controller in the app. antoine@orchus-tech wrote: How do you modify a form's progress bar from another class? Go to the C# projects in this site and search for SplashScreen. There is one that is really good and demonstrates the ability of having a smooth status bar based on actual event timings. My class design is as such main form --> loads main class (contains some static methods etc main form --> loads splash screen main form --> calls each init step notifying SplashScreen of event end main form completes init --> calls SplashScreen with 'all done' event splash screen goes away. main form loads MainForm and shows it. Notice that the main class is still around and everything spawned by the main form has access to that class and its' methods. This is similar to the global thing in VB6 but a purer object approach. Search the net for c# patterns and then check on the Singleton pattern for more info. ______________________________ The Tao gave birth to machine language. Machine language gave birth to the assembler. The assembler gave birth to ten thousand languages. Each language has its purpose, however humble. Each language expresses the Yin and Yang of software. Each language has its place within the Tao. Beauty exists because we give a name to C#. Bad exists because we give a name to COBOL.