Strange scope problem .. ?
Hello. I am receiving a compile error:
Use of unassigned local variable 'defBrush' (CS0165)
How is defBrush out of scope? Looks to be in perfect scope and this doesn't make any sense to me. The instance that it is barking at is highlighted in red below. Funny how it doesn't bark at the previous usages of defBrush in this loop. I have tried instantiating defBrush inside the do-while loop as well and it doesn't work. The only place it works is directly inside the 'if' block it's barking at. Appreciate any help.
Code:
Brush defBrush;
this.defectList.refDef = this.defectList.head;
do
{
xPos = ((mapSpacePixelWidth / this.mapWidth) * this.defectList.refDef.crosswebPos); // Pixel location
yPos = ((mapSpacePixelLength / this.mapLength) * this.defectList.refDef.downwebPos); // Pixel location
defType = this.defectList.refDef.type;
if (defType.Contains("Trans"))
defBrush = new SolidBrush(Color.Yellow);
else if (defType.Contains("Refl"))
defBrush = new SolidBrush(Color.Red);
else if (defType.Contains("Inspection"))
defBrush = new SolidBrush(Color.Violet);
else if (defType.Contains("Coating"))
defBrush = new SolidBrush(Color.Cyan);
else if (defType == "Splice")
defBrush = new SolidBrush(Color.White);
else if (defType == "Bar Mark")
defBrush = new SolidBrush(Color.Silver);
else
MessageBox.Show("New Type discovered: " + defType);
if (defType.Contains("vSml"))
this.gfxMap.FillEllipse(defBrush, xPos+this.mapMarginLeft-2, yPos+this.mapMarginTop-2, 4, 4);
else if (defType.Contains("Sml"))
this.gfxMap.FillEllipse(defBrush, xPos+this.mapMarginLeft-4, yPos+this.mapMarginTop-4, 8, 8);
else if (defType.Contains("Med"))
this.gfxMap.FillEllipse(defBrush, xPos+this.mapMarginLeft-6, yPos+this.mapMarginTop-6, 12, 12);
else if (defType.Contains("Lrg"))
this.gfxMap.FillEllipse(defBrush, xPos+this.mapMarginLeft-8, yPos+this.mapMarginTop-8, 16, 16);
else if (defType.Contains("Streak"))
this.gfxMap.FillRectangle(defBrush, xPos+this.mapMarginLeft-6, yPos+this.mapMarginTop-11, 12, 22);
else
Re: Strange scope problem .. ?
It is not out of scope. Its just telling you that there is a chance the defBrush will not have been assigned before it is used. Take a look at your if-statements, if none of those statements execute you will not have assigned anything to 'defBrush'.
What you should do is just to set defBrush to some default value in the else-statement.
Re: Strange scope problem .. ?
exactly. what if the if conditions you have, do not get executed at all? defBrush would be null, and you are passing null to a method call... which may use the null object causing a null reference exception.
it is best to instantiate the objects you are planning to use.
Re: Strange scope problem .. ?
Thanks for the answers. Kinda glad to see that the compiler halts on bad programming practice. Might simply expect a warning here instead..but it's nice learning the 'ah-ha's of programming.
Re: Strange scope problem .. ?
well the thing is, you can treat warnings as errors within the compiler settings... you can easily switch that on or off and looks like in your case it is switched on. :)
Re: Strange scope problem .. ?
I see..well only if it's specific to this error code because I get plenty of other warnings