In the scope of a "button click" event, I declared a string variable "result". In a for loop within this event, I tried to access "result' but with a compiling error " use of unssigned local variable".
My question is: Why "result" is not visible in the for loop Thanks.
Codes:
private void btnGo_Click(object sender, EventArgs e){
string result; Point newPoint = new Point(1, 1); Circle newCircle = new Circle(2, 2, 0.5); Cylinder newCylinder = new Cylinder(3, 3, 0.5, 4); Shape[] newShape = new Shape[3];newShape[0] = newPoint;
newShape[1] = newCircle;
newShape[2] = newCylinder;
for (int i = 0; i <= newShape.Length - 1; i++){
result += newShape
.ToString() +
}
MessageBox.Show(result);}

a quick question regarding viarable scope
Mirko Messori
Because classes have a constructor. Consder the following (as method variables)
MyClass A;
MyClass B = new MyClass();
In the first, no constructor is called, and A is uninitialized.
In the second, the ctor for MyClass is called, each element of MyClass is initialized, and the value of that object is assigned to B.
So, the behavior is consistent : When a ctor is called, every element of the object is initiallized. When no ctor is caled, nothing is initialized.
Biocide
That's only true for fields (class members).
It is NOT true for local (stack) variables, which MUST be explicitly initialised.
It was that lack of explicit initialisation that provoked the compile error in the OP.
Juraj Borza
Another question:
Why the class variables can be initialized by default values while the method variables cannot be What is the poibt behind this mechanism Can anyone explain to me, plz thanks
xavito
Tang Meister
Dave12349
<nitpicking>
this.theResult.Append(newShape[ i ].ToString() + Environment.NewLine);
By using the + you are creating a new string, which sorta defeats the purpose of using the StringBuilder in the first place. The line should be:
this.theResult.Append(newShape[ i ].ToString());
this.theResult.Append(Environment.NewLine);
</nitpicking>
Tdah
it applies the same method all round I believe. in order to use a variable, you must initialize it. Even if you leave it unintialized, in the back, its initialized by the default values (null).There was a good article about this which I can't find now....
infact...this maybe of interest
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=754958&SiteID=1
Chrispragash
Progalex
Thanks for your reply.
Is this true that :
If a variable is declared within a method, it MUST be initialized before it can be used.
If a variable is declared at the level of class, it dosen't necessarily have to be initialized coz it is done by default (e.t. int to"0", string to "null", bool to "false" etc).
TTris
thats right, you would need to instantiate it. Personally I make sure I instantiate all objects I am going to be using. So...
string result = String.Empty;
then append to the string.
Now going in terms of best practice, you are best using a StringBuilder to append strings as this will not create several instances of the string object in the back, causing memory usage of your app to being increased. So.....
private StringBuilder theResult = new StringBuilder();
...
..
for (int i = 0; i <= newShape.Length - 1; i++)
{
this.theResult.Append(newShape[ i ].ToString() + Environment.NewLine);
}
MessageBox.Show(this.theResult.ToString());
http://msdn2.microsoft.com/en-us/library/system.text.stringbuilder.aspx
does this help