return an array's 2nd dimention as a single dimention array

  • Thread starter Thread starter Alex Leduc
  • Start date Start date
A

Alex Leduc

I'm trying to return an array's 2nd dimention as a single dimention array

private char[] array2d = new char[100, 5001];

public char[] ret(i) {
return;
}

I get a bunch of error for my return statement.

-Cannot implicitly convert ty 'char[*,*]' to char[]'
-Wrong number of indices inside [], expected '1'

I come from a C++ background. I don't want to have to use pointers but
surely there's a way to return a reference to the arrays' second dimention.

I'm begining to wonder if I should drop Arrays of chars entirely and
convert all the code to use string and StringBuilder (which suck IMO but
I digress).

Alexandre Leduc
 
Oop!!!! made some typos. sorry
private char[,] array2d = new char[100, 5001];

public char[] ret(i) {
return array2d;
}


Alex said:
I'm trying to return an array's 2nd dimention as a single dimention array

private char[] array2d = new char[100, 5001];

public char[] ret(i) {
return;
}

I get a bunch of error for my return statement.

-Cannot implicitly convert ty 'char[*,*]' to char[]'
-Wrong number of indices inside [], expected '1'

I come from a C++ background. I don't want to have to use pointers but
surely there's a way to return a reference to the arrays' second dimention.

I'm begining to wonder if I should drop Arrays of chars entirely and
convert all the code to use string and StringBuilder (which suck IMO but
I digress).

Alexandre Leduc
 
Alex Leduc said:
I'm trying to return an array's 2nd dimention as a single dimention array

private char[] array2d = new char[100, 5001];

public char[] ret(i) {
return;
}

I get a bunch of error for my return statement.

-Cannot implicitly convert ty 'char[*,*]' to char[]'
-Wrong number of indices inside [], expected '1'

I come from a C++ background. I don't want to have to use pointers but
surely there's a way to return a reference to the arrays' second dimention.


Not for a rectangular array. You can do it for a jagged array, however:

private char[][] array2d = new char[100][5001];

public char[] ret(i)
{
return array2d;
}
 
If I try to compile only this:

using System;

class MainClass
{
public static void Main(string[] args)
{
char[][] array2d = new char[100][5001];
}
}

I get 3 errors
- incorectly structured array initializer
- ; expected
- invalid expression term ']'
 
Alex Leduc said:
If I try to compile only this:

using System;

class MainClass
{
public static void Main(string[] args)
{
char[][] array2d = new char[100][5001];
}
}

I get 3 errors
- incorectly structured array initializer
- ; expected
- invalid expression term ']'

Oops - apologies, getting slightly confused with Java. You have to do
the following in C#:

char[][] array2d = new char[100][];
for (int i=0; i < 100; i++)
{
array2d = new char[5001];
}

I don't know why this limitation exists in C# - presumably it's to
steer you towards rectangular arrays when the second dimension is
effectively constant, but it's a pain for situations like this :(
 
Back
Top