Results 1 to 15 of 15

Thread: Runtime error

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    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!

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Runtime error

    Code:
    characterarray=stg2c.ToCharArray();
    Please mark you thread resolved using the Thread Tools as shown

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    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!

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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());
                }
            
            }
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    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!

  6. #6
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: Runtime error

    Quote 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]);
            }

  7. #7
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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);
            }
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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);
            }
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    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!

  10. #10
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Runtime error

    You can now mark your thread as Resolved using the Thread Tools menu.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  11. #11
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: Runtime error

    Quote 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?

  12. #12
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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);
            }
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  13. #13
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: Runtime error

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

  14. #14
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Runtime error

    I tried that but it dumps the numbers (ascii) instead of their equivalents.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  15. #15
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: Runtime error

    Quote 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
  •  



Click Here to Expand Forum to Full Width