Problem in converting a string to a char array

raiden

Newcomer
Joined
Dec 1, 2007
Messages
8
Location
Sri Lanka
I am designing a calculator in C#, I need to write a code for a backspace operation. for that i need to convert the text(string) value in the calculator to a single one dimensional char array, I tried using the following

string num1 = "Hello World";
char[] numarr = num1.ToCharArray();

this works only on console and does not work in forms.

then I tried using arraylists,

string txt1 = "Hello World";
ArrayList x1 = new ArrayList();//initializes the new arraylist
x1.Add(txt1);//assigns the variable to the arraylist
string txt2 = System.String.Concat(x1.ToArray());

this code crashes the app during runtime,

Can anybody help me? PLEASE

thanks in advance......
 
What problems are you having with the first bit of code? If it works correctly under a console app then it certainly should work for a windows app.

Not sure what the second snippet should be doing - from the look of it you should just end up with txt2 containing the same as txt1.
 
Yes you are absolutely right, i was surprised too but wen I use it in Console app it works fine. but when I work in windows app I get the following error
---------------------
Error 1 Cannot convert method group 'ToCharArray' to non-delegate type 'char[]'. Did you intend to invoke the method? D:\.NET Development\C#.NET\My C# Codes\Calculator\WindowsApplication1\Form1.cs 265 27 WindowsApplication1
---------------------

why is this happening? please help, thank you
 
Last edited:
Well I think My coding is wrong Ill try to post the answer wen I correct my code, thanks anyway
 
Last edited:
Error 1 Cannot convert method group 'ToCharArray' to non-delegate type 'char[]'. Did you intend to invoke the method?
The compiler error asks an important question. Are you sure you didn't forget the parentheses in the WinForms app? Without parentheses, a function name evaluates to a method group.

In case you aren't aware, or for the benefit of other programmers, a method group is a C# "type" comprised of all overloads of a method. Parentheses are required to invoke a function, even if the method has no arguments, and even if there is only one overload, in order to resolve an overload. It is a lexical matter.

In other words, this error typically means you forgot your parentheses. If you forget your parentheses, you identify a method group instead of invoking a method. The compiler politely asks if you wouldn't rather invoke a method.
 
you are absolutely correct, my bad. I forgot about the parenthesis, anyway i solved the problem by using the following code
Code:
string txt1 = "Hello";
                string txt5 = "";
                char[] txt2 = txt1.ToCharArray();
                int abc = txt2.Length - 1;
                char[] txt3 = new char[abc];
                for (int p = 0; p < abc; p++)
                {
                    txt3[p] = txt2[p];
                    String txt4 = Convert.ToString(txt3[p]);
                    txt5 = txt5 + txt4;
                }
                Console.WriteLine (txt5);
 
Back
Top