Form-Aware Objects

Posted on 2007-10-19
Last Modified: 2010-04-15

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?
Question by:NigelRocks
    LVL 8

    Expert Comment

    Why do you need to know it? I just don't understand. Please explain.
    LVL 2

    Expert Comment

    What kind of objects you want to design? Are you talking about business and database objects?
    LVL 22

    Assisted Solution

    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.

    LVL 16

    Accepted Solution

    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

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now