Quote:
quote:Originally posted by planoie
snip
|
I guess I need to explain further. Here's what I've created:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using GeoRange;
namespace GeoRange
{
public class Targets : List<Target>
{
public List<Target> Load()
{
List<Target> data = new List<Target>();
string connectionString = @"Server=E17818\SQLEXPRESS;Database=test;Trusted_C onnection=true";
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand myCommand = new SqlCommand("SELECT Name FROM Targets", myConnection);
myConnection.Open();
SqlDataReader reader = myCommand.ExecuteReader();
while (reader.Read())
{
Target Target1 = new Target();
Target1.Name = reader["Name"] as string;
//Target.X = reader["X"] as double;
//Target.Y = reader["Y"] as double;
data.Add(Target1);
}
myConnection.Close();
myCommand.Dispose();
return data; // not returning the List
}
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++ +++
//++++++++++++++++++++++++++++++++++++++++++++++++++ +++
using System;
using System.Collections.Generic;
using System.Text;
namespace GeoRange
{
public class Target
{
private string _name;
private double _X;
private double _Y;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public double X
{
get
{
return _X;
}
set
{
_X = value;
}
}
public double Y
{
get
{
return _Y;
}
set
{
_Y = value;
}
}
public Target()
{
}
public Target(string name, double X, double Y)
{
this._name = name;
this._X = X;
this._Y = Y;
}
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++ +++
//++++++++++++++++++++++++++++++++++++++++++++++++++ +++
I then try to use these from a button on a form:
private void button1_Click(object sender, EventArgs e)
{
List<Target> Targets = new List<Target>();
Targets targetList = new Targets();
targetList.Load();
foreach (Target theTarget in targetList)
{
comboBox1.Items.Add(theTarget.Name);
}
//MessageBox.Show(Target1.Name, "A caption");
}
This last part is the problem. targetList isn't returning the list. Anyone know why?