2d array in c#
-
Ok in java it looks like this: private static int maxState = 10; private int [][] avtomat = new int[maxState][256]; Now i would like to declare same array in c#. When i tryed private int [][] avtomat = new int[maxState][]; and fill array with values like this avtomat[0][0]=-1 I get error "An unhandled exception of type 'System.NullReferenceException' occurred in.. Additional information: Object reference not set to an instance of an object." What am i doing wrong? Thx
-
Ok in java it looks like this: private static int maxState = 10; private int [][] avtomat = new int[maxState][256]; Now i would like to declare same array in c#. When i tryed private int [][] avtomat = new int[maxState][]; and fill array with values like this avtomat[0][0]=-1 I get error "An unhandled exception of type 'System.NullReferenceException' occurred in.. Additional information: Object reference not set to an instance of an object." What am i doing wrong? Thx
private int [][] avtomat = new int[maxState][/* This is your problem. You haven't set it to anything */];
It's a null parameter.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
private int [][] avtomat = new int[maxState][/* This is your problem. You haven't set it to anything */];
It's a null parameter.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
But if i add value to second []..i get this error CS0178: Invalid rank specifier: expected ',' or ']'.
use something like
int blah[,] = new int[80,20]
[Edit] C# has two kind of multidimensional array: jagged and rectangular
int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };//rectangular
int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };//jagged or array of array
//as you can see the size of the second array in the jagged one can differ
//more on the topic => msdn lots of examples[/Edit]
modified on Friday, February 26, 2010 10:18 AM
-
Ok in java it looks like this: private static int maxState = 10; private int [][] avtomat = new int[maxState][256]; Now i would like to declare same array in c#. When i tryed private int [][] avtomat = new int[maxState][]; and fill array with values like this avtomat[0][0]=-1 I get error "An unhandled exception of type 'System.NullReferenceException' occurred in.. Additional information: Object reference not set to an instance of an object." What am i doing wrong? Thx
-
use something like
int blah[,] = new int[80,20]
[Edit] C# has two kind of multidimensional array: jagged and rectangular
int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };//rectangular
int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };//jagged or array of array
//as you can see the size of the second array in the jagged one can differ
//more on the topic => msdn lots of examples[/Edit]
modified on Friday, February 26, 2010 10:18 AM
-
But if i add value to second []..i get this error CS0178: Invalid rank specifier: expected ',' or ']'.
Thanks for 1 voting me for giving you the answer. I really appreciate it. :rolleyes:
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
private int [][] avtomat = new int[maxState][/* This is your problem. You haven't set it to anything */];
It's a null parameter.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Hey Pete, I hope you don't mean
private int [][] avtomat = new int[maxState][30];
Because that is not C#! Actually it's unclear what you really mean here, but if you interpret it as above you get the compiler error the OP quoted
-
use something like
int blah[,] = new int[80,20]
[Edit] C# has two kind of multidimensional array: jagged and rectangular
int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };//rectangular
int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };//jagged or array of array
//as you can see the size of the second array in the jagged one can differ
//more on the topic => msdn lots of examples[/Edit]
modified on Friday, February 26, 2010 10:18 AM
-
Now when i did like this int[,] array=new int[10,256] it gives me error CS0022: Wrong number of indices inside []; expected '2' when i am adding values->array[10][2]=-1 for ex. I tryed few examples from msdn still can't get it to work!!
-
Totally OffTopic but what does OP stands for? I mean I know it has something to do with the person that posted the question/message but what's OP from? Object => hope and know not ;)
-
yeah this works..thanks a lot;D I need something else please.. how to write this from java in c#?
try { datoteka.mark(1); ? int tmp = datoteka.read(); datoteka.reset(); ? return tmp;
-
Hey Pete, I hope you don't mean
private int [][] avtomat = new int[maxState][30];
Because that is not C#! Actually it's unclear what you really mean here, but if you interpret it as above you get the compiler error the OP quoted
That's not what I meant. I was pointing out where the null reference was occuring. If you tried the example you quote here, you get an InvalidFieldSpecifier problem, not a null reference exception.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
That's not what I meant. I was pointing out where the null reference was occuring. If you tried the example you quote here, you get an InvalidFieldSpecifier problem, not a null reference exception.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
Pete O'Hanlon wrote:
That's not what I meant. I was pointing out where the null reference was occuring.
Ok, then IMO it's an odd way of pointing that out..
Pete O'Hanlon wrote:
If you tried the example you quote here, you get an InvalidFieldSpecifier problem, not a null reference exception.
You'd get Invalid rank specifier: expected ',' or ']' as the OP also posted, which is exactly my point, if that had been what you meant, you would have given him invalid code. And from the OP's reaction, that is how what he understood your post to mean..
-
Pete O'Hanlon wrote:
That's not what I meant. I was pointing out where the null reference was occuring.
Ok, then IMO it's an odd way of pointing that out..
Pete O'Hanlon wrote:
If you tried the example you quote here, you get an InvalidFieldSpecifier problem, not a null reference exception.
You'd get Invalid rank specifier: expected ',' or ']' as the OP also posted, which is exactly my point, if that had been what you meant, you would have given him invalid code. And from the OP's reaction, that is how what he understood your post to mean..
I didn't give him code - that was the whole point. He asked why he was getting a null reference exception; I told him. What he did next was up to him - without knowing what he was trying to achieve with it, I did not feel it was my place to suggest what he should do to resolve this and nowhere in my original answer did I suggest that he try to put a parameter to put into there. I will not be held responsible for somebody reading more into an answer than was there.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
I didn't give him code - that was the whole point. He asked why he was getting a null reference exception; I told him. What he did next was up to him - without knowing what he was trying to achieve with it, I did not feel it was my place to suggest what he should do to resolve this and nowhere in my original answer did I suggest that he try to put a parameter to put into there. I will not be held responsible for somebody reading more into an answer than was there.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
I don't think I agree Pete, your post looked a lot like "code with something you have to fill in" - it still looks that way to me, and going by his reaction it also looked that way to the OP. But of course I'm not holding you responsible in any way (this is teh interwebs), I just tried (and failed, clearly) to point out how it would be confusing - you really don't need to get all defensive over it :) So, my apologies.
-
I don't think I agree Pete, your post looked a lot like "code with something you have to fill in" - it still looks that way to me, and going by his reaction it also looked that way to the OP. But of course I'm not holding you responsible in any way (this is teh interwebs), I just tried (and failed, clearly) to point out how it would be confusing - you really don't need to get all defensive over it :) So, my apologies.
I can see your point about why it looked that way, but it was never my intention to make it a "fill this bit in and it magically works". This is what I get when answering a post when juggling an installation onto 360 machines at the same time. Perhaps I could have been clearer, but I'm not getting defensive on this - I was just trying to clarify that I knew this wasn't valid C#, and my answer wasn't intended to suggest it was. Next time I'll try not to be as "clever" with where I put the pointer to the problem.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Ok in java it looks like this: private static int maxState = 10; private int [][] avtomat = new int[maxState][256]; Now i would like to declare same array in c#. When i tryed private int [][] avtomat = new int[maxState][]; and fill array with values like this avtomat[0][0]=-1 I get error "An unhandled exception of type 'System.NullReferenceException' occurred in.. Additional information: Object reference not set to an instance of an object." What am i doing wrong? Thx
The Java rectangular array in your example is a special case in Java since Java only has jagged arrays (arrays of arrays), not multidimensional arrays, but allows a special rectangular jagged array initialization. The C# equivalent to this is: [code]//ORIGINAL LINE: private int[][] avtomat = new int[maxState][256]; //JAVA TO VB & C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: private int[][] avtomat = RectangularArrays.ReturnRectangularIntArray(maxState, 256); //---------------------------------------------------------------------------------------- // Copyright © 2008 - 2010 Tangible Software Solutions Inc. // This class can be used by anyone provided that the copyright notice remains intact. // // This class provides the logic to simulate Java rectangular arrays, which are jagged // arrays with inner arrays of the same length. //---------------------------------------------------------------------------------------- internal static class RectangularArrays { internal static int[][] ReturnRectangularIntArray(int Size1, int Size2) { int[][] Array = new int[Size1][]; for (int Array1 = 0; Array1 < Size1; Array1++) { Array[Array1] = new int[Size2]; } return Array; } }[/code] David Anton Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com