Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [C#] Błąd przy kompilowanu
Forum PHP.pl > Inne > Hydepark
Babcia@Stefa
Witam, uczę się C# z kursu centrumxp.pl i mam problem z tym kodem (sam napisałem bez pomocy... ale za to jest gdzieś błąd):

Kod
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestProgram
{
    class Program
    {
        static void Main()
        {
            // Variables
            int[] Numbers = new int[2];
            int Match;
            bool Fail = false;
            string Action;
            string ActionName;

            // What do you want to do?
            System.Console.WriteLine("Co chcesz zrobic:\n1. Dodac\n2. Odjac\n3. Pomnozyc\n4. Podzielic");
            Action = System.Console.ReadLine();

            // Get two numbers
            System.Console.WriteLine("Podaj liczbe nr.1:");
            Numbers[0] = System.Convert.ToInt32(System.Console.ReadLine());
            System.Console.WriteLine("Podaj liczbe nr.2:");
            Numbers[1] = System.Convert.ToInt32(System.Console.ReadLine());

            // Selecting action
            switch (Action)
            {
                case "1":
                    ActionName = (string)"dodawania";
                    Match = Numbers[0] + Numbers[1];
                break;

                case "2":
                    ActionName = (string)"odejmowania";
                    Match = Numbers[0] - Numbers[1];
                break;

                case "3":
                    ActionName = (string)"mnozenia";
                    Match = Numbers[0] * Numbers[1];
                break;

                case "4":
                    ActionName = (string)"dzielenia";
                    Match = Numbers[0] / Numbers[1];
                break;

                default:
                    Fail = true;
                break;
            }

            if (Fail == false)
                System.Console.WriteLine("Wynik "+ActionName+": "+Match);
            else
                System.Console.WriteLine("Nie poprawna akcja wybrana.");
        }
    }
}


Cytat
Error 1 Use of unassigned local variable 'ActionName' C:\Documents and Settings\*\Ustawienia lokalne\Dane aplikacji\Temporary Projects\TestProgram\Program.cs 53 51 TestProgram

Error 2 Use of unassigned local variable 'Match' C:\Documents and Settings\*\Ustawienia lokalne\Dane aplikacji\Temporary Projects\TestProgram\Program.cs 53 67 TestProgram


@edit
W C# Mogę używać MySQL?


@edit
Jaka funkcja sprawdza czy argument jest liczbą/ciągiem itp (PHP: is_string(), is_int(), is_array() itp.)?

Dziękuję, Babcia@Stefa
nospor
jak glosi komunikat uzywasz nie okreslonej (nie podales dla niej wartosci) zmiennej.

Dla ActionName przypisujesz wartosc w switch, ale w tym switch nie dales nic dla default, czyli teroretycznie ActionName moze nie byc nigdy okreslone.
Podobnie ma sie Match.
Babcia@Stefa
Dzięki nospor działa poprawnie smile.gif

@edit
W C# Mogę używać MySQL?


@edit
Kod
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestProgram
{
    class Program
    {
        static void Main()
        {
            // Variables
            int[] Numbers = new int[2];
            int Match;
            bool Fail = false;
            string Action;
            string ActionName;

            // What do you want to do?
            System.Console.WriteLine("Co chcesz zrobic:\n1. Dodac\n2. Odjac\n3. Pomnozyc\n4. Podzielic");
            Action = System.Console.ReadLine();

            // Please give the two numbers
            System.Console.WriteLine("Podaj liczbe nr.1:");
            Numbers[0] = System.Convert.ToInt32(System.Console.ReadLine());
            System.Console.WriteLine("Podaj liczbe nr.2:");
            Numbers[1] = System.Convert.ToInt32(System.Console.ReadLine());

            // Selecting action
            switch (Action)
            {
                case "1":
                    ActionName = (string)"dodawania";
                    Match = Numbers[0] + Numbers[1];
                break;

                case "2":
                    ActionName = (string)"odejmowania";
                    Match = Numbers[0] - Numbers[1];
                break;

                case "3":
                    ActionName = (string)"mnozenia";
                    Match = Numbers[0] * Numbers[1];
                break;

                case "4":
                    ActionName = (string)"dzielenia";
                    Match = Numbers[0] / Numbers[1];
                break;

                default:
                    Fail = true;
                    ActionName = "brak";
                    Match = 0;
                break;
            }

            if (Fail == false)
                System.Console.WriteLine("Wynik "+ActionName+": "+Match);
            else
                System.Console.WriteLine("Nie poprawna akcja wybrana.");

            System.Console.Read();

            Main();
        }
    }
}
batman
Nie żebym się czepiał, ale do C# są inne fora. Polecam codeguru.pl.
A co do pytania - tak można używać mysql z c#. Musisz mieć sterownik odbc (lub oledb - nie pamiętam teraz który, dawno już nie pisałem w c#) do mysql-a.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.