I have a method that fills up the elements of an int[,]. The elements that need to be filled are stored in a .txt file like this:
1
1
2
2
Meaning that I have to fill up the [1,1] and [2,2] element.
For this I use this but it gives the error above
int x = 0;
int y = 0;
for (int i = 0; i < 2; i++)
{
x = int.Parse(SR.ReadLine());
y = int.Parse(SR.ReadLine());
mezo.mezo[x, y] = 1;
}
Thanks in advance!
According to MSDN(http://msdn.microsoft.com/en-IN/library/b3h1hf19.aspx) a FormatException is thrown when:
s is not in the correct format.
s in your case is SR.ReadLine() which is returning some value that is not recognized as a number format.
At first look it might be because of whitespaces in your file.
Try
SR.ReadLine().Trim()
OR
NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite for the number style in the Parse method.
Related
I have never heard of ThinkOrSwrim till yesterday when someone asked me to convert a ThinkOrSwim script to an MQL4 indicator.
A part of the code is as follows:
input length = 21;
input price = close;
input ATRs=1;
input trueRangeAverageType = AverageType.WILDERS;
def flag;
def EMA = ExpAverage(close, length);
def shift1 = ATRs * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);
I want to ask you to kindly check and let me know if my understanding is correct.
input ATRs=1; // This should be a multiplier for ATR, then I think I should give it a double
//type for more flexible control.
input trueRangeAverageType = AverageType.WILDERS;
//As far as I understood, wilders is the same as SMMA in MQL.
.
def shift1 = ATRs * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);
Here is the main piece of this code which I need your help with.
My understanding is as follows
ATRs ==>> Just a multiplier
I think the rest of this line is calculating the ATR, right?
If so, then I can see that I cannot simply convert this to iATR (in mql), because we are not able to choose MA Methode of ATR in mql4.
Then I think first I have to put the "True Range" of each bar in an array and then use this array as a price source to get the averages.
MQL4:
for(int i = 0; i < rates_total; i++)
{
data[i] = iATR(_Symbol, TF_1, 1, i);
}
for(int i = 0; i < limit; i++)
{
ExtBuffer[i] = iMAOnArray(data, 0, Inplenght, 0, InpMAMethod, i);
}
If I'm in the right way yet, Then I think the iATR period has to be 1, to have the TrueRange of each bar and not the average of the TrueRanges.
And then have the variable length (from thinkOrSwim inputs) as the period parameter for iMAOnArray.
I would appreciate any help with it.
Regards
Edit:
I forgot to ask you something,
why should the programmer who wrote this thinkscript code call this variable shift1?
I have a List of the type Model. when I loop all its elements and loop the next one except for the last one, then change the last one manually, the one before changes.
here is a little code to reproduce the problem (also you can run it directly in dartpad from here)
void main() {
List<Model> s = [];
for (int i = 0; i < 5; i++) {
s.add(Model(i));
}
for (int i = 0; i < s.length - 1; i++) {
s[i] = s[i + 1];
}
print(s);
s[s.length-1].x = 100;
print(s);
}
class Model {
int x;
Model(this.x);
#override
String toString() => 'x: ' + this.x.toString();
}
notice that this problem does not happen when you comment out the for loop or the manual change, or instead of changing the last one's property, you reassign a new value to it, like s[s.length - 1] = Model(100);. seems like dart for some reason is re-running the loop.
When you run the second for loop, you assign the i + 1th Model to the ith position in the list:
// initialise list
for (int i = 0; i < s.length; i++) {
s[i] = s[i + 1]
}
If you unwrap the loop, it looks roughly like this:
s[0] = s[1];
s[1] = s[2];
s[2] = s[3];
s[3] = s[4];
Notice that this leaves s[4] unchanged, but also assigns it to s[3].
In Dart, variables contain references to objects. This means that when your list runs s[3] = s[4];, both s[3] and s[4] point to the same object.
Now, if you modify s[4] you, are actually modifying the objects that s[4] refers to, which happens to also be the object that s[3] refers to, so they both appear to change in your list.
I'm a total newbie in Dart and I've a lot of issues trying to change a member value of an Object inside a loop.
I've my object so defined:
class Cell {
int magicnum, x, y;
Cell(this.magicnum);
toString() {
return ("$magicnum - [$x][$y]");
}
}
I create a List of List of Cell (2d array) and then I need to fill x and y value according to position of each object in array.
for (int x = 0; x < DIM; x++) {
for (int y = 0; y < DIM; y++) {
grids[x][y].x = x;
grids[x][y].y = y;
}
}
This obviosly doesn't work because in Dart everything (also integer) is an Object and so, all Cell objects in my array have the same x and y value (they all got a reference to the same object).
How can I do?
Thanks
#julemand101
Array is made this way:
List<Cell> cells = List<Cell>.generate(DIM, (i) => Cell(i + 1));
List<List<Cell>> grids = List<List<Cell>>();
for (int i = 0; i < DIM; i++) {
grids.add(shuffleCell(cells));
}
You problem is you are not actually cloning each Cell object when you are doing the following (taken from the code example from comments):
List<Cell> newlist = List<Cell>.from(items);
Instead, you are creating a new List containing the same references to Cell objects as the previous list of items.
To create a copy of Cell objects you need to implement a clone method like:
class Cell {
int magicnum, x, y;
Cell(this.magicnum);
Cell.from(Cell cell)
: magicnum = cell.magicnum,
x = cell.x,
y = cell.y;
}
And do the following to iterate each element of the old list, create a new Cell object for each element and convert the result to a new List:
List<Cell> newlist = items.map((item) => Cell.from(item)).toList();
I would like to display the elements in a vector in a listbox. However, I'm constantly getting a error: error C2664: 'System::Windows::Forms::ListBox::ObjectCollection::Add' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Alloc>' to 'System::Object ^'
I am using windows form in c++/cli.
this is the code:
for (size_t z = 0; z < container.size(); z++){
listBox_name->Items->Add(container[z]);
}
Based on the error message, your vector is a vector of std::string. Use marshal_as to convert the std::string to a managed String^ that the managed list box can accept.
for (size_t z = 0; z < container.size(); z++){
listBox_name->Items->Add(marshal_as<String^>(container[z]));
}
If you find you're doing this a lot, consider changing your vector of std::string to be a fully-managed type, such as List<String^>^.
Below code is throwing an IndexOutOfBoundsException at line Field f = getField(counter);
Why is it being thrown ? Surely the field exists because I am looping based on fieldcount. Or is the list fields in the manager not gauranteed to be sequential? If this is the case how should I delete fields from a screen that are of type - MyButtonField
Thanks
int fieldCount = getFieldCount() - 1;
if(fieldCount > 1){
for(int counter = 0; counter <= fieldCount ; ++counter){
Field f = getField(counter);
if(f instanceof MyButtonField){
delete(f);
}
}
}
You haven't specified what delete(f) does, but if it removes it from the list of fields, then your "valid count" will effectively decrease.
To rewrite this somewhat and fix the problem:
for (int index = getFieldCount() - 1; index >= 0; index--){
Field f = getField(index);
if (f instanceof MyButtonField) {
delete(f);
}
}
This will go from the end of the fields instead of the start, so it doesn't matter if you remove an entry and everything shuffles up - the items which shuffle up will be the ones you've already looked at.
The best way is to use Iterator for iteration then call the method remove().
Example:
for(Iterator it = getFields().iterator();it.hasNext()){
Field f = (Field) it.next();
if(f instanceof MyButtonField){
it.remove();
}
}
The method getFields() has to return a collection of Field elements.