Solved

Console App able to have property?

Posted on 2008-11-02
6
206 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 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

21 Experts available now in Live!

Get 1:1 Help Now