Click here to Skip to main content
15,900,254 members
Home / Discussions / C#
   

C#

 
GeneralRe: Getting information about an object hosted on a different application Pin
led mike12-Jun-08 5:26
led mike12-Jun-08 5:26 
GeneralRe: Getting information about an object hosted on a different application Pin
dimaj12-Jun-08 5:37
dimaj12-Jun-08 5:37 
AnswerRe: Getting information about an object hosted on a different application Pin
Christian Graus11-Jun-08 7:35
protectorChristian Graus11-Jun-08 7:35 
GeneralRe: Getting information about an object hosted on a different application Pin
dimaj11-Jun-08 7:41
dimaj11-Jun-08 7:41 
GeneralRe: Getting information about an object hosted on a different application Pin
Christian Graus11-Jun-08 8:25
protectorChristian Graus11-Jun-08 8:25 
GeneralRe: Getting information about an object hosted on a different application Pin
Christian Graus11-Jun-08 8:25
protectorChristian Graus11-Jun-08 8:25 
GeneralRe: Getting information about an object hosted on a different application Pin
dimaj11-Jun-08 8:29
dimaj11-Jun-08 8:29 
QuestionA field initializer cannot reference the non-static field, method or property... help with code snippet Pin
ssclaire11-Jun-08 6:45
ssclaire11-Jun-08 6:45 
I came across this error in my code that I cannot fix:
A field initializer cannot reference the non-static field, method, or property 'ConsoleApplication.Polygon.colorValues'
So extracted what I thought was the relevant part of the code to post and ask for help. The problem is that now this simplified code worksConfused | :confused: . But I cannot find the problem in the original, full-length code.

Since the original code is too long to post here, I was hoping I could post the "working" code here with the error. Maybe someone could tell me at least what to look for.

This is my working code.

The code creates a class with two fields. Both fields are enumerations...
using System;

namespace ConsoleApplication
{
    class Program
    {
        enum ColorType {Red, Green, Blue, Yellow};      // all possible colors
        enum ShapeType {Square, Circle, Triangle};      // all possible shapes

        class Polygon
        {
            // each polygon has a color and a shape
            public ColorType Color { get; set; }
            public ShapeType Shape { get; set; }

            private Polygon() { }       // hide the default constructor
            public Polygon(ColorType color, ShapeType shape)
            {
                Color = color;
                Shape = shape;
            }
        }

The code then tries to create an array of these classes. The elements of the array contain each possible combination of the enumerations. To do that, I use Enum.GetValues() to get an array of possible enum values and use the Array.Length property to figure out the size of the array I will need to hold all the combinations.
        static void Main(string[] args)
        {
            Array colorValues = Enum.GetValues(typeof(ColorType));
            Array shapeValues = Enum.GetValues(typeof(ShapeType));
            Polygon[] allPolygons = new Polygon[<code>colorValues</code>.Length * <code>shapeValues</code>.Length];

            int index = 0;
            foreach (ColorType color in colorValues)
            {
                foreach (ShapeType shape in shapeValues)
                {
                    allPolygons[index] = new Polygon(color, shape);
                    Console.WriteLine(allPolygons[index]);
                    index++;
                }
            }
        }
    }
}

I underlined the portion (above) that is generating the error:
A field initializer cannot reference the non-static field, method, or property 'ConsoleApplication.Polygon.colorValues'.

Unfortunately, this code works but the real code doesn't. Can someone help me interpret what the compiler might be having a problem with so I know what to look for?

Thanks.
AnswerRe: A field initializer cannot reference the non-static field, method or property... help with code snippet Pin
S. Senthil Kumar11-Jun-08 6:56
S. Senthil Kumar11-Jun-08 6:56 
GeneralRe: A field initializer cannot reference the non-static field, method or property... help with code snippet Pin
ssclaire11-Jun-08 8:35
ssclaire11-Jun-08 8:35 
AnswerSome Progress... Update Pin
ssclaire11-Jun-08 7:08
ssclaire11-Jun-08 7:08 
GeneralRe: Some Progress... Update Pin
Christian Graus11-Jun-08 7:19
protectorChristian Graus11-Jun-08 7:19 
GeneralRe: Some Progress... Update Pin
S. Senthil Kumar11-Jun-08 8:27
S. Senthil Kumar11-Jun-08 8:27 
AnswerRe: A field initializer cannot reference the non-static field, method or property... help with code snippet Pin
Christian Graus11-Jun-08 7:08
protectorChristian Graus11-Jun-08 7:08 
QuestionCompiler Error Message: CS1519: Invalid token 'foreach' in class, struct, or interface member declaration Pin
Marvin A. Lee II11-Jun-08 6:17
Marvin A. Lee II11-Jun-08 6:17 
AnswerRe: Compiler Error Message: CS1519: Invalid token 'foreach' in class, struct, or interface member declaration Pin
leppie11-Jun-08 6:21
leppie11-Jun-08 6:21 
GeneralRe: Compiler Error Message: CS1519: Invalid token 'foreach' in class, struct, or interface member declaration Pin
Marvin A. Lee II11-Jun-08 11:51
Marvin A. Lee II11-Jun-08 11:51 
AnswerRe: Compiler Error Message: CS1519: Invalid token 'foreach' in class, struct, or interface member declaration Pin
S. Senthil Kumar11-Jun-08 6:22
S. Senthil Kumar11-Jun-08 6:22 
AnswerRe: Compiler Error Message: CS1519: Invalid token 'foreach' in class, struct, or interface member declaration Pin
leppie11-Jun-08 6:23
leppie11-Jun-08 6:23 
QuestionCode Generation competition Article Submission problem. Pin
hdv21211-Jun-08 6:06
hdv21211-Jun-08 6:06 
AnswerRe: Code Generation competition Article Submission problem. Pin
leppie11-Jun-08 6:19
leppie11-Jun-08 6:19 
GeneralRe: Code Generation competition Article Submission problem. Pin
hdv21211-Jun-08 6:23
hdv21211-Jun-08 6:23 
GeneralRe: Code Generation competition Article Submission problem. Pin
leppie11-Jun-08 6:25
leppie11-Jun-08 6:25 
AnswerRe: Code Generation competition Article Submission problem. Pin
Giorgi Dalakishvili11-Jun-08 6:58
mentorGiorgi Dalakishvili11-Jun-08 6:58 
QuestionShould I use new for a string? [Solved] Pin
Hamed Musavi11-Jun-08 5:59
Hamed Musavi11-Jun-08 5:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.