RE: inheritance trouble - how to detect run-time or design-time in code

From: Steven Cheng[MSFT] (v-schang_at_online.microsoft.com)
Date: 03/30/04


Date: Tue, 30 Mar 2004 08:00:16 GMT

Hi PearCZ,

>From your description, you made a simple base page class and make some
certain pages derived from it. The base class will throw a exception its
construnctor according to some certain configure datas in the web.config
file. However you found that after the first the derived page run and
throws the exception, I'll no longer be able to load the derived page into
VS.NET's design view again, yes?

As for this problem, I've tested following the steps you provided on my
side and did repro the problem you mentioned. In fact, this problem is
because the VS.NET will also run the page at design time(when you double
click the page to show its design) , it'll call the page class's
constructor, OnInit method ...etc. That's why when you closed the design
view window and open it again, you'll get the exception too. And normally,
if the certain code is in Page_Init or Page_Load event, you can use a
certain codeline to detect whether it is design time or runtime so as to
make sure the code only be called at runtime, for example:
Protected Overrides Sub OnInit(ByVal e As EventArgs)
        InitializeComponent()
        MyBase.OnInit(e)

        Dim com As System.ComponentModel.IComponent = CType(Me,
System.ComponentModel.IComponent)
        If Not com.Site.DesignMode Then
            GetWebConfig()
        End If

  End Sub 'OnInit

However, as for the Constructor, we're unable to use the above means to
get the mode info. So currently we have the following means to workaround
the problem:
1. When developing the page, we comment the certain code which throws the
exception in the construcutor and uncomment it when deploy them.

2. Move the code into page's OnInit method and use the
Dim com As System.ComponentModel.IComponent = CType(Me,
System.ComponentModel.IComponent)
If Not com.Site.DesignMode Then
            GetWebConfig()
End If

To confirm that the certain code(throws exception) only be called at
runtime rather than design time. Please check out the above things, if you
have anything unclear, please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx



Relevant Pages

  • Re: is such exception handling approach good?
    ... There is nothing wrong with throwing from constructor. ... It may be a good design, it may not be a good design from user's point ... resource API to free-up the resource on exception. ... change something - that is not initialization. ...
    (microsoft.public.vc.language)
  • Re: is such exception handling approach good?
    ... It may be a good design, it may not be a good design from user's point ... doesn't get the connection created automatically on object creation. ... instance you use a class somebody else wrote which throws an exception ... constructor code would be shared... ...
    (microsoft.public.vc.language)
  • Re: exiting a constructor early
    ... the exception object, and to 're-throw' a different object if you ... the body of the constructor - the handler is for handling the ... exception object - the constructor body need not do it. ... | at your design and ask yourself why that is the case. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: is such exception handling approach good?
    ... There is nothing wrong with throwing from constructor. ... an oracle database in the ctor then what should you do with the object ... It may be a good design, it may not be a good design from user's point ... resource API to free-up the resource on exception. ...
    (microsoft.public.vc.language)
  • Re: Class in another file
    ... > oriented design is that it is a technique that focuses design on ... programmer did was: design a way of using a structure of standard ... different but related object (i.e. what's now called a "constructor ... that's not as nice as true parameter overloading that Common Lisp ...
    (comp.lang.java.programmer)

Quantcast