I'm new with C# and my console application for some reason the interface IService doesn't transfer the value to the Service. I tried to debug: _service.GetAccountName(name), name has value and then I get System.NullReferenceException for _service.GetAccountName. What I'm doing wrong here? Thank you for your patience.
Program:
static void Main(string[] args)
{
var newMenu = new Menu();
newMenu.Show();
}
Menu
public class Menu
{
private IService _service { get; set; }
public void Show()
{
var shouldStop = "";
while (shouldStop.ToLower() != "yes")
{
Console.WriteLine("Please select one of the following options:");
Console.WriteLine("1.Create BC \n2.View BC\n3.Edit BC\n4.Delete BC");
var response = int.Parse(Console.ReadLine());
switch (response)
{
case 1:
Console.WriteLine("You choose to create bank account");
ChooseTypeOfAccount();
break;
case 2:
Console.WriteLine("You choose to view bank account");
var user = ChooseToViewBankAccount();
if (user != null)
{
Console.WriteLine("Id:" user.Id "\n" user.Name "\n" user.Surname);
}
else
{
Console.WriteLine("User can not be found");
}
Console.WriteLine("ALL USERS");
var allUs = _service.GetAllUsers();
for(var i = 0; i < allUs.Item1.Count; i )
{
Console.WriteLine(allUs.Item1[i].Name " " allUs.Item1[i].Surname);
for(var j = 0; j < allUs.Item2.Count; j )
{
Console.WriteLine(allUs.Item2[i].Name " " allUs.Item2[i].Surname);
}
}
break;
case 3:
Console.WriteLine("You choose to edit BC");
break;
case 4:
Console.WriteLine("You choose to delete BC");
break;
default:
Console.WriteLine("oops wrong choice");
break;
}
Console.WriteLine("To exit type yes");
shouldStop = Console.ReadLine();
}
}
private User ChooseToViewBankAccount()
{
Console.WriteLine("Please enter your name");
var name = Console.ReadLine();
User user = _service.GetAccountByName(name);
return user;
}
Service:
public class Service : IService
{
public AccountsData _accountsRepository { get; set; }
public ChildAccountsData _childAccountRepository { get; set; }
public Service()
{
_accountsRepository = new AccountsData();
_childAccountRepository = new ChildAccountsData();
}
IService
public interface IService
{
User GetAccountByName(string name);
void CreateRegularAccount(string name, string surname);
void CreateChildAccount(string childName, string childSurname);
(List<Account>, List<ChildAccount>) GetAllUsers();
}
CodePudding user response:
as currently written you need to do
static void Main(string[] args)
{
var newMenu = new Menu();
newMenu.Service = new Service();
newMenu.Show();
}
you would need ot change
private IService _service { get; set; }
to
public IService _service { get; set; }
but thats probably not what you want (allow callers to change Service as you go along). SO do this
private IService _service = new Service();
PS, dont call you service IService
call it something useful like IDBAccess
or somethig like that.
CodePudding user response:
the most common way is to use dependency injection using a constructor.
var newMenu = new Menu(new Service ());
but you will need to add the constructor to Menu class
public class Menu
{
private IService _service { get; set; }
public Menu (IService service)
{
_service = service;
}
....
}
or less popular way
var newMenu= new Menu { Service = new Service()}
but in this case you will have to change Service property to public
public class Menu
{
public IService Service { get; set; }
.....
}