Solved

Console App able to have property?

Posted on 2008-11-02
6
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

738 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