Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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......

  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted (edited)

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

Edited by raiden
  • Leaders
Posted
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.

[sIGPIC]e[/sIGPIC]
Posted

you are absolutely correct, my bad. I forgot about the parenthesis, anyway i solved the problem by using the following 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);
[/Code]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...