C
C#11mo ago
Maujey

Calling a method from static class which also inherits from a non static class

Calling a method from static class which also inherits from a non static class
65 Replies
Jimmacle
Jimmacle11mo ago
do you have a question? your post isn't formatted as one i suggest trying what you're asking about, because you'll probably find your answer
Angius
Angius11mo ago
I don't think a static class can inherit from a non-static class
Maujey
Maujey11mo ago
how to embed code? here
Angius
Angius11mo ago
$code
MODiX
MODiX11mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Maujey
Maujey11mo ago
$code
static class LockscreenWindow : Window
{
public override void DrawWindow()
{
Console.WriteLine("hi");
}
}
static class LockscreenWindow : Window
{
public override void DrawWindow()
{
Console.WriteLine("hi");
}
}
this is lockacreen window class
MODiX
MODiX11mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Maujey
Maujey11mo ago
$code
class Window
{
public virtual void DrawWindow()
{

}
}
class Window
{
public virtual void DrawWindow()
{

}
}
MODiX
MODiX11mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Angius
Angius11mo ago
Read what the bot posted instead of using the bot command, just FYI
Maujey
Maujey11mo ago
it inherits from window class
Angius
Angius11mo ago
And, again, static classes cannot inherit non-static classes
Maujey
Maujey11mo ago
wait
Angius
Angius11mo ago
Why is your LockScreenWindow static anyway?
Maujey
Maujey11mo ago
thats what I wanna tell
Jimmacle
Jimmacle11mo ago
that's why i suggested trying it because it's not valid C# so this question doesn't make sense
Maujey
Maujey11mo ago
using System;

class WindowManager
{
public enum WindowName
{
Lockscreen,
Homescreen
}


public void SwitchWindow(WindowName _wName)
{
if(_wName == WindowName.Lockscreen)
{
LockscreenWindow.DrawWindow();
}
}

}
using System;

class WindowManager
{
public enum WindowName
{
Lockscreen,
Homescreen
}


public void SwitchWindow(WindowName _wName)
{
if(_wName == WindowName.Lockscreen)
{
LockscreenWindow.DrawWindow();
}
}

}
MODiX
MODiX11mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Angius
Angius11mo ago
You don't need the $code, it's a command that makes the bot tell you how to post the code
Maujey
Maujey11mo ago
oh
Angius
Angius11mo ago
Just triple backtics, cs, new line, code, new line, triple backticks
Maujey
Maujey11mo ago
see this I dont want to instantiate each window i have inheriting from window class
Angius
Angius11mo ago
Well, tough Window is not static, so no class that inherits it can be static
Maujey
Maujey11mo ago
So I make windows static?
Angius
Angius11mo ago
Do you have access to the Window class?
Maujey
Maujey11mo ago
yes
Angius
Angius11mo ago
Is it a class you yourself created?
Maujey
Maujey11mo ago
yes indeed
Angius
Angius11mo ago
Then sure, make it static And see what happens
Maujey
Maujey11mo ago
class Window
{
public virtual void DrawWindow()
{

}
}
class Window
{
public virtual void DrawWindow()
{

}
}
Jimmacle
Jimmacle11mo ago
i think you'll find that static and inheritance don't mix
Maujey
Maujey11mo ago
DrawWindow is a virtual method here
Jimmacle
Jimmacle11mo ago
and?
Maujey
Maujey11mo ago
lockscreenwindow inherits from it
Jimmacle
Jimmacle11mo ago
that code is not valid static classes cannot inherit
Maujey
Maujey11mo ago
But i want to call drawwindow of locksreen(overridem method)here without instantiatimg here thats where I am stuck
Jimmacle
Jimmacle11mo ago
idk what you want us to say you can't call an instance method without an instance
Maujey
Maujey11mo ago
see here
Jimmacle
Jimmacle11mo ago
see what?
Maujey
Maujey11mo ago
using System;

class WindowManager
{
public enum WindowName
{
Lockscreen,
Homescreen
}


public void SwitchWindow(WindowName _wName)
{
if(_wName == WindowName.Lockscreen)
{
LockscreenWindow.DrawWindow();
}
}

}
using System;

class WindowManager
{
public enum WindowName
{
Lockscreen,
Homescreen
}


public void SwitchWindow(WindowName _wName)
{
if(_wName == WindowName.Lockscreen)
{
LockscreenWindow.DrawWindow();
}
}

}
Jimmacle
Jimmacle11mo ago
...i can see the literal code what do you want us to see about it?
Maujey
Maujey11mo ago
LockscreenWindow.DrawWindow(); It is statically called method
Jimmacle
Jimmacle11mo ago
which isn't valid based on your other code, because that class is not declared in a valid way to begin with you need to back all the way up and stop sharing code based on other code that doesn't compile it's not statically called, because it's not valid, because your class declaration is wrong
Maujey
Maujey11mo ago
hmm
Jimmacle
Jimmacle11mo ago
static classes cannot inherit from any other class, and to call instance methods you need an instance so either 1. don't inherit from Window or 2. make LockscreenWindow non-static and create an instance of it
Maujey
Maujey11mo ago
see jimmacle, Imagine if I have multiple(many)windows, will I call each of their functions by instantiating their respective classes?
Jimmacle
Jimmacle11mo ago
yes
Maujey
Maujey11mo ago
That would make an absolute mess
Jimmacle
Jimmacle11mo ago
your current code seems like more of a mess tbh
Maujey
Maujey11mo ago
Like mess what? I am just trying to organise some code
Jimmacle
Jimmacle11mo ago
why do you want windows to be static in the first place?
Maujey
Maujey11mo ago
I didnt wanted it to be static I was just testing
Jimmacle
Jimmacle11mo ago
so what do you actually want from us?
Angius
Angius11mo ago
What's messy about instantiating multiple classes?
Maujey
Maujey11mo ago
ya know for instantiatimg classes we do it like this MyClass test = new MyClass(); imagine i have so many classes like this
Jimmacle
Jimmacle11mo ago
i don't see a problem you're trying to avoid writing code that must be written
Angius
Angius11mo ago
Also,
var test = new MyClass();
MyClass test = new();
var test = new MyClass();
MyClass test = new();
there are ways to shorten the code There are also ways to reuse instances, should that be needed
Maujey
Maujey11mo ago
aha
Angius
Angius11mo ago
Either with a simple singleton or dependency injection
Maujey
Maujey11mo ago
thats what I wanted!
Jimmacle
Jimmacle11mo ago
well, it's not what you asked at all and we can't read minds
Maujey
Maujey11mo ago
so sorry I am bad at explaining things
Angius
Angius11mo ago
So you can, technically, do
static class Windows
{
public static OrderWindow Order = new();
public static UserWindow User = new();
public static SettingsWindow Settings = new();
}
static class Windows
{
public static OrderWindow Order = new();
public static UserWindow User = new();
public static SettingsWindow Settings = new();
}
and use it with
Windows.Settings
Windows.Settings
Jimmacle
Jimmacle11mo ago
which you'd need to do in some capacity if windows need persistent state either way
Maujey
Maujey11mo ago
yess thanks a lot
Want results from more Discord servers?
Add your server