ben04rogers
ben04rogers

Reputation: 65

How do I access variables in Main() from different method

Im trying to access a the array fortune from my DisplayFortune method but it says it doesnt exist in the current context. Any help would be appreciated. Im new to c#.

static void Main()
{
    // Write your main here.
    string[] fortune = { "String 1", "String 2", "String 3", "String 4", 
    "String 5", "String 6" };
    Random rand = new Random();
    int index = rand.Next(fortune.Length);
    WriteLine(fortune[index]);
    ReadLine();
}

public static void DisplayFortune(string first, string second)
{
    WriteLine(fortune[index]);

}

Upvotes: 0

Views: 4714

Answers (9)

sayah imad
sayah imad

Reputation: 1553

it's normal your array fortunes is in another scope , to resolve your problem there is two way .

  1. You need to declare your array fortunes outside Main method , in this case your array fortunes will be avaible for both method Main and DisplayFortune beceause all of them share the same level-class scope

    // Level-Class Scope
    static string[] fortune ;
    
    static void Main()
    {
        // Write your main here.
        fortune = new string[6];
        Random rand = new Random();
        int index = rand.Next(fortune.Length);
        Console.WriteLine(fortune[index]);
        Console.ReadLine();
    }
    
    public static void DisplayFortune(int index)
    {
        Console.WriteLine(fortune[index]);
    }
    
  2. The second way to resolve your probleme is to pass it as a parameter in DisplayFortune

    Simple Method :

    public static void DisplayFortune(string[] array, int index)
    {
        Console.WriteLine(array[index]);
    }
    

    Extension Method :

    public static void DisplayFortune(this string[] array, int index)
    {
        Console.WriteLine(array[index]);
    }
    

Best regards .

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186803

Local variables like string[] fortune, Random rand, int index are not visble beyound the scope they are declared in (i.e. Main method). When implementing DisplayFortune method, let's have a look which argument(s) we want:

 public static void DisplayFortune(/* what should we put here? */)
 {
     // we want:
     //   fortune
     //   index 
     WriteLine(fortune[index]);
 }

Thus we can put

 public static void DisplayFortune(string[] fortune, int index)
 {
     WriteLine(fortune[index]);
 }

Finally, since it's a public method (everyone can call it), let's validate the input (what if I execute it as DisplayFortune(null, -1234)?):

 public static void DisplayFortune(string[] fortune, int index)
 {
     if (null == fortune)
         throw new ArgumentNullException(nameof(fortune));
     else if (index < fortune.GetLowerBound(0) || index > fortune.GetUpperBound(0)) 
         throw new ArgumentOutOfRangeException(nameof(index)); 

     WriteLine(fortune[index]);
 }

To call the method we should provide its arguments:

 static void Main() 
 {
    string[] fortune = { 
      "String 1", "String 2", "String 3", "String 4", "String 5", "String 6" 
    };

    Random rand = new Random();
    int index = rand.Next(fortune.Length); 

    ...
    DisplayFortune(fortune, index);
 }

Upvotes: 1

Nathaniel Pisarski
Nathaniel Pisarski

Reputation: 308

So, in the code that you posted there are a few strange things. The reason you can't access the variable is because it's out of scope. Anywhere you see the curly brackets { } you're creating a scoped block. Variables declared within that block can only be referenced in the same block. So, you can't access the variable in another function unless it:

  • Is declared at a higher scope
  • Is passed into the function as a parameter

The second odd thing about the code is that you have two unused parameters in DisplayFortune. You should change those parameters to accept the array.

public static void DisplayFortune(string[] fortunes, int index)
{
    WriteLine(fortunes[index]);
}

Then, call it like DisplayFortune(fortunes, index); in Main.

I assume you're learning, so just to point out one more way to improve your code:

  • Your DisplayFortune method shouldn't print the fortune - it should return it as a string, so your Main method can make use of it.
  • List item

Upvotes: 0

Efraim Newman
Efraim Newman

Reputation: 1041

There are a few ways you can do this. You can declare your variable Outside the Main function like this:

static  string[] fortune = { "String 1", "String 2", "String 3", "String 4", 
"String 5", "String 6" };

another option is to pass the variable to the method:

public static void DisplayFortune(string first, string second, string[] fortune)
{
    WriteLine(fortune[index]);
}

Upvotes: 0

JessGabriel
JessGabriel

Reputation: 1082

your function doesn't make anysense. - Option 1 : Declare fortune as const outside anything - Option 2 : Add array as displayFortune() parameters

Upvotes: 0

Rahul
Rahul

Reputation: 77896

Explicitly pass that as method parameter like

public static void DisplayFortune(string first, string second, string[] fortune)
{
    WriteLine(fortune[index]);

}

Upvotes: 3

user10285265
user10285265

Reputation:

You cannot access a variable inside another method without taking it outside of the method scope.

So move the variable out from Main, and then you can access it.

Eg. pseudo code

int yourVaribale = 0;
static Main { yourVariable = 5; OtherMethod(); }...
static OtherMethod{ yourVariable = 10; }....

Upvotes: 0

Cid
Cid

Reputation: 15247

You can't.

Else if you are making this variable a member of the class, such as :

// declaration outside of Main's scope
static string[] fortune;

static void Main()
{
    // Write your main here.
    fortune = { "String 1", "String 2", "String 3", "String 4", 
    "String 5", "String 6" };
    Random rand = new Random();
    int index = rand.Next(fortune.Length);
    WriteLine(fortune[index]);
    ReadLine();
}

public static void DisplayFortune(string first, string second)
{
    WriteLine(fortune[index]);
}

Or if you pass this variable to the method directly, such as :

public static void DisplayFortune(string first, string second, string[] fortune)
{
    WriteLine(fortune[index]);
}

Upvotes: 0

yfet
yfet

Reputation: 69

Declare your fortune variable outside of Main. Just below the class declaration.

Upvotes: 0

Related Questions