Home > Mobile >  C# - How to call a string array from a void method to the main class
C# - How to call a string array from a void method to the main class

Time:07-14

i have a string array called movieNames which is in a void class called movieList , now im able to call the movieList method in my mainClass but i have issues to get a refrence of movieNames array

using System;
class Arrays
{
    public static void Main()
    {
        Arrays arr = new Arrays();
        arr.movieList();
        Random random = new Random();
        var random1 = random.Next(movieNames.Length); // as u can see i want to get one movie from the list and want to print in console
        Console.WriteLine( movieNames[random1]);


    }
    public void movieList()
    {
       
        string[] movieNames = new string[118];
        movieNames[0] = "The Lunchbox";
        movieNames[1] = "Sholay";
        movieNames[2] = "3 Idiots";
        movieNames[3] = "PK";
        movieNames[4] = "Dangal";
        movieNames[5] = "Andaz Apna Apna";
        movieNames[6] = "Munna Bhai M.B.B.S.";
        movieNames[7] = "Lagaan: Once Upon a Time in India";
        movieNames[8] = "Black Friday";
        movieNames[9] = "Kabhi Khushi Kabhie Gham";
        movieNames[10] = "Rang De Basanti";
        movieNames[11] = "Dilwale Dulhania Le Jayenge";
        movieNames[12] = "Chak de! India";
        movieNames[13] = "Taare Zameen Par";
        movieNames[14] = "Gangs of Wasseypur";
        movieNames[15] = "Parinda";
        movieNames[16] = "Mother India";
        movieNames[17] = "Drishyam";
        movieNames[18] = "Satya";
        movieNames[19] = "Deewaar";
        movieNames[20] = "Nayakan";
        movieNames[21] = "Kai Po Che!";
        movieNames[22] = "Dostana";
        movieNames[23] = "Munna Bhai M.B.B.S.";
        movieNames[24] = "Omkara";
        movieNames[25] = "Parinda";
        movieNames[26] = "Rang De Basanti";
        movieNames[27] = "Rockstar";
        movieNames[28] = "Sholay";
        movieNames[29] = "Taare Zameen Par";
        movieNames[30] = "The Lunchbox";
        movieNames[31] = "3 Idiots";
        movieNames[32] = "Dangal";
        movieNames[33] = "Andaz Apna Apna";
        movieNames[34] = "Munna Bhai M.B.B.S.";
        movieNames[35] = "Lagaan: Once Upon a Time in India";
        movieNames[36] = "Black Friday";
        movieNames[37] = "Kabhi Khushi Kabhie Gham";
        movieNames[38] = "Rang De Basanti";
        movieNames[39] = "Dilwale Dulhania Le Jayenge";
        movieNames[40] = "Chak de! India";
        movieNames[41] = "Taare Zameen Par";
        movieNames[42] = "Gangs of Wasseypur";
        movieNames[43] = "Parinda";
        movieNames[44] = "Mother India";
        movieNames[45] = "Drishyam";
        movieNames[46] = "Satya";
        movieNames[47] = "Deewaar";
        movieNames[48] = "Nayakan";
        movieNames[49] = "Kai Po Che!";
        movieNames[50] = "Dostana";
        movieNames[51] = "Munna Bhai M.B.B.S.";
        movieNames[52] = "Omkara";
        movieNames[53] = "Parinda";
        movieNames[54] = "Rang De Basanti";
        movieNames[55] = "Rockstar";
        movieNames[56] = "Sholay";
        movieNames[57] = "Taare Zameen Par";
        movieNames[58] = "The Lunchbox";
        movieNames[59] = "3 Idiots";
        movieNames[60] = "Dangal";
        movieNames[61] = "Andaz Apna Apna";
        movieNames[62] = "Munna Bhai M.B.B.S.";
        movieNames[63] = "Lagaan: Once Upon a Time in India";
        movieNames[64] = "Black Friday";
        movieNames[65] = "Kabhi Khushi Kabhie Gham";
        movieNames[66] = "Rang De Basanti";
        movieNames[67] = "Dilwale Dulhania Le Jayenge";
        movieNames[68] = "Chak de! India";
        movieNames[69] = "Taare Zameen Par";
        movieNames[70] = "Gangs of Wasseypur";
        movieNames[71] = "Parinda";
        movieNames[72] = "Mother India";
        movieNames[73] = "Drishyam";
        movieNames[74] = "Satya";
        movieNames[75] = "Deewaar";
        movieNames[76] = "Nayakan";
        movieNames[77] = "Kai Po Che!";
        movieNames[78] = "Dostana";
        movieNames[79] = "Munna Bhai M.B.B.S.";
        movieNames[80] = "Omkara";
        movieNames[81] = "Parinda";
        movieNames[82] = "Rang De Basanti";
        movieNames[83] = "Rockstar";
        movieNames[84] = "Sholay";
        movieNames[85] = "Taare Zameen Par";
        movieNames[86] = "The Lunchbox";
        movieNames[87] = "3 Idiots";
        movieNames[88] = "Dangal";
        movieNames[89] = "Andaz Apna Apna";
        movieNames[90] = "Munna Bhai M.B.B.S.";
        movieNames[91] = "Lagaan: Once Upon a Time in India";
        movieNames[92] = "Black Friday";
        movieNames[93] = "Kabhi Khushi Kabhie Gham";
        movieNames[94] = "Rang De Basanti";
        movieNames[95] = "Dilwale Dulhania Le Jayenge";
        movieNames[96] = "Chak de! India";
        movieNames[97] = "Taare Zameen Par";
        movieNames[98] = "Gangs of Wasseypur";
        movieNames[99] = "Parinda";
        movieNames[100] = "Mother India";
        movieNames[101] = "Drishyam";
        movieNames[102] = "Satya";
        movieNames[103] = "Deewaar";
        movieNames[104] = "Nayakan";
        movieNames[105] = "Kai Po Che!";
        movieNames[106] = "Dostana";
        movieNames[107] = "Munna Bhai M.B.B.S.";
        movieNames[108] = "Omkara";
        movieNames[109] = "Parinda";
        movieNames[110] = "Rang De Basanti";
        movieNames[111] = "Rockstar";
        movieNames[112] = "Sholay";
        movieNames[113] = "Taare Zameen Par";
        movieNames[114] = "The Lunchbox";
        movieNames[115] = "3 Idiots";
        movieNames[116] = "Dangal";
        movieNames[117] = "Andaz Apna Apna";

        
    }
}

