I have a list within my Main()
and I'm trying to add an item to that list from within a variable. But it's throwing the error "The name 'dogList' does not exist in the current context"
Inside my addDog()
method, dogList.Add()
is not working due to above.
namespace DoggyDatabase
{
public class Program
{
public static void Main(string[] args)
{
// create the list using the Dog class
List<Dog> dogList = new List<Dog>();
// Get user input
Console.WriteLine("Dogs Name:");
string inputName = Console.ReadLine();
Console.WriteLine("Dogs Age:");
int inputAge = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Dogs Sex:");
string inputSex = Console.ReadLine();
Console.WriteLine("Dogs Breed:");
string inputBreed = Console.ReadLine();
Console.WriteLine("Dogs Colour:");
string inputColour = Console.ReadLine();
Console.WriteLine("Dogs Weight:");
int inputWeight = Convert.ToInt32(Console.ReadLine());
// add input to the list.
addDog(inputName, inputAge, inputSex, inputBreed, inputColour, inputWeight);
}
public static void addDog(string name, int age, string sex, string breed, string colour, int weight)
{
// The name 'dogList' does not exist in the current context
dogList.Add(new Dog()
{
name = name,
age = age,
sex = sex,
breed = breed,
colour = colour,
weight = weight
});
}
}
public class Dog
{
public string name { get; set; }
public int age { get; set; }
public string sex { get; set; }
public string breed { get; set; }
public string colour { get; set; }
public int weight { get; set; }
}
}
dogList
is local to the methodMain
. What you want to do instead is to placedogList
outside of that scope.Alternately you can send the list into your add method.
@Ari....Here is how you can do it
The list was inaccessible due to its protection level.When you have to use a list in another method then you have to declare it first.Happy coding
The
dogList
variable is scoped local to the Main method, so it is not accessible to other method in your class, you have few ways to make it correct, one solution can be to pass thedogList
as well as parameter to that method like:and change the signature of
addDog
method as well to be :If you don't want to do that way, another solution can be to make your
dogList
variable at class level i.e. make it field like:dogList
exists only in the scope of theMain
method. If you declare a variable in one method it becomes local and cannot be accessed in another method.You could solve it by passing the necessary variable as a parameter:
now you pass the variable in the call like this:
or you can declare the variable at the scope of the class:
In the latter version you need to declare it as static, otherwise the compiler will demand an Instance of the class
Program
to be able to access the variableThe main problem is that you have declared dogList locally from within main. You have also declared addDog as static. Static methods are outside of the current object.
Think of Main as your living room you are standing in your living room. Now think of addDog as your bathroom I am standing in there. We have know knowledge that each other is there so there is no way of us to communicate.