0

I'm making an assignment for school and it involves creating an application with certain restrictions on how to create it. Such as having a set amount of methods. I figured i'd say that before posting code that looks unoptimized, and can infact be done with less methods.

Anyway, currently i have a Northwind class, that handles everything that comes from the Database and in this case, creates a list filled with the Shipper object, then i use another Northwind Method to convert that to a queue. (this is what i mean with unoptimized, i could just use a queue from the get-go but i'm not allowed to.) However, when i use it it comes up with a very common error. But i can't figure out why...

class Northwind
{
    public Queue<Shipper> Queue { get; set; }

    public List<Shipper> GetList()
    {
        var con = new SqlConnection("Data Source=DESKTOP-G5VBFCN;Initial Catalog=Northwind;Integrated Security=True");
        var cmd = new SqlCommand("SELECT CompanyName, Phone FROM Shippers",con);
        con.Open();
        var reader = cmd.ExecuteReader();
        var ShipList = new List<Shipper>();

        while (reader.Read())
        {
            var s = new Shipper
            {
                CompanyName = reader["CompanyName"].ToString(),
                Phone = reader["Phone"].ToString()
            };
            ShipList.Add(s);
        }
        con.Close();
        return ShipList;
    }
    public Queue<Shipper> GetQueue(List<Shipper> List)
    {
        Queue<Shipper> ShipperQueue = new Queue<Shipper>(List);
        return ShipperQueue;
    }
}

}

And i use the class in my Form1.cs

public partial class Form1 : Form
{
    Northwind db;
    Shipper Shipper;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        db.Queue = db.GetQueue(db.GetList());
    }

Do please notice that the Northwind db; is actually underlined in green. I just started learning OOP. Thanks for taking the time to look at my code.

1 Answers1

1
Northwind db = new Northwind();

Or make the class static and you don't need to initialize on your own.

static class Northwind
{
    public static Queue<Shipper> Queue { get; set; }

    public static List<Shipper> GetList()
    {
        var con = new SqlConnection("Data Source=DESKTOP-G5VBFCN;Initial Catalog=Northwind;Integrated Security=True");
        var cmd = new SqlCommand("SELECT CompanyName, Phone FROM Shippers",con);
        con.Open();
        var reader = cmd.ExecuteReader();
        var ShipList = new List<Shipper>();

        while (reader.Read())
        {
            var s = new Shipper
            {
                CompanyName = reader["CompanyName"].ToString(),
                Phone = reader["Phone"].ToString()
            };
            ShipList.Add(s);
        }
        con.Close();
        return ShipList;
    }

    public static Queue<Shipper> GetQueue(List<Shipper> List)
    {
        Queue<Shipper> ShipperQueue = new Queue<Shipper>(List);
        return ShipperQueue;
    }
}

and invoke like

private void Form1_Load(object sender, EventArgs e)
{
    Northwind.Queue = Northwind.GetQueue(Northwind.GetList());
}
Scoregraphic
  • 7,110
  • 4
  • 42
  • 64
  • Omg, thanks for giving this tip, I didn't know you could make classes static and make them work like that. Thanks. – Joris Schelfhout Jan 22 '16 at 17:01
  • Technically, this would work even without making the class static - static members are allowed in nonstatic classes, but nonstatic members are not allowed in static classes. – David Jan 22 '16 at 17:02
  • Aravol is right, but a class with only static methods should be static as well. – Scoregraphic Jan 22 '16 at 17:07