I have a litte problem here. I have a two-dimensional array which is [5,5] big.
I have a windows form with 25 buttons. Now I want to store the buttons in the object array, but my problem is, how do I tell the program to know which button to put in the array? Is it possible in some kind of this way:
//_array[i] = button(i);
This is my first time storing objects in an array and I dont know how to do that.
EDIT: The buttons have all the standard names (button1,button,button3...)
EDIT2: I know how to it by hand (_array[x,y] = button1) but I want to know how to it ith a for-loop.
You could filter the controls by using IEnumerable.OfType():
//get all buttons and order them by name
var buttons = Controls.OfType<Button>().OrderBy(x => x.Name).ToList();
for(int i = 0; i < 5; i++)
for(int j = 0; j < 5; j++)
_array[i, j] = buttons[i*5+j];
You can access the buttons using their name as index of the Controls collection:
for (int i = 0; i < 5; i++) {
for (int k = 0; k < 5; k++) {
_array[i, k] = Controls["Button" + (5 * i + k + 1).ToString()];
}
}
Try this:
for (int i=0; i<5; i++)
for (int j=0; j<5; j++)
_array[i,j] = Controls.Item["Button" + (i*5 +j).ToString()];
Related
This question already has an answer here:
What is an "index out of range" exception, and how do I fix it? [duplicate]
(1 answer)
Closed 4 years ago.
for (int i = 0; i <= 6; i++)
{
string[] doors = new string[6];
doors[i] = "#";
for (int j = 1; j <=i; j++)
{
Console.Write(doors[j]);
}
Console.Writeline():
}
Hi guys. I need to print # one and then # twice, until i get to six times. It says System.index.out.of.range. How come?
You should try to extend your array, it's limited to 6 elements but you try to access 7 elements as you go through 0 to 6.
for (int i = 0; i <= 6; i++)
{
string[] doors = new string[7];
doors[i] = "#";
for (int j = 1; j <=i; j++)
{
Console.Write(doors[j]);
}
Console.Writeline():
}
If
I need to print # one and then # twice, until i get to six times.
You don't want any array - string[] doors = new string[6];, just loops:
for (int line = 1; line <= 6; ++line) {
for (int column = 1; column <= line; ++column) {
Console.Write('#');
}
Console.WriteLine();
}
If you have to work with array (i.e. array will be used somewhere else), get rid of magic numbers:
// Create and fill the array
string[] doors = new string[6];
for (int i = 0; i < doors.Length; i++)
doors[i] = "#";
// Printing out the array in the desired view
for (int i = 0; i < doors.Length; i++) {
for (int j = 0; j < i; j++) {
Console.Write(doors[j]);
}
Console.Writeline();
}
Please, notice that arrays are zero-based (array with 6 items has 0..5 indexes for them)
because it is out of range.
change it to this:
for (int i = 0; i <= 6; i++)
{
string[] doors = new string[6];
doors[i] = "#";
for (int j = 0; j <=i.length; j++)
{
Console.Write(doors[j]);
}
Console.Writeline():
}
No need to use 2 loops. Just repeat that character
for (int i = 0; i <= 6; i++)
{
Console.Write(new String("#",i));
Console.WriteLine():
}
For this code in Visual Studio
Point[,] point = new Point[9, 10];
for (int i = 0; i < 9; i++)
{
for(int j = 0; i < 10; j++)
{
point[i, j].X = i;//mark1
point[i, j].Y = j;
}
}
at //mark1,system tell me"index exceeded the number of group boundaries"
Why?
you are doing
for (int j = 0; i < 10; j++)
so the condition i < 10 which is maybe a typo is causing your loop to go out of the range in the array (you are then trying to access the eleement 0, 10 in the array)
replace that with:
for (int j = 0; j < 10; j++)
I'm getting an Index was outside the bounds of the array.
string[] paths = {
"\\\\server\\c$\\folder\\subfolder\\user1\\300\\1\\abc.docx",
"\\\\server\\c$\\folder\\subfolder\\user2\\400\\1\\xyz.docx",
};
FileInfo[] f = new FileInfo[paths.Length];
for (int i = 0; i <= paths.Length; i++)
{
f[i] = new FileInfo(paths[i]);
Console.WriteLine(f[i].Length);
}
I can't seem to figure out why, any ideas?
use < instead of <=
for (int i = 0; i < paths.Length; i++)
{
f[i] = new FileInfo(paths[i]);
Console.WriteLine(f[i].Length);
}
Arrays start counting itens from 0. So, if you have an array with length 2, your objects will be in position [0] and [1]. If you try to access the position [2], you'll get the Index was outside the bounds of the array exception, because index 2 doesn't exist in this array.
In your for loop, you're using <= paths.Length. Your paths length is 2. 2 is less or equals 2, so your code will be executed like this
f[2] = new FileInfo(paths[2]) //Position 2 doesn't exist
To solve this, just change from:
for (int i = 0; i <= paths.Length; i++)
To:
for (int i = 0; i < paths.Length; i++)
You have two items in your array
paths[0], paths[1]
Your looping over three items
paths[0], paths[1], paths[2]
To correct this, change
for (int i = 0; i <= paths.Length; i++)
to
for (int i = 0; i < paths.Length; i++)
I have created a multidimensional array.I wants to display it in the .aspx page. please help.my code is here.It doesnt shows the array values
string[,] array_questions = new string[dt.Rows.Count, dt.Columns.Count];
for (i = 0; i < dt.Rows.Count; i++)
{
for (j = 0; j < dt.Columns.Count; j++)
{
array_questions[i, j] = dt.Rows[i][j].ToString();
//TextBox1.Text = array_questions[i,j];
}
}
Console.Write(array_questions[i, j] + " ");
you can also use Response.write
Console.Write is used for console applications.
If you want to display the data on the aspx webpage you need to create a webcontrol (for example label) and set its text property with an appropriate value.
If you want to write the values in the table in the console visual studio, call the method
Console.Out.Write() and not Console.Write().
Otherwise call Response.Write() to write on the page
// display the array
for (int i = 0; i < array_questions.GetLength(0); i++)
{
for (int j = 0; j < array_questions.GetLength(1); j++)
{
Response.Write("i="+i+" "+"j="+j);
Response.Write("<br>");
}
}
How do you create a multi-dimensional data structure in C#?
In my mind it works like so:
List<List<int>> results = new List<List<int>>();
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
results[i][j] = 0;
}
}
This doesn't work (it throws an ArgumentOutOfRangeException). Is there a multi-dimensional structure in C# that allows me to access members through their indexes?
The problem here is that List doesn't automatically create elements. To initialise a List<List<T>> you need something like this:
List<List<int>> results = new List<List<int>>();
for (int i = 0; i < 10; i++)
{
results.Add(new List<int>());
for (int j = 0; j < 10; j++)
{
results[i].Add(0);
}
}
Note that setting Capacity is not sufficient, you need to call Add the number of times you need. Alternatively, you can simplify things by using Linq's Enumerable class:
List<List<int>> results = new List<List<int>>();
for (int i = 0; i < 10; i++)
{
results.Add(new List<int>());
results[i].AddRange(Enumerable.Repeat(0, 10));
}
Again, note that Enumerable.Repeat(new List<int>(), 10) will not work, since it will add 10 references to the same list.
Another approach using Linq to the extreme:
List<List<int>> results = Enumerable.Repeat(0, 10)
.Select(i => Enumerable.Repeat(0, 10).ToList())
.ToList();
(The unused parameter i is necessary to ensure that you don't reference the same list ten times as discussed above.)
Finally, to access elements, you can use exactly the notation you used before. Once the elements have been added, they can be read or modified as shown:
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
results[i][j] = 2;
int x = results[i][j];
}
}
If you know the dimensions of your structure in advance, and you do not plan to add or remove elements, then a 2D array sounds like your thing:
int[,] n = new int[10, 20];
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
n[i, j] = ...
};
};
You have to create the lists and initialize them with zeros before you can't start indexing into them.
List<List<int>> results = new List<List<int>>();
for (int i = 0; i < 10; i++)
{
results.Add(new List<int>(Enumerable.Repeat(0, 10)));
}
You have to actually 1) create each of the inner lists, and 2) set them to that size.
var Results = Enumerable.Range(0, 10).Select(i => Enumerable.Repeat(0, 10).ToList()).ToList();
I'm a bit of a Linq addict, though.
You could use a datatable and add columns and rows? You would then be able to reference them by name or index.