Solved

Console App able to have property?

Posted on 2008-11-02
6
207 Views
Last Modified: 2012-05-05
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
Comment
Question by:Valimai
6 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22865089
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
 
LVL 53

Expert Comment

by:Dhaest
ID: 22865274
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
 
LVL 18

Accepted Solution

by:
philipjonathan earned 250 total points
ID: 22865510
Another way you can do it, add 'static' keyword:
internal static StringBuilder MessageLog = new StringBuilder();
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 18

Assisted Solution

by:philipjonathan
philipjonathan earned 250 total points
ID: 22865514
Uh, and use Program to refer to the property instead of 'this':
Program.Messagelog.AppendLine("Hello World");
0
 
LVL 1

Author Comment

by:Valimai
ID: 22871846
Hi Philip,

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

thank you very much.
0
 
LVL 18

Expert Comment

by:philipjonathan
ID: 22872822
Yes, you can do that as long as you are referring to the MessageLog from within the Program class scope
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question