Solved

Need to access variables within C# class

Posted on 2010-08-15
6
243 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

With Windows Embedded Handheld, called Windows Mobile, Microsoft re-designed the user interface. The Start Icon moved down to the bottom, inside the menu bar area.   If you need to hide the Start Icon and/or the SIP (soft input panel, softwar…
Once again I push the limits of my phone.  An introduction to the Android Google Now Launcher.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now