Coming from Python, I could easily do array[0][0], but doesn't seem like it in C#.
When I try to fetch data from my MySQL database, it gives me a 2D array, hence the need to use a 2D array.
Array looks like this:
[[hello], [world]]
I'd like to just fetch the string "hello". How would I manage that?
You use
string value = array[0,0];
Here:
var array= new String[,] {{"Hello","World"}};
string value=array[0,0];
You can access multidimensional arrays like this:
int val = a[2,3];
The above statement takes 4th element from the 3rd row of the array.
In your case it would be:
string val = a[0,0];
Pretty straightforward: arrray[0, 0]
Reference
Related
I have a multi dimensional array but i want to be able to instantiate and initialize it in one line does anyone know how to do this?
Here is what i have at the moment.
int[,] Columns = [3,2];
Columns[0,0]= 1;
Columns[1,0]= 0;
Columns[2,0]= 2;
Columns[0,1]= "Distinct";
Columns[1,1]= "Sum";
Columns[2,1]= "Distinct";
im trying to get somthing along the lines of:
If anyone could help it would be much appreciated.
You can, you can use something called a collection initializer with rectangular arrays! But you can't declare the array type as int and try and store strings (such as "Sum") in it too.
You could use the object type to store different data types in the same collection:
object[,] Columns = { { 1, 0, 2 }, { "Distinct", "Sum", "Distinct" } };
It's hard for me to explain, so let me show it with pseudo code:
ObjectX
{
int a;
string b;
}
List<ObjectX> list = //some list of objectsX//
int [] array = list.Select(obj=>obj.a);
I want to fill an array of ints with ints from objectsX, using only one line of linq.
You were almost there:
int[] array = list.Select(obj=>obj.a).ToArray();
you need to just add ToArray at the end
The only problem in your code is that Select returns IEnumerable.
Convert it to an array:
int[] array = list.Select(obj=>obj.a).ToArray();
When creating webservices, in c#, I have found it very useful to pass back jagged arrays, i.e. string[][]
I also found a neat trick to build these in a simple way in my code, which was to create a List and convert it by doing a ToArray() call.
e.g.
public string[][] myws() {
List<string[]> output = new List<string[]>();
return output.ToArray();
}
I would like to be able to employ a similar solution, but I can't think how to do something similar with a 3 level jagged array or string[][][], without resorting to loops and such.
Regards
Martin
You can get there by doing a Select() which converts each inner List<string> to an array using ToArray(), and then converting those results using ToArray():
var x = new List<List<string[]>>();
string[][][] y = x.Select(a => a.ToArray()).ToArray();
And so on for as many levels deep as you'd want to go.
I have been using
int.TryParse(_dtPOSettings.Rows[0]["PASID"].ToString(), out categoryID);
to get string value converted to int. This is working fine when PASID value is a single value.
Now I store multiple values in database as a ',' seperated list. When I get them back i'm splitting them and storing them to a string array. Now i want int.TryParse to take values of string array and convert them to int values and put them back to a int typed list.
But the out param does not support this. Please suggest on how to code this.
int.TryParse(PASIDValues[i].ToString(), out PASIDintValues[i]);
Thanks in advance.
Simply declare a variable to hold value temporarily and if the parse is successful, assign value to array index
int temp = 0;
if(int.TryParse(PASIDValues[i].ToString(), out temp))
PASIDintValues[i] = temp;
Be sure to instantiate the full length of the values you plan to store as a result of your conversion, and use a temporary variable to get the value from int.TryParse.
int[] PASIDintValues = new int[PASIDValues.Count()];
for(int i = 0; i < PASIDValues.Count() - 1; i ++)
{
int tmp = 0;
if(int.TryParse(PASIDValues[i].ToString(), out tmp))
results[i] = tmp;
}
You can't make a reference to a List element, but you can make one to an array element. Just make PASIDintValues an array instead of a list and it should work.
I'm in a little bit of a bind. I'm working with a legacy system that contains a bunch of delimited strings which I need to parse. Unfortunately, the strings need to be ordered based on the first part of the string. The array looks something like
array[0] = "10|JohnSmith|82";
array[1] = "1|MaryJane|62";
array[2] = "3|TomJones|77";
So I'd like the array to order to look like
array[0] = "1|MaryJane|62";
array[1] = "3|TomJones|77";
array[2] = "10|JohnSmith|82";
I thought about doing a 2 dimensional array to grab the first part and leave the string in the second part, but can I mix types in a two dimensional array like that?
I'm not sure how to handle this situation, can anyone help? Thanks!
Call Array.Sort, but passing in a custom implementation of IComparer<string>:
// Give it a proper name really :)
public class IndexComparer : IComparer<string>
{
public int Compare(string first, string second)
{
// I'll leave you to decide what to do if the format is wrong
int firstIndex = GetIndex(first);
int secondIndex = GetIndex(second);
return firstIndex.CompareTo(secondIndex);
}
private static int GetIndex(string text)
{
int pipeIndex = text.IndexOf('|');
return int.Parse(text.Substring(0, pipeIndex));
}
}
Alternatively, convert from a string array into an array of custom types by splitting the string up appropriately. This will make life easier if you're going to do further work on the array, but if you only need to sort the values, then you might as well use the code above.
You did say that you need to parse the strings - so is there any particular reason why you'd want to parse them before sorting them?
new[] {
"10|JohnSmith|82",
"1|MaryJane|62",
"3|TomJones|77",
}.OrderBy(x => int.Parse(x.Split('|')[0]));
Use an ArrayList (http://msdn.microsoft.com/en-us/library/system.collections.arraylist_methods(VS.80).aspx) so you can sort it.
If the array is large, you will want to extract the initial integers all in one pass, so you are not parsing strings at every comparison. IMO, you really want to encapsulate the information encoded in the strings into a class first. Then sort the array of those objects.
Something like:
class Person {
int Index { get; }
string Name { get; }
int Age { get; } // just guessing the semantic meaning
}
So then:
Map your encoded string into an ArrayList of Person objects.
Then use ArrayList.Sort(IComparer) where your comparer only looks at the Index.
This will likely perform better than using parse in every comparison.
for lolz
Array.Sort(array, ((x,y) => (int.Parse(x.Split('|')[0]) < int.Parse(y.Split('|')[0])) ? -1 : (int.Parse(x.Split('|')[0]) > int.Parse(y.Split('|')[0])) ? 1 : 0));