Novo

Forum kao aplikacija na telefonu

Dodajte forum na početni ekran i koristite ga kao aplikaciju, sa menijem na dnu ekrana.

  • 6 odgovora
  • 4.386 pregleda

Najnovija ↓

Cannot implicity convert type void to string 09.10.2014. u 21:00 · pre 146 meseci #1
gde je problem? vidim da mi metod void, a da trazim int, ali ne znam kako da resim to...

hvala unapred

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {

            int userInput;
            userInput = Convert.ToInt32(txtUserInput);
            lblResults.Text = MultiplicationTable(userInput);
        }
        
        private void MultiplicationTable(int input)
        {
            int[] numbers = {2,3,4,5,6,7,8,9,10};
            for (int x = 0; x < numbers.Length; x++)
            {
                int results;
                results = input*(numbers[x]);
                Console.WriteLine("{0} X {1} = {2}", input.ToString("N0"), numbers[x].ToString("N0"), results.ToString("N0"));
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
Potpis
life is the one,take care of them!

http://hardware-software.serbianforum.info/
Re: Cannot implicity convert type void to string 09.10.2014. u 21:29 · pre 146 meseci #2
Void metode ne vracaju nista pa ako uradis lblResults.Text = MultiplicationTable(userInput); normalno je da ce da ti da gresku.
I zasto mesas Console output i Winforms kontrole, ako ti je ovo Winforms projekt onda Console output nista ne vredi. Bolje bi ti bilo da ti ta metoda vraca int i to konvertujes u string
Potpis
http://i.imgur.com/V3feW.jpg
http://on.wsj.com/H9yjz6 -- India Graduates Millions, but Too Few Are Fit to Hire
Re: Cannot implicity convert type void to string 09.10.2014. u 21:32 · pre 146 meseci #3
Da, u stvari, umesto da bacas na konzolu vrati string:

Code (csharp):

        private void button1_Click(object sender, EventArgs e)
         {

             int userInput;
             userInput = Convert.ToInt32(txtUserInput);
             lblResults.Text = MultiplicationTable(userInput);
         }
         
         private string MultiplicationTable(int input)
         {
             int[] numbers = {2,3,4,5,6,7,8,9,10};
             for (int x = 0; x < numbers.Length; x++)
             {
                 int results;
                 results = input*(numbers[x]);
                 return String.Format("{0} X {1} = {2}", input.ToString("N0"), numbers[x].ToString("N0"), results.ToString("N0"));
             }
         }
 
Potpis
Sloba je za 12 godina promenio antropološki kod srpskog naroda. On je od jednog
naroda koji je bio veseo, pomalo površan, od jednog naroda koji je bio znatiželjan, koji
je voleo da vidi, da putuje, da upozna,
od naroda koji je bio kosmopolitski napravio narod koji je namršten, mrzovoljan,
sumnjicav, zaplašen, narod koji se stalno nešto žali, kome je stalno neko kriv - Z.Đinđić
Re: Cannot implicity convert type void to string 10.10.2014. u 01:14 · pre 146 meseci #6
Hvala svima, evo resio sam ovako

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 forum
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            int input;
            int.TryParse(txtUserInput.Text, out input);
            MultiplicationTable(input);
        }

        private void MultiplicationTable(int input)
        {
            string table = "";

            for (int i = 2; i <= 10; i++)
            {
                table = table + String.Format("{0} X {1} = {2} \r\n", input, i, input * i);
            }

            lblResult.Text = table;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       

       
      


    }
}
Potpis
life is the one,take care of them!

http://hardware-software.serbianforum.info/
Re: Cannot implicity convert type void to string 10.10.2014. u 12:56 · pre 146 meseci
izmenjeno 1 put, poslednji put ssi 10.10.2014. u 13:08
  1. ssi10.10.2014. u 13:08zapis iz teksta poruke
#7
@tomislav91

1) koristi StringBuilder, zato sto je string + string i mnogo puta + string jako sporo. Zato sto: 2000 stvari o c# -> string

2) Funkcija, Klasa itd, treba da rade jednu i samo jednu stvar i to najbolje na svetu. Single responsibility principle

3) TryParse moze da ne uspe :)

4) C# ima String.Empty, System.Environment.NewLine konstante.

5) MultiplicationTable funkcija na bi trebalo da bude u klasi Form1, nego static u klasi MyCustomTableFunctions na primer. Zasto ? Vidi 2), a uz to bice dostupna i na drugim mestima, neces valjda
da radis copy/paste MultiplicationTable u Form2, Form3,... FormN ?

Tako da treba da bude:

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 forum
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            int input = 0;
           
            if (int.TryParse(txtUserInput.Text, out input))
            {
                lblResult.Text = MultiplicationTable(input);
            }
        }

        private string MultiplicationTable(int input)
        {
            StringBuilder result = new StringBuilder();

            for (int i = 2; i <= 10; i++)
            {
                result.AppendFormat("{0} X {1} = {2}", input, i, input * i);
                result.AppendLine();
            }

            return result.ToString();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}




Ili jos bolje:

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 forum
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int input = 0;
           
            if (int.TryParse(txtUserInput.Text, out input))
            {
                lblResult.Text = MyCustomTableFunctions.MultiplicationTable(input);
            }
            else
            {
                lblResult.Text = "I can't parse that thing !";
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }

    public class MyCustomTableFunctions
    {
        public static string MultiplicationTable(int input)
        {
            StringBuilder result = new StringBuilder();

            for (int i = 2; i <= 10; i++)
            {
                result.AppendFormat("{0} X {1} = {2}", input, i, input * i);
                result.AppendLine();
            }

            return result.ToString();
        }
    }
}
Prijavite se da biste odgovorili u ovoj temi.PrijavaRegistracija

Premeštanje teme