0

I am trying to create an application in C# that converts numbers in a text box to roman numerals in a label control and need to use a case statement. However one of my variable Roman gets the error message: Use of unassigned local variable 'Roman'.

Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Roman_Numeral_Converter
 {
  public partial class Form1 : Form
  {   
     public Form1()
    {
        InitializeComponent();
    }

    private void btnCalc_Click(object sender, EventArgs e)
    {

        int Number=int.Parse(txtNum.Text); // To hold Number
        string Roman; // To hold Roman Numeral 

        if (Number>=1 && Number <=10)
        {
            switch (Roman)
            {
                case "Number==1":
                lblRoman.Text = "I";
                break;
                case "Number==2":
                lblRoman.Text = "II";
                break;
                case "Number==3":
                lblRoman.Text = "III";
                break;
                case "Number==4":
                lblRoman.Text = "IV";
                break;
                case "Number==5":
                lblRoman.Text = "V";
                break;
                case "Number==6":
                lblRoman.Text = "VI";
                break;
                case "Number==7":
                lblRoman.Text = "VII";
                break;
                case "Number==8":
                lblRoman.Text = "VIII";
                break;
                case "Number==9":
                lblRoman.Text = "IX";
                break;
                case "Number==10":
                lblRoman.Text = "X";
                break;
            }
        }
        else
        {
            MessageBox.Show("Error: Invalid Input");
        }

    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        // Close the form.
        this.Close();
    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        txtNum.Text = "";
        lblRoman.Text = "";
    }
}

}

4
  • case "Number==1": should be like case 1: and the same for the rest of the cases and the variable Roman should be initialized before being used. Commented Sep 18, 2013 at 4:44
  • 1
    Read: dotnetperls.com/switch Commented Sep 18, 2013 at 4:45
  • You haven't assigned any value to your variable Roman,also the use of switch is not right Commented Sep 18, 2013 at 4:47
  • 2
    If this isn't homework or you aren't required to use switch, I would consider a different design for this task. Commented Sep 18, 2013 at 4:49

5 Answers 5

3

Your structure is a little off.

private void btnCalc_Click(object sender, EventArgs e)
{

    var Number = int.Parse(txtNum.Text); // To hold Number

    switch (Number)
    {
            case 1:
              lblRoman.Text = "I";
              break;
            case 2:
              lblRoman.Text = "II";
              break;
            case 3:
              lblRoman.Text = "III";
              break;
            case 4:
              lblRoman.Text = "IV";
              break;
            case 5:
              lblRoman.Text = "V";
              break;
            case 6:
              lblRoman.Text = "VI";
              break;
            case 7:
              lblRoman.Text = "VII";
              break;
            case 8:
              lblRoman.Text = "VIII";
              break;
            case 9:
              lblRoman.Text = "IX";
              break;
            case 10:
              lblRoman.Text = "X";
              break;
            default:
              MessageBox.Show("Error: Invalid Input");
              break;
    }

}

You're using the lblRoman to hold your result, thus your Roman variable is unnecessary. Additionally, since you're interrogating every possible valid number in your switch, you can just use the default to replace your if/else structure.

I'm assuming you're doing this as an academic exercise. That being said, I would be remiss not to point to you Mosè Bottacini's solution to this problem.

Sign up to request clarification or add additional context in comments.

1 Comment

There is already a value for roman listed under my click event.
0

This is because Roman variable is really unassigned. You should assign it before you enter the statement

1 Comment

Clarification this is homework so i have to use switch case statement.
0

try this,

when your number value like 1 so roman number is I.

private void btnCalc_Click(object sender, EventArgs e)
{
        int Number = int.Parse(txtNum.Text); // To hold Number
        string Roman; // To hold Roman Numeral

        if (Number >= 1 && Number <= 10)
        {
            switch (Number)
            {
                case 1:
                    lblRoman.Text = "I";
                    break;

                case 2:
                    lblRoman.Text = "II";
                    break;

                case 3:
                    lblRoman.Text = "III";
                    break;

                case 4:
                    lblRoman.Text = "IV";
                    break;

                case 5:
                    lblRoman.Text = "V";
                    break;

                case 6:
                    lblRoman.Text = "VI";
                    break;

                case 7:
                    lblRoman.Text = "VII";
                    break;

                case 8:
                    lblRoman.Text = "VIII";
                    break;

                case 9:
                    lblRoman.Text = "IX";
                    break;

                case 10:
                    lblRoman.Text = "X";
                    break;
            }
        }
        else
        {
            MessageBox.Show("Error: Invalid Input");
        }
}

5 Comments

I think you mean switch (Number) not switch (Roman) as Roman is not initialized or even used.
That doesnt work because you can't implicitly convert int to string and also then when they enter the numbers 1-10 into the text box it wont show up as the corresponding roman numeral
check i have update my answer. when your text value 1-10 so it's return roman value in your lblRoman.Text.
To fix it I had to have switch (Roman=Number) if anybody else has same problem.
what you want i am not able to understand. and it is working.
0

Instead of switch, You can do other way using Linq which is even better.

int Number=int.Parse(txtNum.Text);
var romanList = new List<string> {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"};
if (Number >= 1 && Number <= 10)
lblRoman.Text = romanList.Select((r, i) => new { Roman = r, Index = i+1}).FirstOrDefault(x=> x.Index == Number).Roman;

Comments

0

you can replace your switch statement this way. and of course you need to assign a variable before using it.

public string GetNum(string val)
{
    string res = ""; // Assign it an empty string. 
    var numToRom = new Dictionary<string, string>
                       {
                           {"1","I"},
                           {"2","II"}
                           //so on
                       };

        numToRom.TryGetValue(val, out res);
        return res;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.