1

I have method in C#, I have to return all values from ArrayList.

public string vyhledavaniOS()
        {
                foreach (Vozidlo voz in nabídka)
                {
                    if (voz is OsobníVůz)
                        return (voz.TypVozidla() + ": SPZ: " + voz.JakaSPZ + ", Značka: " + voz.JakaZnacka + ", Barva: " + voz.JakaBarva);
                }
        }

This code returns only one value, is there any way, how to return all values?

0

1 Answer 1

1

Yes, you need to change the method to return an array of strings instead of only one string value. Something like this:

public List<string> vyhledavaniOS()
{
    List<string> listToReturn = new List<string>();

    foreach (Vozidlo voz in nabídka)
    {
        if (voz is OsobníVuz)
            listToReturn.Add((voz.TypVozidla() + ": SPZ: " + voz.JakaSPZ + ", Znacka: " + voz.JakaZnacka + ", Barva: " + voz.JakaBarva));
    }

    return listToReturn;
}
Sign up to request clarification or add additional context in comments.

4 Comments

What platform is this? WinForms? WPF? ASP.NET?
Then I HIGHLY recommend that you start learning the MVVM pattern for WPF applications. Here's a very good quick-start tutorial. Alternatively, after you call the vyhledavaniOS() method, you can add the results to the ListBox using something like myListBox.Items.Add().
I have LB_vypis.Items.Add(nb.vyhledavaniOS()) but that does not show the values.
Items.Add() takes one item, but your method returns a list. You have to use a loop to add each item to the list. Something like: foreach(string item in nb.vyhledavaniOS()) { LB_vypis.Items.Add(item); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.