How to put variable into string's name, when my variable is a list made after test.ToCharArray();? RRS feed

  • Question

  • Hey,

    I am learning to split Strings etc. and I was messing around with .ToCharArray(); and then I came up with an idea to try to assign each letter to a string, then replace letter with other letter written in some kind of key, then return string of these letters (like a cipher).

    Also please be patient, I am very new to any kind of programming so please explain like I'm 5.

    Here's the code I've made (in void main(string[] args) of course):

    string test = "test";
                var tests = test.ToCharArray();
                int lolo = 0;
                //for (int lolo = 0; lolo < tests.Length; lolo++)
                //Console.WriteLine("{0}", tests[lolo]);
                string letname;
                    letname = tests[lolo].ToString + System.Convert.ToString;
                    string text = tests[lolo].ToString; //I want to name each string in a diffrent way without knowing how many strings there are, each string named 'text' + value of 'lolo'.
                } while (lolo < tests.Length);

    Also I get 2 errors: CS0019 and CS0428, which stands for "C# Operator '+' cannot be applied to operands of type 'method group' and 'method group'" and "C# Cannot convert method group 'ToString' to non-delegate type 'string'. Did you intend to invoke the method?".

    Thank you for any kind of help.

    Wednesday, February 14, 2018 5:30 PM

All replies

  • First what is called in for instance Dutch a "letter" is in English "character".  A letter is something else.  

    And for the rest test your own code with for instance debugging and see what it tells. 

    String is another name in C# for character array. 

    This part is AFAIK complete nonsense so what are you intending with it. 

     letname = tests[lolo].ToString + System.Convert.ToString;


    Wednesday, February 14, 2018 6:08 PM
  • Hi,

      ToString is a method call and needs parentheses. So "ToString()" in all locations.


    Ethan Strauss

    Wednesday, February 14, 2018 6:14 PM
  • To create a cipher, you don't need to convert to a char array.  You can address the individual characters from within the string.  Consider this silly example:

    using System;
    class program {
        public static void Main( )
            string test = "test";
            string coded = "";
            foreach( char c in test )
                char newc='?';
                if( c == 't' )
                    newc = 'x';
                else if( c == 'e' )
                    newc = 'y';
                else if( c == 's' )
                    newc = 'z';
                coded = coded + newc;
            Console.WriteLine( test + " converted to " + coded );

    In a real program, you would use some kind of a lookup table (an array of char or perhaps a dictionary) instead of the if/elses, but this shows you the idea.

    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Wednesday, February 14, 2018 6:27 PM