Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# class not public

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?

like image 390
Joel Kennedy Avatar asked Jan 23 '26 00:01

Joel Kennedy


2 Answers

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();
    }
}
like image 159
Grant Crofton Avatar answered Jan 24 '26 21:01

Grant Crofton


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();
}
like image 21
skyfoot Avatar answered Jan 24 '26 20:01

skyfoot