Solved

VB.Net -> C# translation -- Array declaration

Posted on 2009-05-06
10
869 Views
Last Modified: 2013-12-17
This is stupid ... but ... how does the following line of VB.Net translate into C#?

Private SeparationColors(MAX_COLORS) As System.Drawing.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

Open in new window

0
Comment
Question by:Daniel Wilson
10 Comments
 
LVL 6

Accepted Solution

by:
Cebik earned 250 total points
ID: 24315354

private System.Drawing.Color[] SeparationColors = new System.Drawing.Color[MAX_COLORS];

Open in new window

0
 
LVL 6

Expert Comment

by:Cebik
ID: 24315384

class a
{
    private System.Drawing.Color[] SeparationColors;
 
    public a()
    {
        SeparationColors = new System.Drawing.Color[MAX_COLORS];
    }
}

Open in new window

0
 
LVL 32

Author Closing Comment

by:Daniel Wilson
ID: 31578478
I knew I was missing something simple!

Thanks!
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 62

Expert Comment

by:Fernando Soto
ID: 24315404
Try it like this:

private System.Drawing.Color[] SeparationColors = new System.Drawing.Color[MAX_COLORS];
0
 
LVL 16

Expert Comment

by:CuteBug
ID: 24315405
This should work

Make sure that you have defined MAX_COLOR as int.

int MAX_COLOR = 256;
System.Drawing.Color[] arrColor = new System.Drawing.Color[MAX_COLOR];

Open in new window

0
 
LVL 6

Expert Comment

by:Cebik
ID: 24315422
Hi ;)

class a
{
    private const byte MAX_COLORS = 25;
    private byte[] SeparationColors = new byte[MAX_COLORS];
 
    public a()
    {
    }
}

Open in new window

0
 
LVL 9

Expert Comment

by:tculler
ID: 24315428
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
0
 
LVL 6

Expert Comment

by:Cebik
ID: 24315435
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()
    {
    }
}

Open in new window

0
 
LVL 4

Expert Comment

by:cauos
ID: 24315447
private System.Drawing.Color[] SeparationColors = new System.Drawing.Color[MAX_COLORS];
0
 
LVL 6

Expert Comment

by:Cebik
ID: 24315818
@tculler:

Thanks ;)
I don't speek english very well, thats why there was no explanation
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now