Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Cannot implicity convert type void to string

[es] :: .NET :: Cannot implicity convert type void to string

[ Pregleda: 2977 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

tomislav91

Član broj: 206601
Poruke: 316
*.dynamic.isp.telekom.rs.



+1 Profil

icon Cannot implicity convert type void to string09.10.2014. u 20:00 - pre 116 meseci
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();
        }
    }
}

life is the one,take care of them!

http://hardware-software.serbianforum.info/
 
Odgovor na temu

AMD guy
Miroslav
.NET developer

Član broj: 128930
Poruke: 1007

Sajt: www.its.edu.rs


+38 Profil

icon Re: Cannot implicity convert type void to string09.10.2014. u 20:29 - pre 116 meseci
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
http://i.imgur.com/V3feW.jpg
http://on.wsj.com/H9yjz6 -- India Graduates Millions, but Too Few Are Fit to Hire
 
Odgovor na temu

mmix
Miljan Mitrović
Profesorkin muz
Passau, Deutschland

SuperModerator
Član broj: 17944
Poruke: 6042



+4631 Profil

icon Re: Cannot implicity convert type void to string09.10.2014. u 20:32 - pre 116 meseci
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"));
             }
         }
 

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ć
 
Odgovor na temu

AMD guy
Miroslav
.NET developer

Član broj: 128930
Poruke: 1007

Sajt: www.its.edu.rs


+38 Profil

icon Re: Cannot implicity convert type void to string09.10.2014. u 20:34 - pre 116 meseci
Meni je ostalo u navici da ako radim sa brojevima onda ta metoda mi vraca broj koji kasnije mogu da konvertujem u string ili sto god.
http://i.imgur.com/V3feW.jpg
http://on.wsj.com/H9yjz6 -- India Graduates Millions, but Too Few Are Fit to Hire
 
Odgovor na temu

30yo

Član broj: 322574
Poruke: 13
*.cpe.vektor.net.



+2 Profil

icon Re: Cannot implicity convert type void to string09.10.2014. u 22:49 - pre 116 meseci
u svakom slucaju se mmix malo presao kad mu je return u petlji
 
Odgovor na temu

tomislav91

Član broj: 206601
Poruke: 316
*.dynamic.isp.telekom.rs.



+1 Profil

icon Re: Cannot implicity convert type void to string10.10.2014. u 00:14 - pre 116 meseci
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();
        }

       

       
      


    }
}

life is the one,take care of them!

http://hardware-software.serbianforum.info/
 
Odgovor na temu

ssi

Član broj: 305920
Poruke: 333
*.static.sbb.rs.



+377 Profil

icon Re: Cannot implicity convert type void to string10.10.2014. u 11:56 - pre 116 meseci
@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();
        }
    }
}


[Ovu poruku je menjao ssi dana 10.10.2014. u 13:08 GMT+1]
 
Odgovor na temu

[es] :: .NET :: Cannot implicity convert type void to string

[ Pregleda: 2977 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.