First of I don't feel it's a good idea to use WPF while learning how to program, so I suggest you forget about WPF and start doing some basic console applications. trying to learn WPF while is not a good idea, I know good programmers who have had trouble learning WPF or think they have learned it but really doesn't have any idea of how to utilize WPF. I'm not trying to be a troll, I'm just afraid you're going to choke yourself by trying to learn to much at the same time.
That said I'll go through some of the problems you have in your code.
1)
b.Add((new car()
{
});
What you're trying to do here is using the
Object initializer[
^]
So you can initialize the properties of your type
car
, like this:
b.Add((new car()
{
cylinders = 6.0,
topspeed = 500.0,
horsepower = 1000.0
});
2) In
Button1_Click
you create a new list to which you add a new
car
with the code we just discussed. But you do not store this list anywhere, what you probably want is to have a field in your class where you can store your
car
instances.
3) Again in
Button1_Click
you have this statement
if(i == n)<br />
that statement will never be true as you start you by ensuring i < n in the statement
if (i < n)
4) I can go on a bit here, but I'm not even sure you understand what I've just written, and I really don't want to write 2-3 pages here if you can't use i for anything, your question is simply to large. And from the bottom of my heart I highly suggest you do not use WPF untill you have a good crasp of C#.
But if you have a specific question about your code feel free to ask and I'll attempt to explain it for you.
Ok, if I understand correctly, your problem is now that you have an instance in window1 which you would like to use in window2.
You can solve this in a couple of ways:
1) You can make the
carlist
field public and static e.g.
public static List<car> carlist = new List<car>();</car></car>
when you do that you can access it like so
window1.carlist
here
window1
is not an instance but the type/class name.
This is not a very good solution, fields shouldn't be public and if you have two instances of
window1
then they would share the
carlist
2) You can give
window2
an the instance of
window1
you're using, and make the
carlist
field public or make a property that return the
carlist
.
3) You can give the
carlist
to
window2
by passing it to the constructor. This is what I would suggest you do.
E.g.
public partial class window1 : Window
{
...
private void button2_Click(object sender, RoutedEventArgs e)
{
window2 win2 = new window2(carlist);
win2.Show();
}
...
}
public partial class window2 : Window
{
private IList<car> carlist;
...
public window2(IList<car> carlist)
{
this.carlist = carlist;
InitializeComponent();
}
...
}