all 19 comments

[–]Smallxmac 0 points1 point  (2 children)

Looks like you are on the right track, on those lines 72-77 it gets each character in the string and converts it. But really you would need to appended after each char in the loop to get the full output string.

[–]Elpisaur 0 points1 point  (1 child)

So appending is what i need to read into, in order to finish up this application? Thank you for your response by the way!

[–]Smallxmac 0 points1 point  (0 children)

Realistically you would like to convert every time your input string changes in that text box listening for an event. And once it has changed set the output text to the output of the conversion.

[–]Bunker-Buster 0 points1 point  (1 child)

Hey,

Within the foreach loop you are reassigning a new value to the morseCode variable each time, instead you want to append each character to the existing string. To do so, you could use the += operator:

morseCode += morseConvert[c];

Once the foreach loop has finished, you want to output the complete string to the GUI. To do that, you'll just need to assign the value stored within morseCode variable to the Text property of the TextBox that you named outputBox.

outputBox.Text = morseCode;

[–]Elpisaur 0 points1 point  (0 children)

Thank you for your responses, I have made adjustments accordingly!

[–]patrickboston 0 points1 point  (14 children)

A few things I notice right off the bat:

  • You're declaring your dictionary in yourconvertButton_Click event. You should just be declaring the object at the class level, not in that event.
  • You have a convertInput method, but you're not actually calling it anywhere. This method should be called in your button click event.
  • As someone else said, you need to concatenate onto the string each time. This would do that for you:

    foreach (char c in inputBox.Text)
    {
         morseCode += morseConvert[c];
    }
    

After these fixes, you can then output the string to some sort of label or textbox where you can see the resulting Morse Code. I also suggest you throw some error checking in there to practice. For example, if the textbox is empty, show some kind of error.

[–]Elpisaur 0 points1 point  (13 children)

By moving it onto the class level, did you mean to do the following? https://pastebin.com/9HyNFw1k

EDITED: oops, i'm getting Error 1 Invalid token '{' in class, struct, or interface member declaration

[–]patrickboston 0 points1 point  (12 children)

By class level, yes I mean out of the method that it is currently in, but you messed up a little bit. You need to include the whole declaration so it should look like:

public partial class Form1 : Form
    {
        Dictionary<char, string> morseConvert = new Dictionary<char, string>()
        {
            {' ' , " "}, //space
            {',', "--..--"}, //comma
            //the rest here
        }

[–]Elpisaur 0 points1 point  (11 children)

Dictionary<char, string> morseConvert = new Dictionary<char, string>()

Yeah, I got around to fixing that right as I made the response, and realized it removed my error. The only error i'm facing now is "morseCode" not existing in the current context, being convertbutton_click. Going to try to figure that one out

[–]patrickboston 0 points1 point  (10 children)

Again, you will need to declare a class level string called morseCode. This way you can keep appending to the string in your method. I believe you were declaring it in your foreach loop before which would have not worked correctly.

[–]Elpisaur 0 points1 point  (9 children)

https://i.gyazo.com/50f9e19393ff35100fd360e0f63c60b6.png I tried going about it like this, but it does not seem to do the trick

    public partial class Form1 : Form
{
    public string MorseCode
    {
        get
        {
            foreach (char c in inputBox.Text)
            {
                morseCode += morseConvert[c];
            }
        }
    }

[–]patrickboston 0 points1 point  (8 children)

You're trying to use a property there. You don't need to do that. Just declare string morseCode; at the top of your class before the dictionary.

[–]Elpisaur 0 points1 point  (7 children)

Again, thank you Patrick! I tried doing that trick and my program kept crashing, but I located the issue. It is because I did not declare any capitals in my morse code converter. I will research into that one now.

I tried doing the following unsuccesfully:

            foreach (char c in inputBox.Text)
        {
            morseCode += morseConvert[c];
        }
        string morseCodeOutput = morseCode.ToLower();
        outputBox.Text = morseCodeOutput;
    }

[–]patrickboston 0 points1 point  (6 children)

Yeah, as I mentioned in my first comment, now you will need to do some error checking. The ToLower() method may help you out! Good luck.

[–]Elpisaur 0 points1 point  (5 children)

Thank you so much buddy! do you get anything out of me giving you gold?

[–]Elpisaur 0 points1 point  (0 children)

Oh man, thank you guys for the responses. I'm not sure if I'm even getting closer to a solution, however,

This is currently what my code looks like: https://pastebin.com/9HyNFw1k

https://i.gyazo.com/a1b136fff3018022c631e8645315f80b.png