Daniel Wilson
asked on
VB.Net -> C# translation -- Array declaration
This is stupid ... but ... how does the following line of VB.Net translate into C#?
Private SeparationColors(MAX_COLOR S) As System.Drawing.Color
(MAX_COLORS is Private Const MAX_COLORS As Byte = 25 which I've translated just fine.)
Thanks!
Private SeparationColors(MAX_COLOR
(MAX_COLORS is Private Const MAX_COLORS As Byte = 25 which I've translated just fine.)
Thanks!
//I tried:
private System.Drawing.Color[MAX_COLORS] SeparationColors;
//but got
//Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
private System.Drawing.Color[] SeparationColors;
SeparationColors = new System.Drawing.Color[MAX_COLORS];
//but got 2 additional errors.
private fixed System.Drawing.Color[] SeparationColors[MAX_COLORS];
//but got
//Fixed size buffer type must be one of the following: bool, byte, short, int, long, char, sbyte, ushort, uint, ulong, float or double
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I knew I was missing something simple!
Thanks!
Thanks!
Try it like this:
private System.Drawing.Color[] SeparationColors = new System.Drawing.Color[MAX_C OLORS];
private System.Drawing.Color[] SeparationColors = new System.Drawing.Color[MAX_C
This should work
Make sure that you have defined MAX_COLOR as int.
Make sure that you have defined MAX_COLOR as int.
int MAX_COLOR = 256;
System.Drawing.Color[] arrColor = new System.Drawing.Color[MAX_COLOR];
Hi ;)
class a
{
private const byte MAX_COLORS = 25;
private byte[] SeparationColors = new byte[MAX_COLORS];
public a()
{
}
}
The above obviously works, but there's really no explanation there.
In C#, the array declaration is considered part of the object's "type". For example, unlike VB and C++, we must say
int[] someArray
instead of
int someArray[]
Also, the arrays are, by default, initialized to "null" ("Nothing" in VB). So, the declaration
System.Drawing.Color[] myColors;
is the same as saying
System.Drawing.Color[] myColors = null; // Nothing, no colors in the array
However, if you do initialize your array, you MUST give the size (this is the downside of arrays all around). For example, the following is NOT valid:
System.Drawing.Color[] myColors = new System.Drawing.Color[];
The second set of brackets must contain a positive integer value, no matter what.
The "fixed" keyword in C# has an entirely different meaning than in other languages--it has to do with pointers, and basically "pinning" them so the GC doesn't run off with the object.
Hope I helped,
Nate
In C#, the array declaration is considered part of the object's "type". For example, unlike VB and C++, we must say
int[] someArray
instead of
int someArray[]
Also, the arrays are, by default, initialized to "null" ("Nothing" in VB). So, the declaration
System.Drawing.Color[] myColors;
is the same as saying
System.Drawing.Color[] myColors = null; // Nothing, no colors in the array
However, if you do initialize your array, you MUST give the size (this is the downside of arrays all around). For example, the following is NOT valid:
System.Drawing.Color[] myColors = new System.Drawing.Color[];
The second set of brackets must contain a positive integer value, no matter what.
The "fixed" keyword in C# has an entirely different meaning than in other languages--it has to do with pointers, and basically "pinning" them so the GC doesn't run off with the object.
Hope I helped,
Nate
it's working checked
class a
{
private const byte MAX_COLORS = 25;
private System.Drawing.Color[] SeparationColors = new System.Drawing.Color[MAX_COLORS];
public a()
{
}
}
private System.Drawing.Color[] SeparationColors = new System.Drawing.Color[MAX_C OLORS];
@tculler:
Thanks ;)
I don't speek english very well, thats why there was no explanation
Thanks ;)
I don't speek english very well, thats why there was no explanation
Open in new window