Link to home
Start Free TrialLog in
Avatar of atomicgs12
atomicgs12Flag for United States of America

asked on

Need to access variables within C# class

In the code below I want to access hLibModule and listner through out the code within the namespace, MyClass. I am getting "Cannot access a non-static member of outer type MyClass.Program' via nested type MyClass.Program.Local' error as the code stands. Can someone please help?

namespace MyClass
{
    public class Program
    {
        IntPtr hLibModule;
        MySecondClass listener;

        public class MyLocal
        {
            public void Init()
            {
                hLibModule = LoadLibrary("MyDLL.dll");
                if (DllRegisterServer() != 0)
                {
                    // MessageBox.Show("Could not initialize the SMSTrap DLL");
                }
                else
                {
                    listener = new MySecondClass ();

                }

            }
        }

        static void Main(string[] args)
        {
            // start SMS text trapping
            MyLocal lcl = new MyLocal ();
            lcl.Init();
           
        }
}
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm a bit of a traditional programmer in the sense that I like things to be clear and distinct. So, is there a need for you to have nested classes? If not, keep them separate (and in separate files as well).

The way I'd write the Init method is that it would return a IntPtr. This way, my code looks lot more cleaner.

Arun

public class Program
{
    static IntPtr hLibModule;
    static MySecondClass listener;

    static void Main(string[] args)
    {
        // start SMS text trapping
        MyLocal lcl = new MyLocal();
        hLibModule = lcl.Init();

    }

}

public class MyLocal
{
    public IntPtr Init()
    {
        IntPtr hLibModule = LoadLibrary("MyDLL.dll");
        if (DllRegisterServer() != 0)
        {
            // MessageBox.Show("Could not initialize the SMSTrap DLL");
        }
        else
        {
            listener = new MySecondClass();

        }

        return hLibModule;

    }
}

Open in new window

If you want to hide hLibModule from the rest of the app, then the declaration structure is reasonable. In which case simply using

 static IntPtr hLibModule;

will work. Watch out for multi-threading though. If more than one thread can access hLibModule and the code it leads to is not re-entrant you will need to put lock statements round the access
Avatar of atomicgs12

ASKER

nmarun: - how does 'listener' in Class MyLocal get reconized?
Try using 'out' keyword and pass a type of the MySecondClass to the Init method.

hLibModule = lcl.Init(out listener);

public IntPtr Init(MySecondClass listener)
{
    //...
}

Here's more information on the 'out' and 'ref' keywords:
http://geekswithblogs.net/ftom/archive/2008/09/10/c-and-the-difference-between-out-and-ref.aspx

Arun
Did this work for you?

Arun