Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# – How to call a string array from a void method to the main class

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";

        
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading