|
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][]; 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.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
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} };
int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
[/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!!
|
|
|
|
|
|
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;
|
|
|
|
|
Maybe this?
try
{
datoteka.mark(1);
int tmp = datoteka.read();
datoteka.reset();
return tmp;
}
catch { return -1; }
|
|
|
|
|
Thanks for 1 voting me for giving you the answer. I really appreciate it. "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.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
|
|
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.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
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.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
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.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
It's because you now have an array of int[] , all of which are null. You have to create the sub-array's as well.
|
|
|
|
|
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
|
|
|
|
|
Hi All
Do you know if it is possible to split a string in several lines in C#
In C++ I can write
std::cout << "this is a very "
"very very very "
"very very very "
"long string split into 4 lines that will be printed in one";
Do you know if I can do the same in C#?
Thanks
MN
|
|
|
|
|
Yes:
Console.WriteLine( "this is a very " +
"very very very " +
"very very very " +
"long string split into 4 lines that will be printed in one"); Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
|
|
|
|
|
Hi Thanks!
Is there any other way without the + char at the end?
For each + I think that C# due to how the String are will create
in memory a new object that is the result of "String1" + "String2".
I need just some facility so that compiler will recognize that it
must print on the same line.
This was the purpose of my questions even if I didn't make it explicit
|
|
|
|
|
Hi,
the C# compiler understands concatenation of string literals and executes them once, while compiling. It all results in one big string literal before your code actually gets executed, just the way you intended it. You could check with Reflector[^].
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
modified on Friday, February 26, 2010 12:57 PM
|
|
|
|