Reggie:
        You don't need to instantiate the class to initialize its static
members. You just need to use some of its members in your code. The
static constructor is called when the class is loaded and it will be
loaded only if it's needed by your call.

        Here is a small sample that initializes a seed using the Random
class. If you comment the DoSomething() method call in Main, it won't
load StaticClass nor execute its static constructor.

using System;

namespace DotNetList.FedeR
{
        class StaticClass
        {
                static float seed;

                static StaticClass()
                {
                        Console.WriteLine("StaticClass Initializing");
                        Random r = new Random();
                        seed = r.Next();
                }

                public static void DoSomething()
                {
                        Console.WriteLine("DoingSomething: Seed="+seed);
                }
        }

        class myApp
        {
                static void Main()
                {
                        Console.WriteLine("Entering Main");
                        // If you comment this line, constructor won't
be called
                        StaticClass.DoSomething();
                }
        }
}

Federico Raggi
Latam Developers Initiative Manager
Microsoft
 
Phone: (954)489-4862
Mobile: (954)465-4862

> -----Original Message-----
> From: Reggie Burnett [mailto:[EMAIL PROTECTED]]
> Sent: Friday, May 24, 2002 10:58 AM
> To: [EMAIL PROTECTED]
> Subject: Re: [DOTNET] Automatically executing code
> 
> That's what I thought. I tried using a static constructor but you have
> to explicitly create an object to get it to fire.  I'm looking for
> something that will automatically run the code to register  my prefix
> without the user of the lib having to do anything.  Their first line
of
> code should be something like
> 
> Wr = WebRequest.Create("blah blah url");
> 
> Is there something at the assembly level or an attribute that I could
> use to automatically fire some code at app startup?
> 
> Reggie
> 
> > -----Original Message-----
> > From: dotnet discussion [mailto:[EMAIL PROTECTED]] On
Behalf
> Of
> > Richard Birkby
> > Sent: Friday, May 24, 2002 4:21 AM
> > To: [EMAIL PROTECTED]
> > Subject: Re: [DOTNET] Automatically executing code
> >
> > Can you use a static constructor somewhere?
> > System.Net registers HTTP, HTTPS and FILE the first time the prefix
> list
> > is
> > used *.
> >
> >
> > Richard
> > * It uses a double check locking idiom without a volatile keyword...
> >
> > > -----Original Message-----
> > > From: dotnet discussion [mailto:[EMAIL PROTECTED]]On
Behalf
> Of
> > > Reggie Burnett
> > > Sent: 24 May 2002 01:17
> > > To: [EMAIL PROTECTED]
> > > Subject: [DOTNET] Automatically executing code
> > >
> > >
> > > I'm implementing a webrequest/webresponse pair and the docs say
that
> you
> > > have to call WebRequest.RegisterPrefix to register your creator
> object
> > > with that prefix.  What is the best way to make that call
> automatically
> > > without the user of the library needing to explicitly do that?
> > >
> > > Reggie
> > >
> > > You can read messages from the DOTNET archive, unsubscribe from
> DOTNET,
> > or
> > > subscribe to other DevelopMentor lists at
> http://discuss.develop.com.
> >
> > You can read messages from the DOTNET archive, unsubscribe from
> DOTNET, or
> > subscribe to other DevelopMentor lists at
http://discuss.develop.com.
> 
> You can read messages from the DOTNET archive, unsubscribe from
DOTNET, or
> subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to