How to make string variable writable?

Created at 17 Jan 2024, 12:17
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!
AndreaPereira's avatar

AndreaPereira

Joined 02.12.2023

How to make string variable writable?
17 Jan 2024, 12:17


I would like to change the character of the string str, but you tell me that it is read-only.  How can I overcome this problem?

private void TextBox_TextChanged(TextChangedEventArgs obj){
            
            char oldChar = '.';
            char newChar = ',';
            string a;
            string str = obj.TextBox.Text;
           
            for (int i = 0; i < str.Length; i++){
                if (str[i] == oldChar) {
                    str[i] = newChar;    <------- PROBLEM
                    
                }
            }            
            Quantity = Convert.ToDouble(str);
        }

@AndreaPereira
Replies

PanagiotisCharalampous
18 Jan 2024, 10:02

Hi there,

Use String.Replace() instead.

Best regards,

Panagiotis


@PanagiotisCharalampous

AndreaPereira
18 Jan 2024, 11:16 ( Updated at: 18 Jan 2024, 14:11 )

How to make string variable writable?

Thanks, I'll go and test...

 

This function should solve the comma problem on the textbox. When I enter floating point text with the period, the function should replace the period with the comma. But it doesn't work, what am I doing wrong? or is there a better solution to do this? Thanks you Panagiotis.

private void TextBox_TextChanged(TextChangedEventArgs obj){
            
            char oldChar = '.';
            char newChar = ',';
            string str = obj.TextBox.Text;

            for (int i = 0; i < str.Length; i++){
                if (str[i] == oldChar){
                    str.Replace(oldChar,newChar);         
                }
            }        
            Quantity = Convert.ToDouble(str);
        }

 

PanagiotisCharalampous said: 

Hi there,

Use String.Replace() instead.

Best regards,

Panagiotis

 


@AndreaPereira

PanagiotisCharalampous
19 Jan 2024, 06:50

RE: How to make string variable writable?

AndreaPereira said: 

Thanks, I'll go and test...

 

This function should solve the comma problem on the textbox. When I enter floating point text with the period, the function should replace the period with the comma. But it doesn't work, what am I doing wrong? or is there a better solution to do this? Thanks you Panagiotis.

private void TextBox_TextChanged(TextChangedEventArgs obj){                        char oldChar = '.';            char newChar = ',';            string str = obj.TextBox.Text;            for (int i = 0; i < str.Length; i++){                if (str[i] == oldChar){                    str.Replace(oldChar,newChar);                         }            }                    Quantity = Convert.ToDouble(str);        }

 

PanagiotisCharalampous said: 

Hi there,

Use String.Replace() instead.

Best regards,

Panagiotis

 

Hi there, 

It seems you haven't read the documentation carefully :) You don't need a for loop and the method does not work on the current string, it returns a new one with the character replaced.

Best regards,

Panagiotis


@PanagiotisCharalampous

AndreaPereira
19 Jan 2024, 11:46

How to make string variable writable?

eheh I mostly read the API reference. Give me time, I'm becoming much more skilled these days thanks to you too. So nothing can be done as I understand it??

 

Hi there, 

It seems you haven't read the documentation carefully :) You don't need a for loop and the method does not work on the current string, it returns a new one with the character replaced.

Best regards,

Panagiotis

 


@AndreaPereira

AlgoCreators
19 Jan 2024, 14:17

How to make string variable writable?

hi, Check this code, I hope it solves your problem

using cAlgo.API;
using System.Text.RegularExpressions;
namespace cAlgo
{
    // This sample indicator shows how to use TextChangedEventArgs
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TextChangedEventArgsSample : Indicator
    {
        protected override void Initialize()
        {
            var stackPanel = new StackPanel
            {
                BackgroundColor = Color.Gold,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                Opacity = 0.6,
            };
            var textBox = new TextBox
            {

                Text = "Enter text here...",
                FontWeight = FontWeight.ExtraBold,
                Margin = 5,
                ForegroundColor = Color.White,
                HorizontalAlignment = HorizontalAlignment.Center,
                Width = 150
            };
            textBox.TextChanged += TextBox_TextChanged;
            stackPanel.AddChild(textBox);
            Chart.AddControl(stackPanel);
        }
        private void TextBox_TextChanged(TextChangedEventArgs obj)
        {
            string str = obj.TextBox.Text;
            if (str.Length > 0)
                if (str[str.Length - 1] == '.')
                {
                    var x = Regex.Replace(str,@"[.]+", ",");              
                    obj.TextBox.Text = "";
                    obj.TextBox.Text = x;
                }
        }
        public override void Calculate(int index)
        {
        }
    }
}

 


@AlgoCreators

AndreaPereira
19 Jan 2024, 14:34

How to make string variable writable?

Hi Chegini,

Thanks for the help, now I'll see if it solves the problem.. :)

 

meeting.chegini said: 

hi, Check this code, I hope it solves your problem

 


@AndreaPereira

AndreaPereira
19 Jan 2024, 15:50

How to make string variable writable?

I tried…it doesn't compile...it says regex. it does not exist as a method.

meeting.chegini said: 

hi, Check this code, I hope it solves your problem

 

 


@AndreaPereira