Witam,
Jestem początkujący, ale w języku C# stworzyłem taki kod i pytanie, dlaczego wszędzie, gdzie chcę sprawdzić długość tablicy menu (menu.Length) pojawia mi sie błąd „Odwołanie do obiektu nie zostało ustawione na wystąpienie obiektu.” Menu było null.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Specjalne_Menu
{
class Menu
{
int AktualnyElement = 0;
string[] menu;
public Menu()
{
}
public void NaszeMenu()
{
Console.Clear();
string[] menu =
{ "W przod",
"W tyl",
"Pokaz opcje",
"O tworcy"
};
}
public void WypiszTablice()
{
Console.Clear();
for (int i = 0; i < menu.Length; i++)
{
if (i == AktualnyElement)
Console.WriteLine(">>" + menu[i]);
else
Console.WriteLine(menu[i]);
}
}
public bool CzytanieKlawiszy(ConsoleKey klawisz)
{
switch(klawisz)
{
case ConsoleKey.UpArrow:
AktualnyElement--;
break;
case ConsoleKey.DownArrow:
AktualnyElement++;
break;
case ConsoleKey.Escape:
return false;
default:
return true;
}
if (AktualnyElement < 0)
AktualnyElement = 0;
if (AktualnyElement >= menu.Length)
AktualnyElement = menu.Length - 1;
return true;
}
}
}
Za pomoc z góry dziękuję.
pozdrawiam