locked
why my program does not work

    Question



  • Error is A field initializer cannot reference the non-static field, method, or property 'Guess.Program.RandomNumber(int, int)' 

    • Edited by samanoo Monday, October 13, 2008 11:15 AM
    Monday, October 13, 2008 10:05 AM

Answers

  • It's because you're declaring int RandomNumber in the class Program while you want to use it in class Program1. You must use RandomNumber in the same class as it was created in. Here's an example of how it works:

    public class Program  
    {   // Doesn't matter if it's a class. As long as it's within a block  
        // you won't be able to use it outside that block.  
           
        int IntToUse = 0;     // IntToUse is declared here in class Program  
    }  
     
    public class Program1  
    {  
        Console.WriteLine(IntToUse.ToString());     // We're trying to use IntToUse here  
                                                    // but due to that it's declared in  
                                                    // class Program, we cannot use it in  
                                                    // class Program1.  

    As you can see, you cannot use IntToUse outside the block where it was declared.
    • Marked as answer by samanoo Monday, October 13, 2008 11:03 AM
    Monday, October 13, 2008 10:22 AM

All replies

  • It's because you're declaring int RandomNumber in the class Program while you want to use it in class Program1. You must use RandomNumber in the same class as it was created in. Here's an example of how it works:

    public class Program  
    {   // Doesn't matter if it's a class. As long as it's within a block  
        // you won't be able to use it outside that block.  
           
        int IntToUse = 0;     // IntToUse is declared here in class Program  
    }  
     
    public class Program1  
    {  
        Console.WriteLine(IntToUse.ToString());     // We're trying to use IntToUse here  
                                                    // but due to that it's declared in  
                                                    // class Program, we cannot use it in  
                                                    // class Program1.  

    As you can see, you cannot use IntToUse outside the block where it was declared.
    • Marked as answer by samanoo Monday, October 13, 2008 11:03 AM
    Monday, October 13, 2008 10:22 AM
  • I think your code looks like this

    namespace Guess 
        class Program 
        { 
            static void Main(string[] args) 
            { 
                RandomNumber(10, 10); 
            } 
     
            void RandomNumber(int a, int b) 
            { 
            } 
        } 

    RandomNumber is an instance member of class Program. You are trying to call that from a static method. So either you need to instantiate class Program and use that object to call RandomNumber method. Or just make RandomNumber method as static.

    Sincerly, Navaneeth.K.N
    Monday, October 13, 2008 2:03 PM