Jay1b Posted September 4, 2006 Posted September 4, 2006 Hi I'm having problem getting a class to be referenced from several forms. The class in question is a simple class to log output to a file. So at any point i can write LogEvent("12 rows were returned") and this will be written out to a log file. I would like to instigate the class when the main screen loads and have every other form call this class with just using the above code. Can someone please tell me how i can do this? Thanks Jay Quote
*Experts* mutant Posted September 4, 2006 *Experts* Posted September 4, 2006 You could build the log class using static methods if you only need one log application wide. Then you would be able to access it like you want: ClassName.LogEvent("Hello World") Quote
Jay1b Posted September 4, 2006 Author Posted September 4, 2006 Thanks. Have you got an example of calling/creating static methods please? Quote
*Experts* mutant Posted September 4, 2006 *Experts* Posted September 4, 2006 Check out this short intro with code sample on MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconSharedMembers.asp Quote
mskeel Posted September 4, 2006 Posted September 4, 2006 option/idea 1. Make it a static class with a LogEvent(string) method? option/idea 2. Initialize the Logger class in your main form (with a file name, etc) and pass it to each of the classes that might use it. option/idea 3. Do the same as 2, but pass a delegate to the LogEven(string) method instead of the Logger Class. Quote
Jay1b Posted September 11, 2006 Author Posted September 11, 2006 Thanks guys. I've finally got my head around sharing classes. Or at least i think i have. I'm going to be using one of two methods. 1) For classes such as the LogEvent one, which will be accessed from all frames, i'll use shared 2) For classed which will just be used between 2 frames, i'll pass the instance of the class between the two frames. 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.