Error with Arrays - Beware!!

Created at 13 Dec 2024, 00:06
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
PH

PhoenixCapital

Joined 19.04.2017

Error with Arrays - Beware!!
13 Dec 2024, 00:06


Hi Spotware,

I am not sure if I am doing something wrong here, but it would appear that a list A which has been assigned to another list B seems to take all the data of list B in the entire code going forward. I dont know if I am missing something here or I have done things incorrectly, but please let me know.

 

Here is the code.

protected override void OnTick()
       {
           double[] prices = new double[5];
           double[] PriceList = { 1, 2, 3, 4, 5 };

           for (int i = 0; i < PriceList.Length; i++)
           {
               Print("OLD PRICE LIST: {0}", PriceList[i]);
           }
           
           prices = PriceList;
           
           for (int i = 0; i < prices.Length; i++)
           {
               prices[i] *= 0.10;
           }
           
           for (int i = 0; i < PriceList.Length; i++)
           {
               Print("NEW PRICE LIST: {0}", PriceList[i]);
           }


       }
 

NEW PRICE LIST should still be 1, 2,3,4,5. I have not reassigned it. I only assign PriceList to price. However, my result was that PriceList becomes price.

Result is:

12/12/2024 01:01:28.970 | Info | OLD PRICE LIST: 1
12/12/2024 01:01:28.970 | Info | OLD PRICE LIST: 2
12/12/2024 01:01:28.970 | Info | OLD PRICE LIST: 3
12/12/2024 01:01:28.970 | Info | OLD PRICE LIST: 4
12/12/2024 01:01:28.970 | Info | OLD PRICE LIST: 5
12/12/2024 01:01:28.970 | Info | NEW PRICE LIST: 0.1
12/12/2024 01:01:28.970 | Info | NEW PRICE LIST: 0.2
12/12/2024 01:01:28.970 | Info | NEW PRICE LIST: 0.30000000000000004
12/12/2024 01:01:28.970 | Info | NEW PRICE LIST: 0.4
12/12/2024 01:01:28.970 | Info | NEW PRICE LIST: 0.5

Using a for loop seems to avoid this IDE error though. It is just not the ideally way to code.

Please be mindful if you are doing this.

 

Spotware - any solutions?

 

Thanks,


@PhoenixCapital
Replies

firemyst
13 Dec 2024, 00:42 ( Updated at: 13 Dec 2024, 06:34 )

This is not a Spotware issue.

The solution is you need to better understand how arrays and objects are copied in C#.

When you say “prices = PriceList”, you're assigning the underlying reference of PriceList to prices, meaning whatever you change in one, changes in the other.

If you want to copy the actual values, you need to either:

  1. use a loop to copy each value in the array
  2. Google search and learn how to do it other ways, such as : https://stackoverflow.com/questions/14651899/create-a-copy-of-integer-array 

@firemyst

PanagiotisCharalampous
13 Dec 2024, 07:04

Hi there,

That's how C# works. When you assign a list to another list, then you actually reference the previous. If you want to copy a list, use CopyTo method.

Best regards,

Panagiotis


@PanagiotisCharalampous

PhoenixCapital
13 Dec 2024, 12:09 ( Updated at: 13 Dec 2024, 14:37 )

RE: Error with Arrays - Beware!!

PanagiotisCharalampous said: 

Hi there,

That's how C# works. When you assign a list to another list, then you actually reference the previous. If you want to copy a list, use CopyTo method.

Best regards,

Panagiotis

Hi Panagiotis,

 

Thanks for your reply. 

I am not sure you fully understand. Yes, if I assign a list to another list say list B to list A, List A will take all the references from List B. However, if I change anything in List A, it should not change anything in List B because I only assigned List B to List A, not the other way round i.e. List A to List B.

Just to break it down:

List B is assigned to List A

List A is NOT assigned to List B.

Any changes to List A should NOT affect List B. They are independent of each other after the change to List A.

This is how I have been doing it for 2 years and there was no issues. If you look at previous versions, it worked seamless without any issues.

