1

I basically want to display an array from a method with a method on my main method. The first method is the one with the array and the second one is the one that I want to use to display it but I don't know how.

Sorry if this is painfully obvious, I just can't figure it out

static public string[] MakeInsults(string[] sNames, string[] sVerbs, string[] sObjects, out int iNumber)
{
    Random random = new Random();
    Utility.GetValue(out iNumber, "Enter the number of insults to generate: ", 5, 100);
    string[] Insults = new string[iNumber];

    for (int i = 0; i < Insults.Length; i++)
    {
        Insults[i] = sNames[random.Next(0, 4)] + " " + sVerbs[random.Next(0, 4)] + " " + sObjects[random.Next(0, 4)];
    }

    return Insults;
}
static public string DisplayInsults(string[] sInsults)
{
   //Use this to display MakeInsults()
}
1
  • Just console write the content in array? Commented May 31, 2016 at 1:23

2 Answers 2

1

You can do it in two ways:

  • Call MakeInsults() From DisplayInsults() : For this you need to do following changes in the DisplayInsults;

Change return type of DisplayInsults to void and then no arguments are needed. the method will be like the following:

static public void DisplayInsults()
{
    Console.WriteLine("The elements in the array are:\n");
    Console.WriteLine(String.Join("\n",MakeInsults(sNames,sVerbs,sObjects,iNumber));
}
  • Call DisplayInsults() From MakeInsults(): For this you need to do following changes in the MakeInsults;

Change return type of MakeInsults to void and then call the DisplayInsults in place of return. the method will be like the following:

static public void MakeInsults(string[] sNames, string[] sVerbs, string[] sObjects, out int iNumber)
{
    //Process your statements;
    DisplayInsults(Insults);
}

Where the DisplayInsults will be defined like the following:

static public void DisplayInsults(string[] sInsults)
{
    Console.WriteLine("The elements in the array are:\n");
    Console.WriteLine(String.Join("\n",sInsults);
}
Sign up to request clarification or add additional context in comments.

Comments

0

If it's console app:

public static void DisplayInsults(string[] sInsults)
 {
   for (int i = 0; i < sInsults.Length; i++) {
     System.Console.Out.WriteLine(sInsults[i]);
 }

Comments

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.