I need to create a multidimensional guard List three values, X, Y and Z, and I need a List that is because once the value is queried, the array must be removed.
The query would look something like this: List [0] [0] = X, List [0] [a] = Y and List [0] [2] = X, so that I can remove only the index 0 and he already remove all the other three.
If you need to create a multidimensional list, you can always create a list of lists like so:
var multiDimensionalList = new List<List<string>>{
new List<string>{"A","B","C"},
new List<string>{"D","E","F"},
new List<string>{"G","H","I"},
};
Console.WriteLine(multiDimensionalList[2][1]); // Prints H
multiDimensionalList[2].RemoveAt(1);
Console.WriteLine(multiDimensionalList[2][1]); // Prints I
multiDimensionalList[2][1] = "Q";
Console.WriteLine(multiDimensionalList[2][1]); // Prints Q
Be aware though that attempting to replace a value that doesn't exist by way of assignment will throw an exception:
multiDimensionalList[2][5] = "R"; // Throws an ArgumentOutOfRangeException
Your question is very hard to understand, but perhaps what you are looking for can be accomplished with an array of arrays? This is how multidimensional arrays are implemented in some languages anyways.
In your case you might be using a List of List's: List>. And this would satisfy your requirement to remove "all the other three" by removing the first element in the outer List<> object.
I'm sorry but your question is a little bit hard to understand, but I will take a stab at it. Please don't interchange the words Array and List as they are different yet related ideas in C#. I believe that you mean Array with your use of [] brackets. Although you might want to consider using lists as they have a nice way to remove certain elements from a list by using the element. the MSDN has some good information as to how you might proceed.
List(T).Remove method
the list will restructure it self to remove or add elements as desired.
I am not sure I am following your logic as you are using both strings and integers as your second indexer, and referencing X twice, but not Z. Assuming these are typos, I am going to take a guess at what you want.
Have you considered a custom type with X, Y, AND Z properties, and an indexer to give you the behavior you described:
You also don't mention what types your values are, so I am using object, but feel free to substitute your own type (or a generic type)
public class MyType
{
private object[] backingArray = new object[3];
public object this[int index]
{
get { return backingArray[index]; }
set { backingArray[index] = value; }
}
public object X
{
get { return backingArray[0]; }
set { backingArray[0] = value; }
}
public object Y
{
get { return backingArray[1]; }
set { backingArray[1] = value; }
}
public object Z
{
get { return backingArray[2]; }
set { backingArray[2] = value; }
}
}
You could then use it like this:
List<MyType> list = new List<MyType>();
list = PopulateList(); // fill list with values
var x = list[0][0];
var y = list[0][1];
var z = list[0][2];
Of course, this implementation depends on your 2nd dimension always consisting of 3 elements. If it will not be consistent, then one of the other answers abound for your needs.
Related
I need to read a line in a file.
Based on the first 3 characters in the file, I can determine a type of record.
This indicates the number of strings the line needs to be split into.
I need to hold all lines of the same type in a List.
How do I do this?
My sample file would look like
123|gf|hf|gr|9
145*gf*43*434*9*645*554
123|grf|fe|yr|9
So all 123 would be in a list of string array type of length 4 like :
public List<string[]> NTE =new List<string[4]>();
Except declaring a length isn't being accepted by the compiler
You could use
List<string[]> NTE =new List<string[]>();
And then as you need to add an element to the NTE, you only need to specify that the size will be 4:
NTE.Add(new string[4]); //here it is defined having size of 4, not in the list declaration
Then when you use it:
NTE[0] = ...something
That is going to be a string[4] array
class ArrayofFour
{
string[] a = new string[4];
public string this[int i]
{
get
{
return a[i];
}
set
{
a[i] = value;
}
}
}
Use the ArrayofFour instead of an array, you can use it like an array using the indexers. This will take care of validation you need.
Then you can have a List<ArrayofFour> NTE = new List<ArrayofFour>();
I think this is what you need or at least help you get there.
I am new to c# and am trying to build an array of arrays of items. I have looked at 2d arrays and jagged arrays and simply can't work out what i'm supposed to be using and can not get it to work. It's not so much building the array it's then looping through it to interrogate the array elements. I'm working within an existing library which is where all the variables come from, most of the other supporting code I've left out as it's not relevant. Once a instance is found i'm then trying to update a field from 0 to 1. Many thanks for any help in advance.
//Declare array
private double[,] myOpenTrades;
private void mymethod (double score, double RSIComboScore, int type, int line)
{
myOpenTrades[line,0] = type;
myOpenTrades[line,1] = CurrentBar;
myOpenTrades[line,2] = Close[0];
myOpenTrades[line,3] = rewardClose;
myOpenTrades[line,4] = riskClose;
myOpenTrades[line,5] = score;
myOpenTrades[line,6] = RSIComboScore;
myOpenTrades[line,7] = this.getSMATrend();
myOpenTrades[line,8] = Math.Round(NSRSICS(5, 15, 60, 240).Rsi200AVGPlot[0]);
myOpenTrades[line,9] = myReward;
myOpenTrades[line,10] = myRisk;
myOpenTrades[line,11] = 0;
}
protected override void OnMyChange()
{
foreach(double[] row in myOpenTrades)
{
if(Close[0] >= row[3] && row[11]==0)
{
Print("WIN:"+row[10]);
row[11]=1;
}
else if(Close[0] >= row[4] && row[11]==0)
{
Print("LOSE:"+row[9]);
row[11]=1;
}
}
{
I don't know why this is being downvoted, it seems like a legitimate question from a new user who made some effort.
To answer your question, an array of arrays is not the best choice, because from a logical organization perspective you don't have a "grid" (i.e. 2D array) of the same item, you have multiple items as one record, and an array of records. Not to mention that you appear to be trying to mix and match types; your array is declared as double but the first record type is an integer.
I would recommend using a class as follows:
class OpenTrades
{
public int Type;
public Bar CurrentBar;
public double Score;
// etc...
}
(This is assume that CurrentBar's type is Bar; you'll have to substitute Bar with whatever that type actually is.)
Then you would instantiate an array of your class like this:
OpenTrades[] myOpenTrades = new OpenTrades[11]; // This will create an array of 11 elements, indices 0 to 10
Now in your mymethod function you can assign values to each of your members.
myOpenTrades[line].Type = type;
myOpenTrades[line].Bar = CurrentBar;
myOpenTrades[line].Score = score;
// etc ...
I read about indexers in MSDN - Indexers which explains how we can use objects like array with index i.e. just like normal Array. However, I think we can create array of objects like
point[] array = new point[100];
So what is the special advantages Indexer over object array?
If all you are after is a collection of objects then an indexer has absolutely no benefit over an array. However, if you need to store state as well as a collection, that's where an indexer shines.
For example, consider the following
public class Tree
{
private Branch[] branches = new Branch[100];
...
public string Name { get; set; }
public Branch this[int i]
{
get
{
return branches[i];
}
}
}
Tree holds an internal collection but also has state of it's own. Having an indexer property allows for simple access to the underlying collection e.g.
tree.Name = "Tree";
var branch = tree[0];
Not in this case that you have mentioned above. However, if you have anything that cannot be represented as an array will be a good example for Indexers to be used.
One .Net framework example is Dictionary. If you see the definition of Dictionary type in .Net you will find that they let you get an access of value through key. So that is a good example of using indexers where the index is presented as string.
Without indexers, how would you do that? of course by index value but it cannot be of type string then, will that be user friendly? I guess not!
So indexers gives you an opportunity to represent your code well.
Similarly, in case of point type, of course you can access the value of by index i.e. 0,1,2...99. What if you want to make more user friendly, such as point["x"]. That is where Indexers will help you.
Another example I could think of how about if you want to access your stack like s1 instead of push and s[0] instead of pop method.
There is a very good example of indexers by Microsoft where you can access file byte by byte by providing character location as index.
http://msdn.microsoft.com/en-us/library/aa288465(v=vs.71).aspx
In your line of code, you've defined an array of point objects, whatever those might be.
point[] array = new point[100];
Assuming you have direct access to the array, you can access the first element in your array like this:
var firstPoint = array[0];
The page you linked to is showing you how you could access that array, if it were defined inside your class, and you didn't have direct access to the array (since it's private).
For example, we could modify the example on that page to use your array:
class SampleCollection
{
private Point[] arr = new Point[100];
public Point this[int i]
{
get { return arr[i]; }
set { arr[i] = value; }
}
}
Then you could access the first element in the array like this:
var sc = new SampleCollection();
var item1 = sc[0];
That isn't an indexer.
An indexer is not used to create an array of objects, it is actually an operator overload to the '[]' operator.
An example for it's use would be if you wanted to make a List wrapper class.
In order to preserve the square braces functionality you would need (and want) to override the square braces operator. This is done via an indexer method.
how to get direct value from a list that contain an array ?
hey guys ,
i want to directly get a certain value from a list that contain an array
List<int[]> myList = new List<int[]>();
myList.Add( new int[2] { 10, 11 } );
its clear for me to get this using foreach loop like
foreach ( int[] p in mylist)
console.write(p[0]);
i do want to retrive this single data using expression like list[0] for list of integers
thanks..
Your question is very unclear, but if you know the array index within the array, you can use:
int value = myList[listIndex][arrayIndex];
Effectively this is just doing:
int[] array = myList[listIndex];
int value = array[arrayIndex];
im not entirely sure what you mean . . .
it would look like a 2d array: myList[position of array in list][position of item in selected array]. this is because a list is generic container and the overloaded bracket operator will return the specific type (which in this case is an array), that then enables you to use the bracket again to refer to the items contained in the array.
the snipplet you wrote actually only iterates the first item foreach array in your list (was this on purpose)?
in essence, you kind of need 2 pieces of information unless you only want the first item in each list (position 0) in which case you would create a new container class, implement the IList interface and overload the bracket operator like this:
public int this[int index]
{
get
{
return myList[index][0];
}
set
{
myList[index][0] = value;
}
}
Is it possible to know the length of a string array - without having an object instance - via reflection?
E.g. in this case: 2.
public string[] Key
{
get { return new string[] { Name, Type }; }
}
EDIT: ok, I will not try to do this, it doesn't make much sense.
Perhaps you mean "without having the exact type of the Array". C# Arrays all derive from Array, so you can cast an Array reference to Array and use the Length property.
If you TRULY wants to reflect the property,
var type = typeof(MyClass);
var prop = type.GetProperty("Key");
var method = prop.GetGetMethod();
var body = method.GetMethodBody();
var ils = body.GetILAsByteArray();
from here you'll have to use one of the various libraries to decode bytes to IL OpCodes (for example https://gist.github.com/104001) . The OpCode you are looking for is newarr. The last push of an int32 before the newarr is the size of the array.
You have two things going on there... telling the length of an array is pretty simple once you have an array; you just call .Length (in the case of a vector).
However, you mention an instance, and you are showing an instance property; which makes me think it is the containing object you lack. In which case... no. You can't make a virtcall on a null instance. And trying to use static-call on an instance member of a class is very evil; IIRC the runtime will kick you for this.
You could, however, make it a static property just by adding the static modifier. Then you just pass in null as the instance to reflection.
I guess you mean you want to know the size of the array the property will return if it were called?
I don't think you can do it sensibly.
If the property had a conditional then it could return different sized arrays, so
you'd have to evaluate the property to know the size. Which could have side effects or be dependent on other values in the object (or statics).
Consider this one:-
static public int n;
public string[] Key
{
get {
if (n > 1)
return new string[] { "Name", "Type" };
else
return new string[] { "Name", "Type", "Cheese" };
}
}
Basically, you'd have to run the code.