Question CodeDom Related in Manipulating a string(s)

ProtekNickz

Member
Joined
Jan 13, 2022
Messages
14
Programming Experience
5-10
Hi, I've been programming for quite a few years, but only fluctuating in and out of it, with various languages, Here' goes anyway ,

I have created a form with various labels and textboxes and a button, once information is entered in to a textbox then I need to change the text in a multiline code, Example bellow:

String Manipulate:
@"
using System;

namespace MyProgram
{
    class MyStringsProgram
    {
        static void Main(string[] args)
        {
            PrintInfomation(false);
        }
       
        static void PrintInformation(bool isChecked)
        {
            if(isChecked)
            {
                Console.WriteLine(""The Information in"" + Textbox1.Text + "" is Correct!"");
            }
            else
            {
                Console.WriteLine(""The Information in"" + Textbox1.Text + ""  is Incorrect!"");
            }
        }
    }
}
"

Every time I try to add Textbox1.Text to the Multiline code which will be compiled at Runtime, Well it won't compile due to the Textbox1.Text being added, so I cannot compile the information on the fly that is needed, The program is so I can create certain settings in a exe to run on other machines in my network for a project I'm working on, please take note of the @" CODE " as they have to be their so I can place the code in a multiline so I don't have to do each line in a separate string builder code, The reason i would like it in this way is it's easier to edit the code as it's more native to what I see in the main programs form, more human readable.

Yours ProtekNickz.
 
In sitting here with a nice cup of Coffee steaming away and analyzing my code this morning, I have Figured it out, so for those looking for maybe an answer here it is bellow:

Here's the Answer to my own problem!:
@"
using System;

namespace MyProgram
{
    class MyStringsProgram
    {
        static void Main(string[] args)
        {
            PrintInfomation(false);
        }
      
        static void PrintInformation(bool isChecked)
        {
            if(isChecked)
            {
                Console.WriteLine(""The Information in" + '"' + Textbox1.Text + '"' + @" is Correct!"");
            }
            else
            {
                Console.WriteLine(""The Information in" + '"' + Textbox1.Text + '"' + @"  is Incorrect!"");
            }
        }
    }
}
"

Because I have declared the Block Code to be a Constant using the @" CODE ", I had to break out of it using @", I hope you get the idea anyway, also if you wish to replace " back in for strings use this method ' " ' + TexboxHere + ' " ' no spaces in the symbols, this is if your grabbing text / string from an object on the form or a variable.
 
Yes this would work great for single lines of code as you have displayed, But the problem is as i tried to state is the code above is all treated as a Block String all of is as it's basically a template in side my main form this is why i mentioned the @" Code In between here " which makes it a Const, and new lines can not be allowed to be made, their for i had to break the Const out and back in using what I did, You Idea of string Interpolation is a great idea but not bot a block string sadly :/

Cheers ProtekNickz :).
 
You can still use string interpolation with your block text.

C#:
$@"
using System;

namespace MyProgram
{
    class MyStringsProgram
    {
        static void Main(string[] args)
        {
            PrintInfomation(false);
        }
      
        static void PrintInformation(bool isChecked)
        {
            if(isChecked)
            {
                Console.WriteLine(""The Information in {Textbox1.Text} is Correct!"");
            }
            else
            {
                Console.WriteLine(""The Information in {Textbox1.Text} is Incorrect!"");
            }
        }
    }
}
"
 
Last edited:
Back
Top Bottom