C
C#7mo 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
Jimmacle7mo 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
Angius7mo ago
I don't think a static class can inherit from a non-static class
Maujey
Maujey7mo ago
how to embed code? here
Angius
Angius7mo ago
$code
MODiX
MODiX7mo 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
Maujey7mo 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
MODiX7mo 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
Maujey7mo ago
$code
class Window
{
public virtual void DrawWindow()
{

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

}
}
MODiX
MODiX7mo 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
Angius7mo ago
Read what the bot posted instead of using the bot command, just FYI
Maujey
Maujey7mo ago
it inherits from window class
Angius
Angius7mo ago
And, again, static classes cannot inherit non-static classes
Maujey
Maujey7mo ago
wait
Angius
Angius7mo ago
Why is your LockScreenWindow static anyway?
Maujey
Maujey7mo ago
thats what I wanna tell
Jimmacle
Jimmacle7mo ago
that's why i suggested trying it because it's not valid C# so this question doesn't make sense
Maujey
Maujey7mo 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
MODiX7mo 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
Angius7mo ago
You don't need the $code, it's a command that makes the bot tell you how to post the code
Maujey
Maujey7mo ago
oh
Angius
Angius7mo ago
Just triple backtics, cs, new line, code, new line, triple backticks
Maujey
Maujey7mo ago
see this I dont want to instantiate each window i have inheriting from window class
Angius
Angius7mo ago
Well, tough Window is not static, so no class that inherits it can be static
Maujey
Maujey7mo ago
So I make windows static?
Angius
Angius7mo ago
Do you have access to the Window class?
Maujey
Maujey7mo ago
yes
Angius
Angius7mo ago
Is it a class you yourself created?
Maujey
Maujey7mo ago
yes indeed
Angius
Angius7mo ago
Then sure, make it static And see what happens
Maujey
Maujey7mo ago
class Window
{
public virtual void DrawWindow()
{

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

}
}
Jimmacle
Jimmacle7mo ago
i think you'll find that static and inheritance don't mix
Maujey
Maujey7mo ago
DrawWindow is a virtual method here
Jimmacle
Jimmacle7mo ago
and?
Maujey
Maujey7mo ago
lockscreenwindow inherits from it
Jimmacle
Jimmacle7mo ago
that code is not valid static classes cannot inherit
Maujey
Maujey7mo ago
But i want to call drawwindow of locksreen(overridem method)here without instantiatimg here thats where I am stuck
Jimmacle
Jimmacle7mo ago
idk what you want us to say you can't call an instance method without an instance
Maujey
Maujey7mo ago
see here
Jimmacle
Jimmacle7mo ago
see what?
Maujey
Maujey7mo 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
Jimmacle7mo ago
...i can see the literal code what do you want us to see about it?
Maujey
Maujey7mo ago
LockscreenWindow.DrawWindow(); It is statically called method
Jimmacle
Jimmacle7mo 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
Maujey7mo ago
hmm
Jimmacle
Jimmacle7mo 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
Maujey7mo ago
see jimmacle, Imagine if I have multiple(many)windows, will I call each of their functions by instantiating their respective classes?
Jimmacle
Jimmacle7mo ago
yes
Maujey
Maujey7mo ago
That would make an absolute mess
Jimmacle
Jimmacle7mo ago
your current code seems like more of a mess tbh
Maujey
Maujey7mo ago
Like mess what? I am just trying to organise some code
Jimmacle
Jimmacle7mo ago
why do you want windows to be static in the first place?
Maujey
Maujey7mo ago
I didnt wanted it to be static I was just testing
Jimmacle
Jimmacle7mo ago
so what do you actually want from us?
Angius
Angius7mo ago
What's messy about instantiating multiple classes?
Maujey
Maujey7mo ago
ya know for instantiatimg classes we do it like this MyClass test = new MyClass(); imagine i have so many classes like this
Jimmacle
Jimmacle7mo ago
i don't see a problem you're trying to avoid writing code that must be written
Angius
Angius7mo 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
Maujey7mo ago
aha
Angius
Angius7mo ago
Either with a simple singleton or dependency injection
Maujey
Maujey7mo ago
thats what I wanted!
Jimmacle
Jimmacle7mo ago
well, it's not what you asked at all and we can't read minds
Maujey
Maujey7mo ago
so sorry I am bad at explaining things
Angius
Angius7mo 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
Jimmacle7mo ago
which you'd need to do in some capacity if windows need persistent state either way
Maujey
Maujey7mo ago
yess thanks a lot
Want results from more Discord servers?
Add your server
More Posts