0

I've recently written this short application in C#:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Checker { class Program { static void Main(string[] args) { Properties.Settings.Default.Save(); Program re = new Program(); re.next(); } public void next() { Console.WriteLine("Have you already entered name?"); int ch = int.Parse(Console.ReadLine()); if (ch == 0) { Console.WriteLine("What is your name?"); String name = Console.ReadLine(); Console.WriteLine("Thank you!"); Console.ReadKey(); } Console.WriteLine("Your name is " + name); } } } 

Now, I've created a settings file, and created a "name" variable there, with the "String" type.

The scope of it is "User".

So I want it to load the "name" variable with the properties line, but I can't even compile the program because of this error:

Error 1 The name 'name' does not exist in the current context 

How can I solve it?

1
  • What does Properties.Settings has to do with your current code ? Commented Oct 10, 2013 at 17:42

3 Answers 3

4

The answer to your problem becomes a little more apparent when you indent:

String name; if (ch == 0) { Console.WriteLine("What is your name?"); name = Console.ReadLine(); Console.WriteLine("Thank you!"); Console.ReadKey(); } else { name = Settings.Default.name; } Console.WriteLine("Your name is " + name); 

Now you can see that you defined a String called name inside the if-block, thus using it in the Console.WriteLine outside the if-block is out of scope! Move that last Console.WriteLine inside the if-block to solve the scoping issue.


Edit: Based on your comment, your code needs a bit more logic to attain what you're trying to do. I updated my snippet above to accomplish what I think you're trying to do.

Sign up to request clarification or add additional context in comments.

8 Comments

But I want to display the saved name, and not the new name that the user enters.
I made the if block to ask if the user had already entered a name. If he did, he'll see the name he entered (which is the variable "name" which should've been retrieved).
@BarSharabani It's pretty clear you don't fully understand what Properties.Settings.Default.Save(); does. As it is, your variable name has absolutely no interaction/relation whatsoever with the settings you're saving. Please read: msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx
Also, @ruslander, how do you know he even has name defined or even stored in his settings?
@tnw I'm just basing that on his original post, in which he said "Now, I've created a settings file, and created a "name" variable there, with the "String" type." I'm assuming he has indeed created this correctly.
|
0

You declared that variable inside the if block.
As the compiler is trying to tell you, it doesn't exist outside that block.

If you want to use your Settings class, write Settings.Default.Name.

Comments

0

Your intentions are a bit unclear, but to me it seems as though you are trying to have the application display the name of the user if it has already been saved or ask for it if it hasn't. If that is the case, something like this should work:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Checker { class Program { static void Main(string[] args) { Program re = new Program(); re.next(); Properties.Settings.Default.Save(); } public void next() { String name = Settings.Default.name; if (String.IsNullOrEmpty(name)) { Console.WriteLine("What is your name?"); name = Console.ReadLine(); Settings.Default.name = name; Console.WriteLine("Thank you!"); Console.ReadKey(); } else { Console.WriteLine("Your name is " + name); } } } } 

In your OP, your settings were not being saved before the program exited nor were you setting the name property.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.