473,326 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

do constructor logic in class having only static variables & methods

TS
I am trying to add an event handler for a static variable of a class and i
can't figure out how to do so since the class is never instantiated.

The wmp variable gets instantiated when my code calls one of my static
methods (not listed) for the first time. Any help is greatly appreciated
public class MediaHelper

{

private static WindowsMediaPlayer wmp = new WindowsMediaPlayerClass();

//I need to do this somewhere: wmp.PlayStateChange += new
Microsoft.MediaPlayer.Interop._WMPOCXEvents_PlaySt ateChangeEventHandler(Play
er_PlayStateChange);

private static void Player_PlayStateChange(int stateValue)
{string ss;
if( stateValue == 3)
ss = stateValue.ToString();
}

}
Nov 16 '05 #1
4 1796
In article <Ow**************@tk2msftngp13.phx.gbl>, TS wrote:
I am trying to add an event handler for a static variable of a class and i
can't figure out how to do so since the class is never instantiated.

The wmp variable gets instantiated when my code calls one of my static
methods (not listed) for the first time. Any help is greatly appreciated


public class MediaHelper
{
private static WindowsMediaPlayer wmp;

static MediaHelper ()
{
wmp = new WindowsMediaPlayerClass ();
//I need to do this somewhere:
wmp.PlayStateChange +=
new Microsoft.MediaPlayer.Interop._WMPOCXEvents_PlaySt ateChangeEventHandler(Player_PlayStateChange);
}

private static void Player_PlayStateChange(int stateValue)
{
string ss;
if( stateValue == 3)
ss = stateValue.ToString();
}
}

HTH
--
Tom Shelton [MVP]
Nov 16 '05 #2
TS
that worked, thanks

So is this called a static constructor? When I set a breakpoint in my
original code when wmp was instantiated just below the class declaration,
this line would be hit the first time the first static method was called. It
is just confusing to me how this happens.

I noticed you didn't put a public or private before the "static
Mediahelper()", any reason for doing so?

"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:Of**************@TK2MSFTNGP15.phx.gbl...
In article <Ow**************@tk2msftngp13.phx.gbl>, TS wrote:
I am trying to add an event handler for a static variable of a class and i can't figure out how to do so since the class is never instantiated.

The wmp variable gets instantiated when my code calls one of my static
methods (not listed) for the first time. Any help is greatly appreciated
public class MediaHelper
{
private static WindowsMediaPlayer wmp;

static MediaHelper ()
{
wmp = new WindowsMediaPlayerClass ();
//I need to do this somewhere:
wmp.PlayStateChange +=
new

Microsoft.MediaPlayer.Interop._WMPOCXEvents_PlaySt ateChangeEventHandler(Play
er_PlayStateChange); }

private static void Player_PlayStateChange(int stateValue)
{
string ss;
if( stateValue == 3)
ss = stateValue.ToString();
}
}

HTH
--
Tom Shelton [MVP]

Nov 16 '05 #3
TS <ma*********@311.com> wrote:
that worked, thanks

So is this called a static constructor? When I set a breakpoint in my
original code when wmp was instantiated just below the class declaration,
this line would be hit the first time the first static method was called. It
is just confusing to me how this happens.
From the spec (section 17.11 with ECMA numbering):

<quote>
The static constructor for a class executes at most once in a given
application domain. The execution of a static constructor is triggered
by the first of the following events to occur within an application
domain:

* An instance of the class is created.
* Any of the static members of the class are referenced.
</quote>
I noticed you didn't put a public or private before the "static
Mediahelper()", any reason for doing so?


Static constructors neither require nor allow access modifiers, as they
can't be called directly anyway.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
TS
thank you!

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
TS <ma*********@311.com> wrote:
that worked, thanks

So is this called a static constructor? When I set a breakpoint in my
original code when wmp was instantiated just below the class declaration, this line would be hit the first time the first static method was called. It is just confusing to me how this happens.


From the spec (section 17.11 with ECMA numbering):

<quote>
The static constructor for a class executes at most once in a given
application domain. The execution of a static constructor is triggered
by the first of the following events to occur within an application
domain:

* An instance of the class is created.
* Any of the static members of the class are referenced.
</quote>
I noticed you didn't put a public or private before the "static
Mediahelper()", any reason for doing so?


Static constructors neither require nor allow access modifiers, as they
can't be called directly anyway.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Neil Zanella | last post by:
Hello, It seems to me that using too many variables at class scope in C++ (e.g. private data members) can be just as bad as having a C program with lots of global variables. This is especially...
8
by: Piro | last post by:
I have a class that I want to make accessible to a web service. This class does some work in its constructor method and sets some class variables in its various methods. The problem I am having...
19
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and...
10
by: Rene | last post by:
I jus realized that I can change the values of "static variables" and "instance variable" through the standard constructor This means that something like this will compile: public class...
5
by: | last post by:
Hi, How long do webservice objects live for? In particular, if i have static variables filled with data from a static constructor in a webservice, how long will that data persist? thxs
40
by: Sek | last post by:
Is it appropriate to throw exception from a constructor? Thats the only way i could think of to denote the failure of constructor, wherein i am invoking couple of other classes to initialise the...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
6
by: =?Utf-8?B?TWF0dA==?= | last post by:
I'm having a problem with a static class constructor being called twice. I have the static class MasterTaskList which uses a BackgroundWorker to execute multiple methods on a separate thread. The...
5
by: Dave | last post by:
Hello, Suppose you have a class with a static property with only a get (read only). You also have code in a static constructor that sets these properties but takes 1 hour to run. Now suppose...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.