简体   繁体   中英

How to get new instance of object class?

First of all, my programming concept isn't really good, so I'm not even sure if my question has the right choice of words. Anyways, here's the problem I faced:

private void gridCommsEqp_OnSelectionChanged(object sender, SelectionChangeEventArgs e)
{
    var temp = (CommsEqp) gridCommsEqp.SelectedItem;
    var commItems = new List<CommsItem>(issueAdapt.getCommsItemsByEqpID(temp.EqpID));
}

Here I'm trying to get the correct Comms Item based on the equip selected (can only select ONE) through my DataGrid. Everytime a selection changes, it will update new set of item(s) that correspond with that equip.

This is my "getCommsItemsByEqpID" method:

public IEnumerable<CommsItem> getCommsItemsByEqpID(int eqpID)
{
    DataTable commItem = issueTransactions.getCommsItemsByEqpID(eqpID);
    var listCommsItems = new List<CommsItem>((from DataRow row in commItem.Rows 
                                             select new CommsItem((int)row["ItemID"], 
                                            (string)row["cardNO"], 
                                            (string)row["serial"])).ToList());
    return listCommsItems;
}

The issue is that every time a selection changes, my code will continue to add to the list, rather getting a new instance. So for example, after I play around with the selection by clicking different rows of the datagrid many times, I would have obtained a list of multiple equipIDs of all rows that I have selected before. How do I avoid this?

Though I could have assigned the list straight away, I tried creating new instance to see if it helps as seen above. I also thought the "select new CommsItem" will create a new instance of the object class, and hence previous records will not be retained. But it's still giving me back a combined list of all previously selected items.

Thanks in advance for the help!

Edit: More codes shown as requested

public DataTable getCommsItemsByEqpID(int eqpID)
    {
        var sqlString = String.Format("SELECT ItemID, cardNo, 
                                   serial FROM tblCommsItem WHERE EqpID = {0}", eqpID);
        establishDB(sqlString);
        ad.Fill(ds, "CommsItems");
        conn.Close();
        return ds.Tables["CommsItems"];
    }

private void establishDB(string sqlString)
    {
        command.CommandText = sqlString;
        conn = connectDB();
        ad.SelectCommand = command;
        command.Connection = conn;
    }

private static SqlConnection connectDB()
    {
        var conn = new SqlConnection();
        string connString = ConfigurationManager.ConnectionStrings["SecureDB"].ToString();
        conn.ConnectionString = connString;
        conn.Open();
        return conn;
    }

Edit 2: More codes shown:

private void gridCommsEqp_OnSelectionChanged(object sender, SelectionChangeEventArgs e)
    {
        var temp = (CommsEqp) gridCommsEqp.SelectedItem;

            var commItems = new List<CommsItem>(issueAdapt.getCommsItemsByEqpID(temp.EqpID));
            if (commItems[0].CardNO == "" && commItems[0].Serial == "")
            {
                gridCommsItem.Columns.Clear();
                gridCommsItem.MaxColumnWidth = Double.PositiveInfinity;
                var gridColumn = new GridViewColumn{Header = "This item is not serialized. You can directly select the quantity.", Width = gridCommsItem.Width - 50, HeaderTextAlignment = TextAlignment.Center};
                gridCommsItem.Columns.Add(gridColumn);
                gridCommsItem.ShowGroupPanel = false;
                gridCommsItem.IsEnabled = false;
            }

    }

The problem I suspect probably is that you have not cleared the data in the DataSet and DataTable before each Fill. So clear the content in DataSet and DataTable before you fill the data into DataSet.

The cause is this line:

ad.Fill(ds, "CommsItems");

Set the debugger break point here. You will see more accumulated records after each click.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM