Click here to Skip to main content
15,071,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The method header is as follows

public void Deletetorpedo(List<CTorpedo> torpList)
{
}

We've tried instantiating the class, then using the instance to gain access to the list we want to use.

TorpedoCSVProvider torpedos = new TorpedoCSVProvider();

The problem is the list of objects can only be accessed through a ref, because the method that populates the list after reading from the file returns a return code to confirm whether the read went succesfully or not.

int retCode = torpedos.GetTorpedo(ref torpList);

From our understanding that means "torpList" has now been changed by the method we passed it through to. Is this where we are going wrong? because later when we try to add the objects in the list to the listbox... nothing shows up.

So please help us with the casting of these objects and populating the list.
Posted

If I understand what you're aslking...

Since the ListBoxItems collection is a collection of type object, you don't have to cast the object when you add it to the ListBox. Just add this method to your item class:

C#
public override string ToString()
{
    // assuming desiredProperty is a string
    return desiredProperty;
}


Now, you can simply add your items to the listbox, and the listbox will use whatever is returned by ToString() for displaying the items.

When you use ListBox.SelectedItem, you will need to cast that to the appropriate type (in your case, TorpedoSvcProvider I suspect:

C#
TorpedoSvcProvider selected = listBox1.SelectedItem as TorpedoSvcProvider;
   
v2
Comments
BillWoodruff 19-Aug-11 1:15am
   
+5 Nailed it.
There is just one problem: you can add any objects to the list items, but what will be shown in UI when you do it? The answer is: whatever there respective implementations of the virtual method ToString returns. Therefore, you need to override this method to show desired strings in the
ListBox<code> control.<br />
<br />
<dd>—SA</dd>
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900