Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7567131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:35:19+00:00 2026-05-30T14:35:19+00:00

I need to make some changes so that I only retrieve one object at

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:35:20+00:00Added an answer on May 30, 2026 at 2:35 pm

    Not sure to understand, but how about a simple GetById method?

    public class Foo
    {
        public int Id { get; set; }
    
        public string Name { get; set; }
    }
    
    public class Test
    {
        private List<Foo> list = new List<Foo>();
    
        public void Add(Foo foo)
        {
            this.list.Add(foo);
        }
    
        public Foo GetById(int id)
        {
            return this.list.FirstOrDefault(z => z.Id == id);
        }
    }
    
    ....
    
    Test test = new Test();
    test.Add(new Foo { Id = 1, Name = "1" });
    test.Add(new Foo { Id = 2, Name = "2" });
    test.Add(new Foo { Id = 3, Name = "3" });
    
    Foo foo2 = test.GetById(2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to download file from FTP server and make some changes on it
It usually happens to me that I make some local changes, only to discover
I need to make some changes to an old Oracle stored procedure on an
I am C++ programmer and I need to make some changes to VB6 code.
I need to make some permission changes on a MS SQL server (2005) database.
I need to make some reflective method calls in Java. Those calls will include
I need to make some code to talk to a SOAP web service. Unfortunately
I need to make some commits using Git but I would like the timestamp
In a .NET 3.0 project, I need to make some calls via P/Invoke (specifically
I need to make use of some OWL ontologies in c#. Does anyone have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.