Roz
Roz

Reputation: 11

Display an array from a method using another method?

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()
}

Upvotes: 1

Views: 59

Answers (2)

sujith karivelil
sujith karivelil

Reputation: 29026

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);
}

Upvotes: 1

Roman
Roman

Reputation: 598

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]);
 }

Upvotes: 0

Related Questions