C# and COM Object

T

Trecius

Hello, Newsgroupians:

I have a question that is troubling me. I have a COM / OLE component that I
am adding to my C# project. In creating a wrapper class for the component, I
have declared the class as static as well as every member and function in the
class, which is -- of course -- required.

Well, when trying to use my wrapped class, the first call to the class calls
the static constructor for the class. I only have one line in the static
constructor.

public static class PressureWrapper
{
private static PressureManager m_pmanager = null;

static PressureWrapper()
{
PressureWrapper.m_pmanager = new PressureManager();
}
}

That's all. However, after hanging for about two minutes, the static
constructor finally thows an exception. Two things puzzle me, if the
original OEM program is running, the static constructor works just fine and
my code will continue to execute and not hang. Also, if I don't make the
class or the member variable static, I am able to create the object, and the
program can continue to execute. This is illustrated below:

public class PressureWrapper
{
private PressureManager m_pmanager = null;

public PressureWrapper()
{
this.m_pmanager = new PressureManager();
}
}

public static void main()
{
PressureWrapper pw = new PressureWrapper();
pw.SomeFunc();
}

In this case it works. Any ideas?

Thank you, all.


Trecius
 
W

Willy Denoyette [MVP]

Trecius said:
Hello, Newsgroupians:

I have a question that is troubling me. I have a COM / OLE component that
I
am adding to my C# project.

What kind of component is this?

In creating a wrapper class for the component, I
have declared the class as static as well as every member and function in
the
class, which is -- of course -- required.

Well, when trying to use my wrapped class, the first call to the class
calls
the static constructor for the class. I only have one line in the static
constructor.

public static class PressureWrapper
{
private static PressureManager m_pmanager = null;

static PressureWrapper()
{
PressureWrapper.m_pmanager = new PressureManager();
}
}

That's all. However, after hanging for about two minutes, the static
constructor finally thows an exception.

What exception?

Two things puzzle me, if the
original OEM program is running, the static constructor works just fine
and
my code will continue to execute and not hang.

What OEM program are you talking about?

Also, if I don't make the
class or the member variable static, I am able to create the object, and
the
program can continue to execute. This is illustrated below:

public class PressureWrapper
{
private PressureManager m_pmanager = null;

public PressureWrapper()
{
this.m_pmanager = new PressureManager();
}
}

public static void main()
{
PressureWrapper pw = new PressureWrapper();
pw.SomeFunc();
}

In this case it works. Any ideas?

It should work in any case.

Willy.
 
D

DSK Chakravarthy

Dude,

You are confusing yourself with the singleton pattern. Read the singleton
pattern once before you implement a private static variable of the parent
class type.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top