Reputation: 7
Currently I have this list:
List<Car> cars = new List<Car>()
{
new Car()
{
CarId = 1,
CarName = "BMW",
CarMod = "M6",
},
}
Under Form1.cs. I want it in a class maybe called MyCars.cs and be able to call upon it with this linq which lies under Form1.cs.
private void btn_Click(object sender, EventArgs e)
{
string entered = txtBox.Text;
var q = from car in cars
where car.CarMod.Contains(entered)
select cars;
dataGridView1.DataSource = query.ToList();
}
How do I call on it without getting "The name cars dosent exist in the current context"?
Wouldn't it be better and more elegant to put it in a class? I was thinking of making a class called MyCars.cs
Thanks alot for all help
Upvotes: 0
Views: 110
Reputation: 12544
Unless I'm mistaken, your goal is not to create a new class-type, but rather have your car logic in a separate file? You can put it in another file as long as the classname is the same. If the form is made in a 'normal' way, from .net 2.0 and up, they should be in a partial class
//Form1.cs
namespace A
{
public partial class Form1
{
public Form1()
{
InitalizeComponent();
}
}
}
As long as you use both the same name space and a partial class in the other file, you're good to go
//MyCars.cs
namespace A
{
partial class Form1
{
List<Car> cars = ..... /etc
}
}
Upvotes: 1