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 = "";
    }
}

}

Pokerfan25
  • 11
  • 1

5 Answers5

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.

Community
  • 1
  • 1
Jaime Torres
  • 10,365
  • 1
  • 48
  • 56
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");
        }
}
Manish Sharma
  • 2,406
  • 2
  • 16
  • 31
0

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

Boltosaurus
  • 2,138
  • 3
  • 21
  • 33
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;
Prasad Kanaparthi
  • 6,423
  • 4
  • 35
  • 62
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;
}
Hossain Muctadir
  • 3,546
  • 1
  • 19
  • 33