Thread: error: object ref. required for non static field

  1. #1
    Registered User
    Join Date
    Nov 2011

    error: object ref. required for non static field

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    namespace RabbitGenetics
        public class JWClass
            //Jersey Wooly Color Genetics
            // create list for sire data
            public List<int> siregtype = new List<int>();
            //create list for dam data
            public List<int> damgtype = new List<int>();
            // set sire & dam array size
            public const int rows = 128;
            public const int columns = 7;
            // create lists for dictionaries
            public List<int> colorslist = new List<int>();
            public List<int> gtypelist = new List<int>();
            // create sire & dam data 2D arrays
            public int[,] siredata = new int[rows, columns];
            public int[,] damdata = new int[rows, columns];
            //create offspring 1D array
            public int[] offspring = new int[14];
            // create sire & dam 1D arrays
            public int[] sire = new int[14];
            public int[] dam = new int[14];
            //create child 4D array
          //  public int[, , ,] child = new int[color_counter, 4];
            public Dictionary<string, int> colors = new Dictionary<string, int> { };
            public string color; //name key
            Dictionary<string, string> geneo = new Dictionary<string, string> { };
            public string gtype; // geneotype key
            // gene groups
            public int Broken, Agouti, Brown, Colour, Dilute, Ext, Vienna;
            // Vienna types flags
            bool vienna = false; bool vm = false; bool vc = false;
            //color flags
            bool dilute = false; bool charlie = false; bool broken = false; bool rew = false;
            // data holders
            public int sen1, sen2, sa1, sa2, sb1, sb2, sc1, sc2, sd1, sd2, se1, se2, sv1, sv2;
            public int den1, den2, da1, da2, db1, db2, dc1, dc2, dd1, dd2, de1, de2, dv1, dv2;
            public char colorpct;
            //declare counters
            public int sirecounter = 0;
            public int damcounter = 0;
            public int offspringcounter = 0;
            public int totalcount = 0;
            public int color_count = 0;
            // public color_counter = 0;
            public static bool e_trap;
        } // end JWClass
        public class Genetics
          // public JWClass jw;
            static void Main()
                  var  jw = new JWClass();
              //  obj = new JWClass();
            } // end Main
            // start program run
              public static  void prog()
               //array setup variiables for parsing combinations into the correct fields.
             jw.Broken = (jw.rows / 2);
            } // end prog
         } // End Class GenticsClass
    } // end Namespace RabbitGenetics
    I can't figure why I'm getting this error.
    I want to access and/or modify vars in class JWClass from class Genetics.
    I'm new to C# and OOP programming style.
    My background is COBOL using the structured style

  2. #2
    Registered User
    Join Date
    Mar 2009
    You're attempting to access the non-static instance "jw" from within a static method. Change:

    public JWClass jw;

    public static JWClass jw;

  3. #3
    Registered User
    Join Date
    Nov 2011
    Thanks. That did the trick.

    It then showed another error where I couldn't access var rows.
    Seems I can not use an instance to access it. So I used JWClass.rows and it works.

    Also found out I can use an instance for all the following vars that reference the var Broken

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Hannover, Germany
    Constants are always static, because they don't change for every instance, so it doesn't make sense to allocate memory for them for every instance.

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Constructor is not called for static object?
    By Memloop in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2009, 09:32 AM
  2. static object never inits
    By krappa in forum C++ Programming
    Replies: 10
    Last Post: 10-30-2008, 12:04 PM
  3. Sizeof static member object?
    By jw232 in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 11:36 AM
  4. global and static object
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-09-2008, 06:51 AM
  5. pass all the array as blob object in one field
    By Leite33 in forum Windows Programming
    Replies: 0
    Last Post: 11-12-2006, 01:27 PM