Solved

Need to access variables within C# class

Posted on 2010-08-15
6
248 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 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
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…

756 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