By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,812 Members | 863 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,812 IT Pros & Developers. It's quick & easy.

Help creating an array of 3 colors

P: n/a
Hi,

I normally program in C++, and I'm trying to write this little tool in C#,
but I quickly realized that I can't use pointers, so instead I need to create
an array of 3 Color 's

private Color cDay;
private Color cNight;
private Color cLuminous;

Color[] colors = new colors[3] { cDay, cNight, CLuminous };
curColor = 0; // An index used to switch the struct I'm "pointing"
to

but, the above doesn't work. Tells me I'm trying to initialize with
non-static data.

I'm trying to do the above so that later I can do something like this:

curColor[0].R = controlRed.Value;

I'm pulling my hair out to do something so simple.

Help.

Jul 30 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hello Stick,

Just remove the 3 since you're setting up the contents it will be 3 in length.
E.g.:

Color[] colors = new colors[] { cDay, cNight, CLuminous };

Thanks,
Shawn Wildermuth
Speaker, Author and C# MVP
http://adoguy.com
Hi,

I normally program in C++, and I'm trying to write this little tool in
C#, but I quickly realized that I can't use pointers, so instead I
need to create an array of 3 Color 's

private Color cDay;
private Color cNight;
private Color cLuminous;
Color[] colors = new colors[3] { cDay, cNight, CLuminous };
curColor = 0; // An index used to switch the struct I'm
"pointing"
to
but, the above doesn't work. Tells me I'm trying to initialize with
non-static data.

I'm trying to do the above so that later I can do something like this:

curColor[0].R = controlRed.Value;

I'm pulling my hair out to do something so simple.

Help.

Jul 30 '06 #2

P: n/a
mac
Not sure about this because I am a newbie, but I used the compiler, and I
think the word "colors" (right of the assign) should be "Color". Also a
casing problem on CLuminious (should be cLuminious). Either statement below
will work:

//Statement 1
Color[] colors = new Color[] { cDay, cNight, cLuminous };

// Statement 2
Color[] colors = { cDay, cNight, cLuminous };

Statement 2 is less typing, but I don't know which is considered the
preferred form. Is there a general preference among experiences coders?

Thanks,

Mac

"Shawn Wildermuth (C# MVP)" <sw*********@newsgroup.nospamwrote in message
news:fc**************************@msnews.microsoft .com...
Hello Stick,

Just remove the 3 since you're setting up the contents it will be 3 in
length. E.g.:

Color[] colors = new colors[] { cDay, cNight, CLuminous };

Thanks,
Shawn Wildermuth
Speaker, Author and C# MVP
http://adoguy.com
>Hi,

I normally program in C++, and I'm trying to write this little tool in
C#, but I quickly realized that I can't use pointers, so instead I
need to create an array of 3 Color 's

private Color cDay;
private Color cNight;
private Color cLuminous;
Color[] colors = new colors[3] { cDay, cNight, CLuminous };
curColor = 0; // An index used to switch the struct I'm
"pointing"
to
but, the above doesn't work. Tells me I'm trying to initialize with
non-static data.

I'm trying to do the above so that later I can do something like this:

curColor[0].R = controlRed.Value;

I'm pulling my hair out to do something so simple.

Help.


Jul 30 '06 #3

P: n/a
Yeah, that doesn't solve it.

I even tried this:

struct rgbColor
{
private int red;
private int green;
private int blue;
};
rgbColor cDay, cNight, cLuminous;

rgbColor[] ourColors = new rgbColor[] { cDay, cNight, cLuminous };
private int curColor = 0;

and this won't compile. So it seems that I cannot make an array of struct
in C#.

Patrick

"Shawn Wildermuth (C# MVP)" wrote:
Hello Stick,

Just remove the 3 since you're setting up the contents it will be 3 in length.
E.g.:

Color[] colors = new colors[] { cDay, cNight, CLuminous };

Thanks,
Shawn Wildermuth
Speaker, Author and C# MVP
http://adoguy.com
Hi,

I normally program in C++, and I'm trying to write this little tool in
C#, but I quickly realized that I can't use pointers, so instead I
need to create an array of 3 Color 's

private Color cDay;
private Color cNight;
private Color cLuminous;
Color[] colors = new colors[3] { cDay, cNight, CLuminous };
curColor = 0; // An index used to switch the struct I'm
"pointing"
to
but, the above doesn't work. Tells me I'm trying to initialize with
non-static data.

I'm trying to do the above so that later I can do something like this:

curColor[0].R = controlRed.Value;

I'm pulling my hair out to do something so simple.

Help.


Jul 30 '06 #4

P: n/a
Stick wrote:
private Color cDay;
private Color cNight;
private Color cLuminous;

Color[] colors = new colors[3] { cDay, cNight, CLuminous };
curColor = 0; // An index used to switch the struct I'm "pointing"
the above doesn't work. Tells me I'm trying to initialize with
non-static data.
Right. You can't initialize an instance field with an instance field:
Your cDay, cNight, cLuminous, and colors are all instance fields.

You can say, though, that

Color[] colors = new Color[] {Color.White, Color.Black,
Color.LightSeaGreen};

or, you can use an instance constructor

class Whatever
{
Color[] colors;

public Whatever()
{
Color cDay = Color.White;
Color cNight = Color.Black;
Color cLuminous = Color.LightSeaGreen;

colors = new Color[] { cDay, cNight, CLuminous };
}
}

In either case, you can then change colors[N] as you wish.

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.
Jul 30 '06 #5

P: n/a
Just wonder what is your data type of curColor. Because if you said:

cusColor =0;

is this an integer?
If it is integer then how can you do:

curColor[0].R = controlRed.Value;

right?

chanmm

"Stick" <St***@discussions.microsoft.comwrote in message
news:EE**********************************@microsof t.com...
Hi,

I normally program in C++, and I'm trying to write this little tool in C#,
but I quickly realized that I can't use pointers, so instead I need to
create
an array of 3 Color 's

private Color cDay;
private Color cNight;
private Color cLuminous;

Color[] colors = new colors[3] { cDay, cNight, CLuminous };
curColor = 0; // An index used to switch the struct I'm
"pointing"
to

but, the above doesn't work. Tells me I'm trying to initialize with
non-static data.

I'm trying to do the above so that later I can do something like this:

curColor[0].R = controlRed.Value;

I'm pulling my hair out to do something so simple.

Help.

Jul 30 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.