[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

Console App able to have property?

Hi,

Can my console app have a property? or is there another way i can have a global type variable?

The code below fails to build.

thanks,


class Program
{
	internal StringBuilder MessageLog = new StringBuilder();
	static void Main(string[] args)
	{
		this.Messagelog.AppendLine("Hello World");
	}
}

Open in new window

0
Valimai
Asked:
Valimai
2 Solutions
 
Anurag ThakurTechnical ManagerCommented:
yes your console application can definitely have a property defined but from where and how you want to access it
can you please state what your actual requirement is so that we can help you out better
0
 
DhaestCommented:
Example of how you can do it ...
namespace ConsoleApplication3
{
 
    class Program
    {
        class GlobalVars
        {
            public StringBuilder MessageLog = new StringBuilder();
        }
 
        static void Main(string[] args)
        {
            GlobalVars myGlobalVariables = new GlobalVars();
            myGlobalVariables.MessageLog.Append("Test");
        }
    }
}

Open in new window

0
 
philipjonathanCommented:
Another way you can do it, add 'static' keyword:
internal static StringBuilder MessageLog = new StringBuilder();
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
philipjonathanCommented:
Uh, and use Program to refer to the property instead of 'this':
Program.Messagelog.AppendLine("Hello World");
0
 
ValimaiAuthor Commented:
Hi Philip,

I used
internal static StringBuilder MessageLog = new StringBuilder();
and refered to it as just
MessageLog.AppendLine();

thank you very much.
0
 
philipjonathanCommented:
Yes, you can do that as long as you are referring to the MessageLog from within the Program class scope
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now