C
C#11mo ago
Maujey

How to share instance of an object in base class to its inherited classes?

class Window
{
protected WindowManager windowManager;

public Window()
{
windowManager = new WindowManager();
}

public virtual void DrawWindow()
{
}
}
class Window
{
protected WindowManager windowManager;

public Window()
{
windowManager = new WindowManager();
}

public virtual void DrawWindow()
{
}
}
using System;

class LockscreenWindow : Window
{
private PasswordManager passwordManager;


public override void DrawWindow()
{
passwordManager = new();

Console.Write("Enter Password: ");
string password = Console.ReadLine();

if(password == passwordManager.GetPassword())
{

Console.WriteLine("Password correct");
windowManager.SwitchWindow(WindowManager.WindowName.Homescreen);
}else
{
Console.WriteLine("Password Incorrect");
}
}

}
using System;

class LockscreenWindow : Window
{
private PasswordManager passwordManager;


public override void DrawWindow()
{
passwordManager = new();

Console.Write("Enter Password: ");
string password = Console.ReadLine();

if(password == passwordManager.GetPassword())
{

Console.WriteLine("Password correct");
windowManager.SwitchWindow(WindowManager.WindowName.Homescreen);
}else
{
Console.WriteLine("Password Incorrect");
}
}

}
8 Replies
Angius
Angius11mo ago
Since it's set in the ctor, you will need to call it
Maujey
MaujeyOP11mo ago
when I am trying the call the windowManager.SwitchWindow it gives me unhandled exception
Angius
Angius11mo ago
Even if with just public LockscreenWindow(): base() {}
Maujey
MaujeyOP11mo ago
what is ctor?
Angius
Angius11mo ago
Constructor
Maujey
MaujeyOP11mo ago
oh can you show example with a code snippet by any chance?
MODiX
MODiX11mo ago
Angius
Even if with just public LockscreenWindow(): base() {}
React with ❌ to remove this embed.
Angius
Angius11mo ago
I did The child class's constructor needs to call the parent class's constructor, for example with : base() Alternatively... you could set that value in the field directly, not in the ctor
Want results from more Discord servers?
Add your server