I will try the Copy.To Method as well. 

 

Thanks,

 


@PhoenixCapital

PhoenixCapital
13 Dec 2024, 12:10 ( Updated at: 13 Dec 2024, 14:37 )

RE: Error with Arrays - Beware!!
firemyst said:

This is not a Spotware issue.

The solution is you need to better understand how arrays and objects are copied in C#.

When you say “prices = PriceList”, you're assigning the underlying reference of PriceList to prices, meaning whatever you change in one, changes in the other.

If you want to copy the actual values, you need to either:

  1. use a loop to copy each value in the array
  2. Google search and learn how to do it other ways, such as : https://stackoverflow.com/questions/14651899/create-a-copy-of-integer-array 


@PhoenixCapital

PhoenixCapital
13 Dec 2024, 12:13 ( Updated at: 13 Dec 2024, 14:37 )

RE: Error with Arrays - Beware!!

firemyst said: 

This is not a Spotware issue.

The solution is you need to better understand how arrays and objects are copied in C#.

When you say “prices = PriceList”, you're assigning the underlying reference of PriceList to prices, meaning whatever you change in one, changes in the other.

If you want to copy the actual values, you need to either:

  1. use a loop to copy each value in the array
  2. Google search and learn how to do it other ways, such as : https://stackoverflow.com/questions/14651899/create-a-copy-of-integer-array 

Hi Firemyst,

 

Thanks for your comment. 

 

Perhaps, this is new. Older versions of ctrader didnt do this at all. I have been doing this for 2 years.

 

I am assigning "prices = PriceList", not PriceList = prices". So I wouldnt think change "prices" would change "PriceList" because I am not assigning "prices" to "PriceList" after the changes. I know other programming languages as well and it doenst work that way. It doesnt make logical sense really. 

 

But yes, I will try the copy to method.

 

Thanks,


@PhoenixCapital

PhoenixCapital
13 Dec 2024, 12:19 ( Updated at: 13 Dec 2024, 14:37 )

Thanks guys. ToArray() is the best solution.

 

using System.Linq;

// code ...
int[] b = a.ToArray();

@PhoenixCapital

PanagiotisCharalampous
13 Dec 2024, 14:50

RE: RE: Error with Arrays - Beware!!

PhoenixCapital said: 

firemyst said: 

This is not a Spotware issue.

The solution is you need to better understand how arrays and objects are copied in C#.

When you say “prices = PriceList”, you're assigning the underlying reference of PriceList to prices, meaning whatever you change in one, changes in the other.

If you want to copy the actual values, you need to either:

  1. use a loop to copy each value in the array
  2. Google search and learn how to do it other ways, such as : https://stackoverflow.com/questions/14651899/create-a-copy-of-integer-array 

Hi Firemyst,

 

Thanks for your comment. 

 

Perhaps, this is new. Older versions of ctrader didnt do this at all. I have been doing this for 2 years.

 

I am assigning "prices = PriceList", not PriceList = prices". So I wouldnt think change "prices" would change "PriceList" because I am not assigning "prices" to "PriceList" after the changes. I know other programming languages as well and it doenst work that way. It doesnt make logical sense really. 

 

But yes, I will try the copy to method.

 

Thanks,

Hi there,

The programming language has nothing to do with cTrader and it's been like this since at least 2000 when C# was introduced :)

Best regards,

Panagiotis


@PanagiotisCharalampous

firemyst
14 Dec 2024, 05:59 ( Updated at: 14 Dec 2024, 06:22 )

RE: RE: Error with Arrays - Beware!!

PhoenixCapital said: 

Perhaps, this is new. Older versions of ctrader didnt do this at all. I have been doing this for 2 years.

It has always been like this. This is how C# has been working since it was first introduced back in early 2000's - even before Spotware's time. 

If you want evidence, download a copy of Visual Studio 2015:

https://visualstudio.microsoft.com/vs/older-downloads/

Create a basic C# console application, set the .Net framework to either 2.0 or 3.5 (if possible) and put your code in there. 

:-)

 

 


@firemyst