3

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.

2
  • From nilaiHuruf() function - you want to assign value to textbox or you want to return string as result? Commented Sep 10, 2019 at 9:26
  • Try replacing all return nilaiHuruf1.ToString(); with return nilaiHuruf1.Text; Commented Sep 10, 2019 at 9:27

5 Answers 5

4

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;
 } 
Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

Do you have any idea, how can I make the function doesn't have to declare the variables (a,b,c) inside the function but declare it on the button?
2

try to replace :

return nilaiHuruf1.ToString();

into your if statement to :

return nilaiHuruf1.Text;

Comments

1

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.

1 Comment

Thanks for the answer but the code doesn't work, since the function nilaiHuruf can not define the (int a, int b, int c) variables.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.