CodePudding user response:

You need:

  • use return in your method movieList() method
  • set return type to string[], not void

So code would look like this:

public string[] movieList()
{
   // the other code is omitted for the brevity
   return movieNames;
}

Or complete code:

public static void Main()
{
    string[] movieList = MovieList();
    Random random = new Random();
    var random1 = random.Next(movieList.Length);
    Console.WriteLine(movieList[random1]);
}


static string[] MovieList()
{
    string[] movieNames = new string[118];
    movieNames[0] = "The Lunchbox";
    movieNames[1] = "Sholay";
    movieNames[2] = "3 Idiots";

    // the other code is omitted for the brevity

    return movieNames;
}

CodePudding user response:

To start, these are methods, not classes. Terminology is important as you research problems, otherwise you're not looking for the right things.

As for the problem itself... movieNames is a local variable to the movieList method. So it only exists within that method. If you want the code which called that method to be able to observe it, return it from the method. Change the return type:

public string[] movieList()

And return the array at the end of the method:

return movieNames;

Then the calling code can observe the returned result from the method (storing the result in its own local variable):

var movieNames = arr.movieList();

CodePudding user response:

You must return the array in your method MovieList and then, call it from Main. David's answer is correct

CodePudding user response:

I suggest making MovieList into a property. I also suggest making it a list instead of an array.

Example:

public List<string> MovieTitles { get {
        List<string> output = new List<string>();
        output.Add("The Lunchbox");
        output.Add("Sholay");
        output.Add("3 Idiots");
        // etc
        return output;
    } }

Now you are able to call Array.MovieTitles and return a list of all your movies.

You shouldn't include the datatype in a variable's name. So MovieList should be renamed just Movies or MovieTitles.

  • Related