Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • 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 ThakurCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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