Solved

Need to access variables within C# class

Posted on 2010-08-15
6
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 86

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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Preface: This article is part of a series focused on cross platform mobile app development (specifically Android and iOS) using the Alloy framework and Titanium Studio made by Appcelerator (https://www.appcelerator.com/). This article presumes a wor…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Suggested Courses

738 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