Solved

Console App able to have property?

Posted on 2008-11-02
6
205 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Linq help 1 16
Duplicate a row 2 33
Image(2) 3 26
index Out OF Range Exception error 4 31
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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!
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now