I’m not looking for specific code, rather, I’m looking for information and guidance. I want to learn but don’t really want someone to code it.
I am looking in how to pass two arrays to a different method so they can be filled with user input. I can’t seem to figure this out and I have researched various sites as well as my text and lectures and can’t find the required techniques to do this. I know how to pass one array to a different method for processing (IE getting avg/sum etcetc) but not how to fill two arrays from one seperate method. Any guidance and information would be greatly appreciated. This is what I’ve got so far, or rather, what I’m left with. I got the other methods fairly done over, just need this part to move onto the debugging phase.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PhoneDial
{
class Program
{
// Get player names and their scores and stores them into array for an unknown number of players up to 100
static void InputData(string[] nameList, int[]playerScore)
{
string userInput;
int count = 0;
do
{
Console.Write("\nEnter a players name: ");
userInput = Console.ReadLine();
if (userInput != "Q" && userInput != "q")
{
nameList[0] = Console.ReadLine();
++count;
}
else break;
Console.WriteLine("Enter {0}'s score:", userInput);
playerScore[0] = Convert.ToInt32(Console.ReadLine());
} while (userInput != "Q" && userInput != "q");
}
//Declare variables for number of players and average score and two arrays of size 100 (one for names, one for respective scores
//Calls functions in sequence, passing necessary parameters by reference
//InputData(), passing arrays and number of players variable by reference
//DisplayPlayerData(), passing arrays and number of players by reference
//CalculateAverageScore(), passing arrays and number of players by reference. Store returned value in avg variable
//DisplayBelowAverage(), passing arrays and number of players variable by reference, passing average variable by value
static void Main(string[] args)
{
string[] nameList = new string[100];
int[] playerScore = new int[100];
int count = 0, avg = 0;
InputData(nameList, playerScore);
}
Your question is not entirely clear, but from what I understand, to declare an array in a method and pass to another method to be filled, you just need:
If, however, you wish to pass some variable reference to a method to then have that method create the array and fill it, you will want to use the
refkeyword (as you have with standard variables) on an array variable. Like so:To do either of these two approaches with two arrays is the same, but with an added parameter. i.e.: