Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder bobleny's Avatar
    Join Date
    May 2007
    Posts
    258
    Thanks
    3
    Thanked 11 Times in 11 Posts

    C#: A field initializer cannot reference the non-static field, method, or property...

    I'm getting this error:
    A field initializer cannot reference the non-static field, method, or property...

    The issue has to do with this code:
    Code:
    namespace Words
    {
        public class Wordy
        {
            private int maxCourses = 4;
    
            private string[] courseArray = new string[maxCourses];
        }
    }
    The error would be on line 7 in the above code. I've underlined the "bad" word.
    courseArray is an indexer array. So:
    Code:
    Wordy word = new wordy();
    
    Wordy[0] = "Good Bye World!";
    You get the idea.

    I'm sure it is something stupid, it all most always is...

    I don't understand why it is complaining, could someone explain it to me?

    Thanks!
    Last edited by bobleny; 07-23-2009 at 01:56 PM.
    --www.firemelt.net--
    * No good deed goes unpunished.
    * Cheer up, the worst has yet to come...

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    It is basically like the error message says. You are using the maxcourses variable which isn't static. So because it isn't static and you are using to initialize a static data member, it fails. The reason for this is that maxcourses is out of scope.

    If you make it static, it will work.
    OracleGuy

  • #3
    Regular Coder bobleny's Avatar
    Join Date
    May 2007
    Posts
    258
    Thanks
    3
    Thanked 11 Times in 11 Posts
    I thought for sure I tried that!

    I don't know why the array would be static either. I didn't tell it to be static, and I would think it would be an instance array. This way there is an instance of the array for each object created. However, it does work the way that it is supposed to now.

    Thanks again, oracleguy!
    --www.firemelt.net--
    * No good deed goes unpunished.
    * Cheer up, the worst has yet to come...

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by bobleny View Post
    I thought for sure I tried that!

    I don't know why the array would be static either. I didn't tell it to be static, and I would think it would be an instance array. This way there is an instance of the array for each object created. However, it does work the way that it is supposed to now.

    Thanks again, oracleguy!
    I totally missed that there wasn't a static keyword on the array. If the maximum is for all instances, you can also try make maxCourses constant.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •