Solved

Need to access variables within C# class

Posted on 2010-08-15
6
247 Views
Last Modified: 2013-12-27
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();
           
        }
}
0
Comment
Question by:atomicgs12
6 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 33443119
You'd need to declare "hLibModule" and "listener" as STATIC:

    public static IntPtr hLibModule;
    public static MySecondClass listener;
0
 
LVL 27

Expert Comment

by:nmarun
ID: 33443200
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

0
 
LVL 1

Expert Comment

by:funazonki
ID: 33444968
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
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:atomicgs12
ID: 33507758
nmarun: - how does 'listener' in Class MyLocal get reconized?
0
 
LVL 27

Expert Comment

by:nmarun
ID: 33513570
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
0
 
LVL 27

Expert Comment

by:nmarun
ID: 33556256
Did this work for you?

Arun
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Let me explain this picture a little bit.  First, in case you haven't already guessed, you are looking at my 2 phones, an Android Samsung Galaxy S5 on the left and an iPhone 5 on the right.  They are on their respective cradles on my desk.  But, you…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question