How to pass things to a method

Category: c# language

Question

jmatty2000 on Sat, 20 Oct 2012 17:10:07


Hi

I'm trying to pass some parameters to a general method I want to use to display various Lists that have been set. My code is way in-complete if anyone can point me in the right direction or correct it for me. I hope you can see what I'm trying to do :)

Many Thanks

Matt

            DisplayDetails("bank","classAccountBanks","AccountBanks","accountDetails") // Call the method using these parameters or whatever will work

        }

        private void DisplayDetails(do I need to put params in here?)
        {
            string Details = "";
            foreach (classAccountBanks bank in AccountBanks)
            {
                accountDetails = accountDetails + bank.Index + " " + bank.Description + "\n";
            }
            MessageBox.Show(Details);
        }


Replies

Levi Domingos on Sat, 20 Oct 2012 17:21:23


yes u do need to create parameters : http://msdn.microsoft.com/en-us/library/yy6y35y8(v=vs.80).aspx

jmatty2000 on Sat, 20 Oct 2012 17:23:29


Hi

Thanks but can you give me an example in my code - as I'm a beginner.

Cheers

Matt

jgalley on Sat, 20 Oct 2012 17:24:09


you might want to do something like:

private void DisplayDetails(List<classAccountBanks> myAccountBanks)
        {
            string Details = "";
            foreach (var bank in myAccountBanks)
            {
                Details = Details + bank.Index + " " + bank.Description + "\n";
            }
            MessageBox.Show(Details);
        }

then you might call it like:

           DisplayDetails(accountBanks);

using the "+" operator to join strings is fine, but you might want to look at StringBuilder() as well.


jon.stromer.galley


jmatty2000 on Sat, 20 Oct 2012 21:38:39


Thanks Jon

You've been a massive help - some of the stuff you've passed onto me has really been an eye opener. And yes, I'll take a more serious look at StringBuilder() next.

Thanks again

Matt