2

I'm New to C# and I'm trying to reach the value of MAX from the while so i can use it outside but i can't...anyone have some ideas !!! Thanks In Advance

while (Condition)
{    
    Double MAX = somecode.....
                         .....
}

Console.WriteLine("The OPTIMAL Value : " + MAX); 
5
  • 2
    Create it outside of the while loop. Is there any need to initiate it inside the while loop? If not, initiate it outside Commented Mar 24, 2013 at 0:07
  • 2
    Just a bit of terminology. "from outside (C#)" is called "from outside scope" outside scope { inside scope } the outer scope has access to the inner scope but not the other way around. Commented Mar 24, 2013 at 0:10
  • 1
    woops, ofc I had to screw that up. It's: "the inner scope has access to the outer scope but not the other way around". What that means is that the inner scope can use variables declared in the outer scope, but the outer scope cannot access the variables declared in the inner scope Commented Mar 24, 2013 at 0:20
  • I'll never understand why people down vote a question like this. The asker is new to the language and is looking for guidance. Commented Mar 24, 2013 at 0:20
  • Gladly, have fun and enjoy C#! Commented Mar 24, 2013 at 0:35

4 Answers 4

7

Declare MAX before you start the while loop. The way you have it you can only access within the while.

Double MAX = 0;
while (Condition)
            {    

                MAX = somecode.....
                                      .....
            }

            Console.WriteLine("The OPTIMAL Value : " + MAX); 
Sign up to request clarification or add additional context in comments.

3 Comments

what if i want to make change to the value of MAX inside the while loop ,the changes wont apply to the value !
Not really sure what you mean. If you change the value of MAX inside of the while loop, and then reference it after the loop, the value you set inside of the while is valid outside as well, as long as you've declared is before entering the while.
Remember to declare and set a value to it. If you just do Double Max; you will still get the Use of unassigned local variable error. (sorry if this obvious, but I had problems with this)
3

You must declare the variable BEFORE the loop.

Double MAX;
while (Condition)
{
    MAX = somecode....
}
Console.WriteLine("The OPTIMAL Value : " + MAX);

Comments

1

It would seem the underlying problem is understanding how scope works. A google search for "C# how scope works" (or similar) might prove helpful.

I found one that's quite simple and easy to understand: http://www.codecandle.com/Articles/191/Csharp/Variables/Variable-scope/codedetail.aspx

So as many others have mentioned you'll need to declare your variable outside your inner scope in order to have access to the changes.

Some pseudo code

// declare variable;
{
  // change variable;
}
// use changed variable

1 Comment

Sir I wanna add my datum to datagrid in another class(Page) that datum in while. Could you give me any idea to do that ?My While Loop Thank you so much
0

Declare MAX as a variable outside of the loop for example change the variable name also don't use reserved words as variable names

var dMax = default(double);//this is equivalent to writing Double dMax = 0 when debugginb it will give you this value 0.0

while (Condition)
{    
    dMax = somecode.....
}
Console.WriteLine("The OPTIMAL Value : " + dMax); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.