Jericho
Jericho

Reputation: 81

C# Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox' and not all code paths return a value

I have to calculate 3 variables which the method is being called from another class. But before I print the result, I have to convert the int result into predetermined letter of value. But my code isn't work. I have this error on the code:

CS0029 C# Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox' CS0161 C# '': not all code paths return a value

I've tried to convert function into string, but the code still won't work.

This is the class:

 class Nilai
    {
        public int Calculate(int tugas, int uts, int uas)
        {
            int final = (tugas + uts + uas) / 3;
            return final;
        }
    }

This is the Form1:

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

        private void button1_Click(object sender, EventArgs e)
        {

            nilaiHuruf1 = nilaiHuruf();
        }

       public string nilaiHuruf()
        {
            int a = Convert.ToInt32(tugas1.Text);
            int b = Convert.ToInt32(uts1.Text);
            int c = Convert.ToInt32(uas1.Text);
            Nilai vnilai = new Nilai();
            int hasil = vnilai.Calculate(a, b, c);
            if (hasil >= 85)
            {
                nilaiHuruf1.Text = "A";
                return nilaiHuruf1.ToString();
            }
            else if (hasil >= 80)
            {
                nilaiHuruf1.Text = "A-";
                return nilaiHuruf1.ToString();
            }
            else if (hasil >= 75)
            {
                nilaiHuruf1.Text = "B+";
                return nilaiHuruf1.ToString();
            }
            else if (hasil >= 70)
            {
                nilaiHuruf1.Text = "B";
                return nilaiHuruf1.ToString();
            }

            else if (hasil >= 65)
            {
                nilaiHuruf1.Text = "B-";
                return nilaiHuruf1.ToString();
            }

            else if (hasil >= 60)
            {
                nilaiHuruf1.Text = "C+";
                return nilaiHuruf1.ToString();
            }

            else if (hasil >= 55)
            {
                nilaiHuruf1.Text = "C";
                return nilaiHuruf1.ToString();
            }
            else if (hasil >= 45)
            {
                nilaiHuruf1.Text = "D";
                return nilaiHuruf1.ToString();
            }
            else if (hasil <= 44.99)
            {
                nilaiHuruf1.Text = "E";
                return nilaiHuruf1.ToString();
            }
        }
            }

        }

I expect if I call the method from the Class Nilai to the function nilaiHuruf, then I will get the conversion into letter value. After that, I can call the nilaiHuruf function into the button.

Upvotes: 3

Views: 4804

Answers (5)

Poula Ashraf
Poula Ashraf

Reputation: 21

try to replace :

return nilaiHuruf1.ToString();

into your if statement to :

return nilaiHuruf1.Text;

Upvotes: 2

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

First, I suggest to extract method (business logic and UI separation). In your case, the business logics is in Nilai class:

 //TODO: think over making entire class static: static class Nilai
 class Nilai {
   // static: you don't want "this" here
   public static int Calculate(int tugas, int uts, int uas) {
     return (tugas + uts + uas) / 3;
   }

   public static string Mark(int mark) {
     if (mark >= 85)
       return "A";
     else if (mark >= 80)
       return "A-";
     else if (mark >= 75)
       return "B+";
     else if (mark >= 70)
       return "B";
     else if (mark >= 65)
       return "B-";    
     else if (mark >= 60)
       return "C+";
     else if (mark >= 55)
       return "C"; 
     else if (mark >= 45)
       return "D"; 
     else
       return "E"; 
   }
 }

then use it (UI in Form1)

 public string nilaiHuruf() {
   int a = Convert.ToInt32(tugas1.Text);
   int b = Convert.ToInt32(uts1.Text);
   int c = Convert.ToInt32(uas1.Text); 

   // Since Calculate is static, we don't have to create Nilai instance
   string mark = Nilai.Mark(Nilai.Calculate(a, b, c));  

   nilaiHuruf1.Text = mark; // It's Text property which should be assigned

   // We should return mark, say "B+"; not nilaiHuruf1.ToString();
   return mark;
 } 

Upvotes: 4

cdev
cdev

Reputation: 5351

Exception is your code is not all code paths return value. If you look at your last if, it doesn't have else. You must return something from all code blocks.

And you just need to return nilaiHuruf1.Text

Alternative solution for you. Works with C# 7 and above.

private void button1_Click(object sender, EventArgs e)
{
    nilaiHuruf1.Text = nilaiHuruf();
}

public string nilaiHuruf()
{
    int.TryParse(tugas1.Text, out int a);
    int.TryParse(uts1.Text, out int b);
    int.TryParse(uas1.Text, out int c);

    Nilai vnilai = new Nilai();

    int hasil = vnilai.Calculate(a, b, c);

    string mark = "";

    switch (hasil)
    {
        case int n when (n >= 85):
            mark = "A";
            break;

        case int n when (n >= 80):
            mark = "A-";
            break;

        case int n when (n >= 75):
            mark = "B+";
            break;

        case int n when (n >= 70):
            mark = "B";
            break;

        //// fill other cases

        default:
            mark = "E";
            break;
    }

    return mark;
}

Upvotes: 1

Kami
Kami

Reputation: 19407

The error Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox' is being raised because you are assigning the returned string from a function to a TextBox directly. You need to change this to use the Text property - nilaiHuruf1.Text = nilaiHuruf();

The error not all code paths return a value is being raised because you are not handling all the cases for the int value. In particular, you last else if. Change it to just else as you have already covered all other grades.

Ideally, the code should follow SOP and can be simplified to as :

private void button1_Click(object sender, EventArgs e)
{
    nilaiHuruf1.Text = nilaiHuruf(Convert.ToInt32(tugas1.Text), Convert.ToInt32(uts1.Text), Convert.ToInt32(uas1.Text));
}

public string nilaiHuruf(int a, int b, int c)
{
    string rtn = "";
    Nilai vnilai = new Nilai();
    int hasil = vnilai.Calculate(a, b, c);
    if (hasil >= 85)
    {
        rtn = "A";
    }
    else if (hasil >= 80)
    {
        rtn = "A-";
    }
    else if (hasil >= 75)
    {
        rtn = "B+";
    }
    else if (hasil >= 70)
    {
        rtn = "B";
    }
    else if (hasil >= 65)
    {
        rtn = "B-";
    }
    else if (hasil >= 60)
    {
        rtn = "C+";
    }
    else if (hasil >= 55)
    {
        rtn = "C";
    }
    else if (hasil >= 45)
    {
        rtn = "D";
    }
    else
    {
        rtn = "E";
    }

    return rtn;
}

As the function nilaiHuruf no longer has a dependency on any values within the form, it may be an option to move it to the Nilai class depending on the purpose of that class.

Upvotes: 1

CodeTherapist
CodeTherapist

Reputation: 2806

I think that's simple. When nilaiHuruf1 is a textbox, you can't assign directly a string to it - you must set the Text property of the textbox:

private void button1_Click(object sender, EventArgs e)
{
    nilaiHuruf1.Text = nilaiHuruf();
}

Within the nilaiHuruf() method, return nilaiHuruf1.ToString(); will return the full type name of the textbox - not as you expect the text of the textbox. Use return nilaiHuruf1.Text; instead. Further ToString() is a method that every object has.

Update

When the value of nilaiHuruf1.Text is changed within the method nilaiHuruf() why would you return that value and set it again to the nilaiHuruf1 textbox? Remove the assignment as follows and everything should work:

private void button1_Click(object sender, EventArgs e)
{
    nilaiHuruf();
}

Upvotes: 2

Related Questions