2

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
  • 2
    Looks more like this should be a static class, with static methods Commented Jul 19, 2010 at 16:10
  • Huh? Are you trying to return a TaskbarItemInfo isntance or creating a static class? TaskbarItemInfo loadFunctions() {...} or List<object> loadFunctions() {...}, public TaskbarItemInfo myTaskbarItemInfo; etc. Commented Jul 19, 2010 at 16:13

5 Answers 5

5

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(); } } 
Sign up to request clarification or add additional context in comments.

Comments

4

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(); } 

2 Comments

This is not a property but a public field! Properties would be the way to go here.
You could just use public TaskbarItemInfo TaskbarItemInfo { get; protected set; }.
2

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.

Comments

2

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.

4 Comments

Please don't. Use properties!
0xA3, I'm not questioning you, but why use properties instead of a public variable? Just curious. Thanks!
I recommend you Jon's article on the matter: Why Properties Matter. It might not be relevant for the sample given by the OP, but after all we should be teaching best practices here :-)
@0xA3, fixed... should have wrote it like that in the first place
1

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(); } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.