Welcome back.
In this video I want to show you something really important.
And that is the scope of a variable.
So the scope of a variable depends on where the variable was created.
So if we look at our code here okay.
So we have this int h that we are asking inside of this if statement.
This means that this int h will not be available outside of this if statement okay.
So that means that if I want to for example, access the h here.
So h equals.
You see I don't I can't even enter the age.
You see it automatically gives me aggregate exception.
So this H variable doesn't exist outside of its scope which is this curly brackets all the way to this
curly brackets down here.
So that's starting from here.
That's basically where age exists.
Which is why we can only use age here.
So how can I now make sure that whatever age is asked in here is also usable after that?
So down here for example, therefore what I can do is I can create a variable that is outside of this
if statement.
So h equals zero.
And then here instead of saying int h I'm just going to say age.
So what I do here now is I assign a value to H.
I don't create a new variable called h.
The variable called age is up here.
Okay.
So now that it is outside of this if statement, the scope is basically our entire entire program.cs
file.
I can use it down here.
I can just say age equals zero for example.
So whatever the user has entered at this point will now be reset at that moment, and the age will be
set back to zero.
Okay.
The same goes for our string address.
So for example the string address is inside of our if statement here inside of the if statement here.
So it's only available inside of this nested if statement.
So only down here will this variable be available.
So at this point I can access the address okay.
This is this local variable.
It says a local variable.
So now if I try to access the address down here inside of this else block okay.
So I try to access address I can't do that.
It doesn't work because in this context when this scope so to speak, the address does not exist.
Same goes down here.
I cannot use the address.
So if I were to use the address outside of my if statement, I would need to set it up.
And the best way would be up here.
So I could set up the address and it would be an empty string like so, for example.
And then here I need to get rid of this string statement, because this here is where I create a variable
of type string that is called address.
If I get rid of it, I'm assigning to the existing address variable a value.
All right.
So that's something I wanted to show you.
Scope is a very tricky thing that can lead to errors and problems if you don't take care of scope accordingly.
But now you have seen how variables work, which is why very often variables are just created at the
top of your file, because then they can be used throughout the entire file.
So entire program.cs file.
However, if you know that the variable that you are creating will only be created and used inside of
this very limited context, inside of this very small scope, you don't need to create it globally or
outside of your if statement because for example with the address.
So going back to having the address down here like this, it would make sense to do it this way because
the address will only be requested in this particular case.
So if our program runs and never even gets to that point, then our computer or our processing unit
won't even need to think about setting up the address because it doesn't exist anywhere.
And also the location, the storage, so to speak, that would be allocated for a variable that we set
up here at the top would be useless.
But if we set it up here and our code never gets to that point because the H never is above 18, then
this would never be executed and we would save ourselves a few bits.
Okay, it's not that crazy, but still like, uh, considering a very big software, this can make a
difference.
Okay, so that's it for this video.
See you in the next one.