I’m very new at this, and I’ve encountered an issue with a variable in an if/else statement in C#. I want to print a line involving this variable to the console, but Visual Studio isn’t recognising that the variable has been defined and thus it’s not working.
using System;
using System.Runtime.CompilerServices;
//built in the most recent visual studio with c#
{
Console.WriteLine("This is just a basic generator, more will be added later.");
//defining colours and patterns
string[] ColourList = new string[] { "Red", "Yellow", "Blue", "White", "Black" };
string[] PatternList = new string[] { "Plain", "Spotted", "Striped", "Royal", "Patchy" };
//picking random colours and patterns
Random rnd = new Random();
int rndClr1 = rnd.Next(ColourList.Length);
int rndClr2 = rnd.Next(ColourList.Length);
int rndPat = rnd.Next(PatternList.Length);
//think i had to do this to make the first part of the if/else work, not sure why
string Colour1 = rndClr1.ToString();
string Colour2 = rndClr2.ToString();
if (Colour1 == Colour2)
{
string finalColour = Colour1;
//if the two colours are the same, then that's the final colour, otherwise mix them using the following rules
}
else if (rndClr1 == 0)
{
if (rndClr2 == 1)
{ string finalColour = "Orange"; }
else if (rndClr2 == 2)
{ string finalColour = "Purple"; }
else if (rndClr2 == 3)
{ string finalColour = "Pink"; }
else
{ string finalColour = "Dark Red"; }
}
else if (rndClr1 == 1)
{
if (rndClr2 == 2)
{ string finalColour = "Green"; }
else if (rndClr2 == 3)
{ string finalColour = "Pale Yellow"; }
else if (rndClr2 == 4)
{ string finalColour = "Brown"; }
else { string finalColour = "Orange"; }
}
else if (rndClr1 == 2)
{
if (rndClr2 == 0)
{ string finalColour = "Purple"; }
else if (rndClr2 == 1)
{ string finalColour = "Green"; }
else if (rndClr2 == 3)
{ string finalColour = "Sky Blue"; }
else { string finalColour = "Navy"; }
}
else if (rndClr1 == 3)
{
if (rndClr2 == 0)
{ string finalColour = "Pink"; }
else if (rndClr2 == 1)
{ string finalColour = "Pale Yellow"; }
else if (rndClr2 == 2)
{ string finalColour = "Sky Blue"; }
else { string finalColour = "Grey"; }
}
else if (rndClr1 == 4)
{
if (rndClr2 == 0)
{ string finalColour = "Dark Red"; }
else if (rndClr2 == 1)
{ string finalColour = "Brown"; }
else if (rndClr2 == 2)
{ string finalColour = "Navy"; }
else { string finalColour = "Grey"; }
}
else
{ string finalColour = "Well, I don't know how this happened, but the colour generator broke."; }
//error catcher
Console.WriteLine("The creature has this pattern: " + PatternList[rndPat] + ", and its fur is this colour: " + finalColour + ".");
Console.ReadKey();
//for some reason, vs doesn't recognise finalColour as a variable that's already been defined, so the testing fails
//i don't know why
}
In theory, finalColour should be a usable string that can be part of the printed sentence. However, VS keeps saying "The name ‘finalColour does not exist in the current context’, which is an error that prevents testing/compiling. I have no clue why! I feel like I’m missing something obvious, but I’m new enough at coding that I simply don’t know what that something is. Does anyone more experienced have any ideas?
2
Answers
It’s not VS that does not recognize
finalColor
but .NET. And the reason is thatfinalColor
is code-block level scoped (see https://www.tutorialsteacher.com/articles/variable-scopes-in-csharp) and hence it cannot be seen outside the code block where it has been created. The solution is to declare it outside of it, just before your mainif
and make sure that you do not redeclare it, so in the inner cases remove thestring
keyword and it should be good.When define a variable in a block , so can not use it’s to another block , because it’s not global.
First define variable after change it’s:
New code :