|
-
Jan 23rd, 2009, 07:05 AM
#1
Thread Starter
Frenzied Member
Runtime error
Hi,
Im trying to compile an exercise, that reads a string backward which I would on fixing this error extend it to ceasar's cipher.
my code is here:
Code:
private void button1_Click(object sender, EventArgs e)
{
string stg2c="";
string cstg="" ;
char[] characterarray;
stg2c = textBox1.Text;
characterarray=stg2c.ToCharArray;
for (int i = characterarray.Length - 1; i >= 0; i--)
textBox2.Text += (characterarray[i]);
}
The error message is this:Cannot assign to 'ToCharArray' because it is a 'method group'
Any help pls?
------------------------------------------------------------------------
If an answer to your question has been helpful, then please, Rate it! 
-
Jan 23rd, 2009, 07:25 AM
#2
Re: Runtime error
Code:
characterarray=stg2c.ToCharArray();
Please mark you thread resolved using the Thread Tools as shown
-
Jan 23rd, 2009, 01:46 PM
#3
Thread Starter
Frenzied Member
Re: Runtime error
I have tried to get the ascii number of the characterarray(i)
Code:
num=(KeyValue(characterarray[i]));
but it is not correct
can someone tell me the correct keyword for getting the ascii of characterarray(i) ?
Last edited by angelica; Jan 23rd, 2009 at 01:59 PM.
------------------------------------------------------------------------
If an answer to your question has been helpful, then please, Rate it! 
-
Jan 23rd, 2009, 02:57 PM
#4
Re: Runtime error
Do you just want to get the ascii values of the string? If yes then follow this code:
Code:
private void button1_Click_1(object sender, EventArgs e)
{
string stg2c = "dee-u";
byte[] asciis = Encoding.ASCII.GetBytes(stg2c);
for (int i = asciis.Length -1; i >= 0; i--)
{
MessageBox.Show(asciis[i].ToString());
}
}
-
Jan 23rd, 2009, 03:34 PM
#5
Thread Starter
Frenzied Member
Re: Runtime error
many thanks to both of you guys.
What I need is get each char's ascii so that after add +3 and then put it back in the Char.
for a simple ceasar cipher but Im really stuck on how to go about it. Building on your suggestion I did
Edit:
Code:
ToChar(asciis[i]) =(asciis[i] + 3);
textBox2 += Tochar(asciis[i]);
Last edited by angelica; Jan 23rd, 2009 at 03:57 PM.
------------------------------------------------------------------------
If an answer to your question has been helpful, then please, Rate it! 
-
Jan 23rd, 2009, 03:50 PM
#6
Frenzied Member
Re: Runtime error
 Originally Posted by angelica
I have tried to get the ascii number of the characterarray(i)
Code:
num=(KeyValue(characterarray[i]));
but it is not correct
can someone tell me the correct keyword for getting the ascii of characterarray(i) ?
A char is a short so there is no need to pass it to a method to "convert" it.
int num = characterarray[i];
Also you don't even need to convert it to a char array. string has the [] operator which returns a char.
Code:
private void button1_Click(object sender, EventArgs e)
{
string stg2c="";
string cstg="" ;
stg2c = textBox1.Text;
for (int i = stg2c .Length - 1; i >= 0; i--)
textBox2.Text += (stg2c[i]);
}
-
Jan 23rd, 2009, 03:56 PM
#7
Re: Runtime error
I believe this could be simplified but for not it should work and you just have to modify it accordingly.
Code:
private void button1_Click(object sender, EventArgs e)
{
string stg2c = "dee-u";
byte[] asciis = Encoding.ASCII.GetBytes(stg2c);
for (int i = asciis.Length -1; i >= 0; i--)
{
asciis[i] = (byte)(asciis[i] + 3);
}
char[] chars = Encoding.ASCII.GetChars(asciis);
string result = string.Empty;
for (int i = 0;i <= chars.Length - 1; i++)
{
result += chars[i].ToString();
}
MessageBox.Show(result);
}
-
Jan 23rd, 2009, 04:05 PM
#8
Re: Runtime error
This is smaller.
Code:
private void button1_Click(object sender, EventArgs e)
{
string stg2c = "dee-u";
string result = string.Empty;
for (int i = stg2c.Length - 1; i >= 0; i--)
{
result += Convert.ToChar(Convert.ToByte((stg2c[i]) + 3));
}
MessageBox.Show(result);
}
-
Jan 23rd, 2009, 04:10 PM
#9
Thread Starter
Frenzied Member
Re: Runtime error
Many thanks dee-u, that did it. I am a beginner in C#, so I have a lot to learn
------------------------------------------------------------------------
If an answer to your question has been helpful, then please, Rate it! 
-
Jan 23rd, 2009, 04:13 PM
#10
Re: Runtime error
You can now mark your thread as Resolved using the Thread Tools menu.
-
Jan 23rd, 2009, 04:16 PM
#11
Frenzied Member
Re: Runtime error
 Originally Posted by dee-u
This is smaller.
Code:
private void button1_Click(object sender, EventArgs e)
{
string stg2c = "dee-u";
string result = string.Empty;
for (int i = stg2c.Length - 1; i >= 0; i--)
{
result += Convert.ToChar(Convert.ToByte((stg2c[i]) + 3));
}
MessageBox.Show(result);
}
Why would you convert to a byte and then back?
-
Jan 23rd, 2009, 04:23 PM
#12
Re: Runtime error
Thanks High6! Got further suggestions? =)
Code:
private void button1_Click(object sender, EventArgs e)
{
string stg2c = "dee-u";
string result = string.Empty;
for (int i = stg2c.Length - 1; i >= 0; i--)
{
result += Convert.ToChar((stg2c[i]) + 3);
}
MessageBox.Show(result);
}
-
Jan 23rd, 2009, 04:30 PM
#13
Frenzied Member
Re: Runtime error
 Originally Posted by dee-u
Thanks High6! Got further suggestions? =)
Code:
private void button1_Click(object sender, EventArgs e)
{
string stg2c = "dee-u";
string result = string.Empty;
for (int i = stg2c.Length - 1; i >= 0; i--)
{
result += Convert.ToChar((stg2c[i]) + 3);
}
MessageBox.Show(result);
}
You don't need to convert to a char.
-
Jan 23rd, 2009, 04:43 PM
#14
Re: Runtime error
I tried that but it dumps the numbers (ascii) instead of their equivalents.
-
Jan 23rd, 2009, 04:45 PM
#15
Frenzied Member
Re: Runtime error
 Originally Posted by dee-u
I tried that but it dumps the numbers (ascii) instead of their equivalents.
ah, when you add 3 it stores it as an integer and adds it.
So you can change the Convert.ToChar to (char).
Also it isn't needed, but I would recommend a stringbuilder because it is faster at appending.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|