I need to make some changes so that I only retrieve one object at a time from my list instead of the entire list as I do now.
Right now ive got a private list in class B with a property returning the entire list basically making it public anyway and I want to change this.
The way class a looks(the ui class operating with the list) is, I enter some data validate it and send it to class B which in turn packages it into a list of objects based on the input.
Then class A needs to loop this list and add it to a listview for displaying it which looks like this at the moment:
ListViewItem lvi = new ListViewItem();
foreach ([Object] o in CLassB.getList())
{
lvi = new ListViewItem(o.property0);
lvi.SubItems.Add(o.property1);
lvi.SubItems.Add(o.property2);
lvi.SubItems.Add(o.property3);
}
}
Object is my abstract class which controls how the different types of items are added and getList() is my method in class B returning the entire list.
The thing is these propertys are the common ones all classes share the there are some that arent, like a textbox that you enter specific text about the object and so on which is displayed like this:
private void lvRegistered_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
if (Listview.SelectedItems.Count > 0)
{
foreach ([Object] ob in ClassB.getList())
{
if (Listview.SelectedItems[0].SubItems[0].Text == ob.id.ToString())
{
TextBox.Clear();
TextBox.Text = ob.property4;
}
}
}
}
Now this all works great at the moment but now I have a returned list to operate on but I dont want to return the list making it public I want to return one object of the list based on an index number (yes the functionality will be exactly the same, I made a method returning the count of the private list so I can loop over it and return all). This is for practicing OOP for when I dont want to return everything.
How would this be done? All I can think of is making a new list, take an int as input and search my private list and find the index then add that to the other list and return that list, but I dont know if this is good practice or the best way to do it? Well I havent looked into how to “copy” one element over to next list either but might aswell check and see if theres a better way to do things?
Just get the feeling im “taking the long way around” this way
Not sure to understand, but how about a simple
GetByIdmethod?