www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Unable to access a variable declared inside an if statement (Error:

reply BoQsc <vaidas.boqsc gmail.com> writes:
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);
 		}
 		
 	}
 
 	
 }
May 27 2020
parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Wednesday, 27 May 2020 at 11:03:51 UTC, BoQsc wrote:
 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.

 		if (driveLetter.exists){
 			auto directory = "/Backup";
 			if ((driveLetter ~ directory).exists){
 				auto notice = "Backup directory exists.";
 				
 			}
 			writeln(driveLetter, notice);
 		}
Variables only live in a specified scope, starting from where they are declared, and ending when they reach the '}' indicating the end of said scope. In you case, 'notice' only lives inside the if ((driveLetter ~ directory).exists) scope, and doesn't exist outside. In order to fix this, you will need to declare it outside: if (driveLetter.exists) { auto directory = "/Backup"; auto notice = "Backup directory does not exist."; if ((driveLetter ~ directory).exists) { notice = "Backup directory exists."; } writeln(driveLetter, notice); } This also makes it clearer what value 'notice' will have when the backup directory doesn't exist - in your case you haven't assigned it any value in that case. -- Simen
May 27 2020
parent BoQsc <vaidas.boqsc gmail.com> writes:
On Wednesday, 27 May 2020 at 11:13:09 UTC, Simen Kjærås wrote:
 On Wednesday, 27 May 2020 at 11:03:51 UTC, BoQsc wrote:
 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.

 		if (driveLetter.exists){
 			auto directory = "/Backup";
 			if ((driveLetter ~ directory).exists){
 				auto notice = "Backup directory exists.";
 				
 			}
 			writeln(driveLetter, notice);
 		}
Variables only live in a specified scope, starting from where they are declared, and ending when they reach the '}' indicating the end of said scope. In you case, 'notice' only lives inside the if ((driveLetter ~ directory).exists) scope, and doesn't exist outside. In order to fix this, you will need to declare it outside: if (driveLetter.exists) { auto directory = "/Backup"; auto notice = "Backup directory does not exist."; if ((driveLetter ~ directory).exists) { notice = "Backup directory exists."; } writeln(driveLetter, notice); } This also makes it clearer what value 'notice' will have when the backup directory doesn't exist - in your case you haven't assigned it any value in that case. -- Simen
That's correct. Thanks Simen.
 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) {
 		if (driveLetter.exists) {
 				auto notice = "";
 				if ((driveLetter ~ "/Boot").exists) {
 					notice ~= "\\Boot directory exists";
 					notice ~= " ";
 				}
 				if ((driveLetter ~ "/Windows").exists) {
 					notice ~= "\\Windows folder exists";
 					notice ~= " ";
 				}
 				writeln(driveLetter, notice);
 			}
 		
 	}
 
 	
 }
May 27 2020