Results 1 to 6 of 6

Thread: Strange scope problem .. ?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    45

    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

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    45

    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.

  5. #5
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    45

    Re: Strange scope problem .. ?

    I see..well only if it's specific to this error code because I get plenty of other warnings

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width