Unable to access a variable declared inside an if statement (Error: is shadowing variable)

BoQsc vaidas.boqsc at gmail.com
Wed May 27 11:03:51 UTC 2020


I'm lacking knowledge on how to achieve what I want and getting 
an error.
What is the correct way to do what I tried to achieve in this 
code?
Everything was intuitive until I started to add notice variable 
to the writeln. Rdmd says  variable `notice` is shadowing 
variable.

rdmd output:
> C:\Users\vaida\Desktop>rdmd searchDirectory.d
> searchDirectory.d(21): Error: variable `notice` is shadowing 
> variable `searchDirectory.main.notice`
> Failed: ["C:\\D\\dmd2\\windows\\bin\\dmd.exe", "-v", "-o-", 
> "searchDirectory.d", "-I."]

searchDirectory.d
> import std.stdio : writeln;
> import std.file;
> void main(){
> 
> 	auto drivesLetters = [
> 		"A:", "I:", "Q:", "Y:",
> 		"B:", "J:", "R:", "Z:",
> 		"C:", "K:", "S:", 		"D:", "L:", "T:",
> 		"E:", "M:", "U:",
> 		"F:", "N:", "V:",
> 		"G:", "O:", "W:",
> 		"H:", "P:", "X:",
> 	];
> 	
> 	foreach (string driveLetter; drivesLetters) {
> 		string notice;
> 		if (driveLetter.exists){
> 			auto directory = "/Backup";
> 			if ((driveLetter ~ directory).exists){
> 				auto notice = "Backup directory exists.";
> 				
> 			}
> 			writeln(driveLetter, notice);
> 		}
> 		
> 	}
> 
> 	
> }



More information about the Digitalmars-d-learn mailing list