I'm working with VC 2010 Express with dotNet V4 under Windows XP. I have a small Windows Forms application with a main form and a subform. I have also constructed a utility class of useful functions. There will only ever be one instance of it, and I cannot make it a static class. How do I code a call from the subform to a public method of the utility class without passing a reference of the utility class to the subform?
Is there away to remove the utility class from the main project so as to allow any other classes to call the public methods directly (ie without passing around references)?
Just a note: Singletons and DLLs don't solve the problem of having to pass references. So forget those answers.
Loading
louarnoldPosted Jun 19, 2011, 4:58 PM
To make further comments clear: I have finaly understood that what I wanted would not change the coupling (how many cross-calls) between components although it would increase the coheasion (how how much the components must know about each other). The goal was re-usable code that requires no cohesion, and as little coupling as possible.
The static proxy that you mentioned are somewhat similar to the Singleton pattern. I did some tests and found out that the singleton code really does allow access to anything from anything. The code:
SingletonClass.Instance.DoSomething();
really can call Do Something from anywhere.
But this comes with a price: the hard-coded name 'SingletonClass" means that the caller knows about the callee, and that's not good for a re-usable component.
The better solution is in fact the C# Interface. I had abandoned it, but now I see that they eliminate the cohesion, at the expense of more complicate coupling - if not more of it.
Sam HobbsPosted Jun 19, 2011, 2:00 PM
Note that you are still explaining what you want in terms of what you don't want.
Please don't ignore what I said about a static item for an instance of the class. The class does not need to be static nor anything in the class.
louarnoldPosted Jun 19, 2011, 1:18 PM
I understand now, and yes, it does seem more complex than it needs to be. But thanks for trying.
louarnoldPosted Jun 19, 2011, 1:13 PM
Static classes cannot inherit, and in some classes I have to inherit C# interfaces. Static classes also can't have a "state" because they may be called from different threads. But they also cannot have instance variable because they are static.
I'm an programmer from the days when classes didn't exist. (and that wasn't THAT long ago.) At that time you could write utility code, compile it with test code and test it, and then compile it with one or more other applications. With OO techniques, however, one seems to need much greater coupling. I ask for an OO technque that gives me what I had without OO, beacuse I don't have enough experience to know if such an architecture exists in OO.
(Sorry. I'm new to this forum and I don't know how to get a quote in a reply.)
VulpesPosted Jun 19, 2011, 1:13 PM
louarnoldPosted Jun 19, 2011, 12:51 PM
Sam HobbsPosted Jun 19, 2011, 4:51 AM
You don't have to make all your methods or the class static or anything such as that; you can have a single static instance of an object of the class.
Just a note: dwelling on the negative is likely to cause all answers to be forgotten.
VulpesPosted Jun 18, 2011, 4:33 PM