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.
nilaiHuruf()function - you want to assign value to textbox or you want to return string as result?return nilaiHuruf1.ToString();withreturn nilaiHuruf1.Text;