Form-Aware Objects


I'm developing a library of classes in C#, and naturally want each one to be as generic as possible.  I want the classes to be able to know if they are being used in a Windows Form or in a Web Form.

How do I do this?
Who is Participating?
McExpConnect With a Mentor Commented:
As previous posters have asked I'd be interested in hearing why your utility code needs to know where it is being called from. But here is a solution anyway.

you could add a extra parameter of type object which you can feed in a refrence to the parent form or page. In your function you can then have a statment similar to:-

    public static void SomeFunction(string input, object thisCaller)
        Page page = thisCaller as Page;
        if (page != null)
//            We've Been called from a ASP.NET Page
Why do you need to know it? I just don't understand. Please explain.
What kind of objects you want to design? Are you talking about business and database objects?
JimBrandleyConnect With a Mentor Commented:
In general, your business objects and data layer objects should neither know nor care. You should be able to uses those assemblies in either kind of application.

If you have some classes in mind where it does make a difference, say utilities of some sort that support the UI directly, then you can pass a boolean in the constructor, or if need be, you acn have one constructor that takes a reference to the current (web) page, and another that takes a reference to the current Windows Form.

Your UI layer is pretty much all one or the other.

All Courses

From novice to tech pro — start learning today.