views:

202

answers:

5

I am trying to make a class so when I do the following inside a file:

Functions LoginFunctions = new Functions();
LoginFunctions.loadFunctions();

It will create my object which I need, and make it public so every form which calls the class will be able to use it. The class file is below.

namespace App
{
    public class Functions
    {
        public void loadFunctions()
        {
            TaskbarItemInfo taskbarItemInfo = new TaskbarItemInfo();

        }
    }
}

It doesn't seem to be making the taskbarItemInfo object public, and it is not letting me use it anywhere else other then inside the class. How do I make it public so every file that calls the class can use the object?

+2  A: 

In the example you provide, taskbarItemInfo is declared within the local scope of the loadFunctions() method. If you want it to be public for some class, you must make it a class member before you can make it public.

Andrew Flanagan
+4  A: 

Your taskbaritem class is in the scope of the method and therefore you wont be able to access it outsite of the class.

Create a public property or return it in the method.

    namespace App
    {
        public class Functions
        {
            private TaskbarItemInfo _taskbarItemInfo;

            public TaskbarItemInfo taskbarItemInfo
           {
               get
              {
                   return _taskbarItemInfo;
              }
           }

            public void loadFunctions()
            {
                _taskbarItemInfo = new TaskbarItemInfo();

            }
        }
    }

I would also go and change the loadFunctions method to a constructor which creates all the objects you need.

public Functions()
{
    _taskbarItemInfo = new TaskbarItemInfo();
}
skyfoot
This is *not* a property but a public *field*! Properties would be the way to go here.
0xA3
@0xA3, your right, edited.
skyfoot
You could just use `public TaskbarItemInfo TaskbarItemInfo { get; protected set; }`.
Jaroslav Jandek
+2  A: 

You need to make the variable public.

namespace App
{
    public class Functions
    {
        public TaskbarItemInfo TaskbarItemInfo { get; private set; }

        public void loadFunctions()
        {
            TaskbarItemInfo = new TaskbarItemInfo();
        }
    }
}

EDIT: You could also do the initialization of the items in the constructor.

namespace App
{
    public class Functions
    {
        public TaskbarItemInfo TaskbarItemInfo { get; private set; }

        public Functions() 
        {
            loadFunctions();
        }

        private void loadFunctions()
        {
            TaskbarItemInfo = new TaskbarItemInfo();
        }
    }
}

Then you don't need the LoginFunctions.loadFunctions(); line of code after you initialize your LoginFunctions object.

Chad
Please don't. Use properties!
0xA3
0xA3, I'm not questioning you, but why use properties instead of a public variable? Just curious. Thanks!
drpcken
I recommend you Jon's article on the matter: [Why Properties Matter](http://csharpindepth.com/Articles/Chapter8/PropertiesMatter.aspx). It might not be relevant for the sample given by the OP, but after all we should be teaching best practices here :-)
0xA3
I agree 0xA3. I'm always for learning best practices :)
drpcken
@0xA3, fixed... should have wrote it like that in the first place
Chad
+5  A: 

As the others have mentioned, make it a property, for example like so:

public class Functions
{
    public TaskbarItemInfo TaskbarItemInfo { get; private set; }

    public void loadFunctions()
    {
        this.TaskbarItemInfo = new TaskbarItemInfo();
    }
}
Grant Crofton
+1  A: 

You probably want to access it as a property which generates a private static member when needed.

namespace App
{
    public class Functions
    {
        private static TaskbarItemInfo _taskbarItemInfo;

        public static TaskbarItemInfo TaskBarItemInfoProperty
        {
            get{
               if (_taskbarItemInfo == null) 
               {
                  _taskbarItemInfo = new TaskbarItemInfo();
               }
               return _taskbarItemInfo;
            }
        }
    }

    public class Test
    {
        public void testFunction()
        {
           Functions.TaskBarItemInfoProperty.doSomething();
        }
    }
}
Kimball Robinson