2013-07-28 2 views
-2
namespace WindowsFormsApplication2 
{ 
    public partial class Form1 : Form 
    { 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      Not.Text = 
       (Convert.ToInt32(quiz1per.Text)) * (Convert.ToInt32(quiz1poi.Text)) + 
       (Convert.ToInt32(quiz2per.Text)) * (Convert.ToInt32(quiz2poi.Text)) + 
       (Convert.ToInt32(odev1per.Text)) * (Convert.ToInt32(odev1poi.Text)) + 
       (Convert.ToInt32(odev2per.Text)) * (Convert.ToInt32(odev2poi.Text)) + 
       (Convert.ToInt32(vizeper.Text)) * (Convert.ToInt32(vizepoi.Text)) + 
       (Convert.ToInt32(finalper.Text)) * (Convert.ToInt32(finalpoi.Text)); 
     } 
    } 
} 

Вскоре, когда я добрался до последней точки с запятой, у меня возникла проблема преобразования int в строку. Я только начал С #, я хочу сначала изучить основные вещи.проблемы преобразования 'int' в 'string', также с математическими операторами

ответ

0

Каждый объект в C# имеет ToString(). Вы можете просто позвонить ToString() по вашему результату.

private void button1_Click(object sender, EventArgs e) 
{ 
    int result = 
     (Convert.ToInt32(quiz1per.Text)) * (Convert.ToInt32(quiz1poi.Text)) + 
     (Convert.ToInt32(quiz2per.Text)) * (Convert.ToInt32(quiz2poi.Text)) + 
     (Convert.ToInt32(odev1per.Text)) * (Convert.ToInt32(odev1poi.Text)) + 
     (Convert.ToInt32(odev2per.Text)) * (Convert.ToInt32(odev2poi.Text)) + 
     (Convert.ToInt32(vizeper.Text)) * (Convert.ToInt32(vizepoi.Text)) + 
     (Convert.ToInt32(finalper.Text)) * (Convert.ToInt32(finalpoi.Text)); 
    Not.Text = result.ToString(); 
} 

Вы также хотите использовать int.TryParse, а также. Если вы вводите нечисловое значение в текстовое поле, то Convert.ToInt32 выдаст исключение.

0

Вы не можете назначить int текстовому свойству textBox. Либо формат ИНТ в виде строки,

Not.Text = 
     (Convert.ToInt32(quiz1per.Text)) * (Convert.ToInt32(quiz1poi.Text)) + 
     (Convert.ToInt32(quiz2per.Text)) * (Convert.ToInt32(quiz2poi.Text)) + 
     (Convert.ToInt32(odev1per.Text)) * (Convert.ToInt32(odev1poi.Text)) + 
     (Convert.ToInt32(odev2per.Text)) * (Convert.ToInt32(odev2poi.Text)) + 
     (Convert.ToInt32(vizeper.Text)) * (Convert.ToInt32(vizepoi.Text)) + 
     (Convert.ToInt32(finalper.Text)) * (Convert.ToInt32(finalpoi.Text)).ToString(); 

или бросить его в строку

Not.Text = 
     (string)(Convert.ToInt32(quiz1per.Text)) * (Convert.ToInt32(quiz1poi.Text)) + 
     (Convert.ToInt32(quiz2per.Text)) * (Convert.ToInt32(quiz2poi.Text)) + 
     (Convert.ToInt32(odev1per.Text)) * (Convert.ToInt32(odev1poi.Text)) + 
     (Convert.ToInt32(odev2per.Text)) * (Convert.ToInt32(odev2poi.Text)) + 
     (Convert.ToInt32(vizeper.Text)) * (Convert.ToInt32(vizepoi.Text)) + 
     (Convert.ToInt32(finalper.Text)) * (Convert.ToInt32(finalpoi.Text)); 
+1

Вы не можете использовать 'int' для' string' в C#. Ваш второй пример даже не будет компилироваться. – shf301

0

Как ясно указано в заявке, вы не можете назначить int имуществу, которое должно принять string.

Вы можете позвонить по методу ToString(), чтобы преобразовать число в string.

Смежные вопросы