The name '...' does not exist in the current context
Solution 1:
dogList
is local to the method Main
. What you want to do instead is to place dogList
outside of that scope.
public class Program
{
static List<Dog> dogList = new List<Dog>();
...
Alternately you can send the list into your add method.
Solution 2:
dogList
exists only in the scope of the Main
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:
public static void addDog(string name, int age, string sex, string breed, string colour, int weight, List<Dog> dogList)
now you pass the variable in the call like this:
// add input to the list.
addDog(inputName, inputAge, inputSex, inputBreed, inputColour, inputWeight, dogList);
or you can declare the variable at the scope of the class:
public class Program
{
// create the list using the Dog class
static List<Dog> dogList = new List<Dog>();
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 variable
Solution 3:
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 the dogList
as well as parameter to that method like:
// add input to the list.
addDog(inputName, inputAge, inputSex, inputBreed, inputColour, inputWeight,dogList);
and change the signature of addDog
method as well to be :
public static void addDog(string name, int age, string sex, string breed, string colour, int weight, List < Dog > dogList)
{
}
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:
public class Program
{
List<Dog> dogList = new List<Dog>();
}
Solution 4:
The 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.
public class DogDb
{
// DogDb contains a list of dogs
public List<Dog> dogs { get; set; }
public DogDb() {
dogs = new List<Dog>();
}
// DogDb can control adding new dogs to its list of dogs.
public void addDog(string name, int age, string sex, string breed, string colour, int weight)
{
dogs.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; }
}
}
public class Program
{
public static void Main(string[] args)
{
// Create a new instance of our DogDB class.
var DogDb = new DogDb();
// 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 object.
DogDb.addDog(inputName, inputAge, inputSex, inputBreed, inputColour, inputWeight);
}
Solution 5:
@Ari....Here is how you can do it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
namespace DoggyDatabase
{
public class Program
{
private static List<Dog> dogList = new List<Dog>();
public static void Main(string[] args)
{
// create the list using the Dog class
// 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; }
}
}
}
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