I don’t understand why I am getting this error because my multi-dimensional array should function fine but it isn’t working in this case due to the listed error…I am very frustrated.
error is: Wrong number of indices inside []; expected 2
This is what I have:
public static void DisplayTopScore(string username, double score)
{
string[] highscores = System.IO.File.ReadAllLines(@"C:UsersPublicTestFolderWriteLines2.txt");
string[,] Temphighscores = new string[10, 2];
string[] TempScoresToSplit;
int counter=0;
foreach (string highScore in highscores)
{
TempScoresToSplit = highScore.Split(' ');
Temphighscores[counter][0]= TempScoresToSplit[0];
Temphighscores[counter][1]= TempScoresToSplit[1];
counter++;
}
}
}
The place where it says wrong number of indices are at these 2 lines:
Temphighscores[counter][0]= TempScoresToSplit[0];
Temphighscores[counter][1]= TempScoresToSplit[1];
- Remove From My Forums
-
Question
-
User-1225802336 posted
Hi i have a few errors i have managed to battle them to about 3 or 4, can you please point me in the right direction
The first error I get is «CS0022: Wrong number of indices inside [], expected ‘1’, and its coming from the code below;
for (lngIter = 0; lngIter < strData.Length;++lngIter)
{
if (strWhere[lngIter, 1] >
«»){
strPart1 = strPart1 + «`» + strData[lngIter, 0] +
«`,»;strPart2 = strPart2 + (char)34 + strData[lngIter, 1] + (char)34 +
«,»;}
Answers
-
User-170319569 posted
First thing first, you cannot use «>» operator to compare two strings, in C#. You might consider using alternate methods to check if the array position is NULL or not.
You can use
if (strWhere[IngIter,1].Length > 1)
{
}
-
Marked as answer by
Thursday, October 7, 2021 12:00 AM
-
Marked as answer by
-
User1944080475 posted
Pmillio — The problem is in the signature of the method buildQuery. If you are passing in a multidimensional array in C#, use a syntax like this «string[,]». The method is interpreting this array as unidimensional.
Hope this helps.static
public string buildQuery(string strAction,
string strTableName,
string[] strData, string[] strWhere,
string strWhereOverride,
string strLimit,
string strOffset, string
-
Marked as answer by
Anonymous
Thursday, October 7, 2021 12:00 AM
-
Marked as answer by
-
User-1034726716 posted
try this
if (strOrderBy.Lenght > 2)
-
Marked as answer by
Anonymous
Thursday, October 7, 2021 12:00 AM
-
Marked as answer by
Permalink
Cannot retrieve contributors at this time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CS0022: Wrong number of indexes `2′ inside [], expected `1′ | |
// Line: 9 | |
using System; | |
class ErrorCS0022 { | |
static void Main () { | |
int[] integer_array = {0, 1}; | |
Console.WriteLine («Test for Error CS0022: The compiler should say wrong number of fields inside the indexer«); | |
Console.WriteLine («Trying to access integer_array[2, 3] in a one-dimensional array: {0}«, integer_array[2,3]); | |
} | |
} |
Hi, I am trying to run the below C# code for martix, but getting error
Wrong number of indexes `2' inside [], expected `1' Compilation failed: 1 error(s), 0 warnings
here is my current
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Solution {
static int formingMagicSquare(int[][] s) {
int[] rowSum = new int[s.GetLength(0)], colSum = new int[s.GetLength(1)];
int sum = 0;
int magiconst = 15;
int cons = 0;
for (int r = 0; r < s.GetLength(0); r++) {
for (int c = 0; c < s.GetLength(1); c++) {
rowSum[r] += s[r, c];
}
sum += Math.Abs(rowSum[r] - magiconst);
}
return sum;
}
static void Main(string[] args) {
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int[][] s = new int[3][];
for (int i = 0; i < 3; i++) {
s[i] = Array.ConvertAll(Console.ReadLine().Split(' '), sTemp => Convert.ToInt32(sTemp));
}
int result = formingMagicSquare(s);
textWriter.WriteLine(result);
textWriter.Flush();
textWriter.Close();
}
}
How to resolve this error? Thanks
Asked by:- LuneAgile
0
: 4545
At:- 7/14/2018 4:31:02 PM
C#
multi-dimensional
array
1 Answers
Hi, all sorry I just made a mistake on my code, should rowSum[r] += s[r, c]; transformed to rowSum[r] += s[r] [c];
Thanks
1
At:- 7/15/2018 10:01:27 PM
Thanks for commenting back here about this.
0
By : vikas_jk — at :- 7/16/2018 1:16:38 PM
-
#1
I have some code that works fine in the IDE but gives errors when I try to compile. I can’t see what is wrong — or really understand what the error means, can anyone help?
The error I get is:
error CS0022: Wrong number of indices in [], expected ‘1’
The code is:
sub globals
Public Type(GName,X,Y)CompSource
sub writefile
FileOpen(f,fname,cRandom)
bin.New1(f,False)
bin.WriteSingle(ArrayLen(CompSource()))
For i = 0 To ArrayLen(CompSource())-1
bin.WriteString(CompSource(i).GName) ‘<— error here
bin.WriteSingle(CompSource(i).X)
bin.WriteSingle(CompSource(i).Y)
Next
-
#2
Hi,
I think this should work:
Public Type(GName,X,Y)CompSource[B](0)[/B]
-
#3
Thank you Wolfgang, You are quite right
David.
-
#4
I don’t understand. CompSource is an array?
-
#5
Hi Erel,
yes, later it gets dimmed as an array. It’s probably logical that I should have dimmed it as such in the global sub but the IDE never complained and it worked fine so I never thought about it again.
David.
Я не понимаю, почему я получаю эту ошибку, потому что мой многомерный массив должен работать нормально, но в этом случае он не работает из-за указанной ошибки… Я очень расстроен.
ошибка: Wrong number of indices inside []; expected 2
Это то, что у меня есть:
public static void DisplayTopScore(string username, double score)
{
string[] highscores = System.IO.File.ReadAllLines(@"C:UsersPublicTestFolderWriteLines2.txt");
string[,] Temphighscores = new string[10, 2];
string[] TempScoresToSplit;
int counter=0;
foreach (string highScore in highscores)
{
TempScoresToSplit = highScore.Split(' ');
Temphighscores[counter][0]= TempScoresToSplit[0];
Temphighscores[counter][1]= TempScoresToSplit[1];
counter++;
}
}
}
Место, где указано неправильное количество индексов, находится в этих двух строках:
Temphighscores[counter][0]= TempScoresToSplit[0];
Temphighscores[counter][1]= TempScoresToSplit[1];
1 ответы
Пытаться:
Temphighscores[counter, 0] = TempScoresToSplit[1];
Temphighscores[counter, 1] = TempScoresToSplit[1];
.
Компания Статья MSDN о многомерных массивах наверное стоит прочитать.
ответ дан 02 окт ’13, 13:10
Не тот ответ, который вы ищете? Просмотрите другие вопросы с метками
c#
multidimensional-array
or задайте свой вопрос.