[ tomislav91 @ 09.10.2014. 20:00 ] @
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();
        }
    }
}
[ AMD guy @ 09.10.2014. 20:29 ] @
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
[ mmix @ 09.10.2014. 20:32 ] @
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"));
             }
         }
 
[ AMD guy @ 09.10.2014. 20:34 ] @
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.
[ 30yo @ 09.10.2014. 22:49 ] @
u svakom slucaju se mmix malo presao kad mu je return u petlji
[ tomislav91 @ 10.10.2014. 00:14 ] @
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();
        }

       

       
      


    }
}
[ ssi @ 10.10.2014. 11:56 ] @
@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]