This question already has answers here:
How to add items to a jagged array?
(2 answers)
Closed 8 months ago.
I'm trying to get some data and add those into the 2dimensional string array using foreach loop.
I made an integer variable for index reference but it keeps generating this exception error : 'Object reference not set to an instance of an object.'
can anyone give some solution for this?
the j.ID needs to be in the first place of every cell which contains three items.
string[][] pln_arr = new string[20][];
public int cnt = 0;
foreach (var j in sss)
{
MessageBox.Show("dddddddd");
pln_arr[cnt][0] = j.Id;
MessageBox.Show(pln_arr[0][0]);
cnt++;
}
You have to instantiate the inner array present in pln_arr. We can't directly assign value like pln_arr[cnt][0] = j.Id;
foreach (var j in sss)
{
MessageBox.Show("dddddddd");
pln_arr[cnt] = new string[1]; //This was missing.
//^^^^^ Update size of inner array as per your need.
pln_arr[cnt][0] = j.Id;
MessageBox.Show(pln_arr[0][0]);
cnt++;
}
Related
This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
Closed 8 months ago.
got datagridview with 3 data, I want to get the first cells data on each rows, when I use this kind of code
string[] DataGridViewArray = { };
for (int i = 0; i < DataGridView1.Rows.Count; i++)
{
DataGridViewArray[i] = DataGridView1.Rows[i].Cells[0].Value.ToString().Trim();
}
textBox1.Text = DataGridViewArray.ToString();
why it keep said 'Index was outside the bounds of the array.'
Array is fixed size and its size is determined when declaring it (eg. in your first line of code) .
However, you gave it no single element nor set its size with expected number of items its going to hold - which in your case should be DataGridView1.Rows.Count.
string[] DataGridViewArray = { };
Instead, if you replace that (line #1 code) as follows, it should work:
string[] DataGridViewArray = new string[DataGridView1.Rows.Count];
for (int i = 0; i < DataGridView1.Rows.Count; i++)
{
DataGridViewArray[i] =
DataGridView1.Rows[i].Cells[0].Value.ToString().Trim();
}
textBox1.Text = DataGridViewArray.ToString();
I don't have DataGridView1 but I did something similar just to show you the reason why you are getting out of index error and how to fix it.
Clostly look at the length of the array in a way you defined it vs when array length is provided when declaring it.
It's because you didn't properly instantiate your array so any iteration will be out of bounds.
This question already has answers here:
All possible array initialization syntaxes
(19 answers)
Closed 2 years ago.
I have a string representation of the Guid,I'm doing some testing and I'm hard-coding some GUIDs into the code. The below way can assign Guid of single value.
Guid g = new Guid("11223344-5566-7788-99AA-BBCCDDEEFF00");
Now, My question is how i can assign values to an array list of Guid[]?
You can use the below code snippet to assign values to an array list of Guid[]
Guid[] arr = new Guid[4];
arr.SetValue(new Guid("11223344-5566-7788-99AA-BBCCDDEEFF00"), 0);
arr.SetValue(new Guid("26a285e5-5435-4daa-8d36-3186bd441cff"), 1);
arr.SetValue(new Guid("6d850084-d55a-45a9-9e83-5adf22cfbf04"), 2);
arr.SetValue(new Guid("ec43e526-51c0-41bc-b04a-2f2e72c894cc"), 3);
You can use the below code snippet to fill out your Guid array with randomly generated Guids.
Guid[] arr = new Guid[10];
for (int i = 0; i < 10; i++)
{
arr.SetValue(Guid.NewGuid(), i);
}
This question already has answers here:
Append Loop Variable to Variable Name
(3 answers)
Closed 5 years ago.
I'm trying to check through a list of strings that aren't in an array, and are instead stored as 7 separate variables.
Is it possible to have the i in this case, be appended onto the end of the floor variable in order to select based on that? So something like...
for (int i = 0; i < 7; i++)
{
if (floor(i) ...)
}
Thank you.
Adding a collection of that strings wouldn't push your memory consumption (as strings are stored by reference, not data-copying). However, you'll get more suitable way to operate your data:
// init a collection container
var floors = new string[] {floor0, floor1, floor2, floor3, floor4, floor5, floor6, };
// old-school "array + loop"
for (int i = 0; i < 7; i++)
{
if (floors[i] ...)
}
// or functional-style LINQ
var interestingFloorsIterator = floors.Where(...condition predicate...);
var interestingFloorsArray = floors.Where(...condition predicate...).ToArray();
// etc... etc... etc...
No such availability in C#. You can however change your code to keep a list of a list of strings (List<List<string>>). In this way, you can get the index you need to work with.
List<List<string>> floors = new List<List<string>>();
//... populate data
for (int i = 0; i < floors.Count; i++)
{
List<string> floor = floors[i];
//Perform work on floor
}
Since I can only see a snippet of your code, this is just a rudimentary example.
This question already has answers here:
How to populate/instantiate a C# array with a single value?
(26 answers)
Closed 6 years ago.
So lets say we have an array of doubles like this one that is later used for other stuff.
double[] myArray = new double[25];
How would I go about replacing all the values in that array with a set value?
There are flashier ways, but
for (int i = 0; i < myArray.Length; ++i){
myArray[i] = foo; /*the new value*/
}
is clear and simple.
The "flashy" way (simply create a new array):
var array = Enumerable.Repeat(value, count).ToArray();
Or
Array.ConvertAll(array, e => value);
This question already has answers here:
How to delete an element from an array in C#
(12 answers)
Closed 7 years ago.
How can i remove an array item and get it back to the last array index?
Now i want to REMOVE "id2" in the list and get it back to the last index.
string[] ids = new string { "id1", "id2", "id3", "id4", "id5"};
An array is not a list where you can add or remove elements just that easy. Use a List<T> for that if you want to do it easy.
For your current question, this would do:
string idToRemove = 1;
string lastValue = ids[idToRemove];
for (int i = idToRemove + 1; i < ids.Length; i++)
{
ids[i - 1] = ids[i];
}
ids[ids.Length - 1] = lastValue;
It remembers the index of the element to 'remove'. It remembers it, then replaces every element after it with its next version. In the end it sets the last item value.