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
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
I don't think a static class can inherit from a non-static class
how to embed code?
here
$code
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/$code
this is lockacreen window class
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/$code
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/Read what the bot posted instead of using the bot command, just FYI
it inherits from window class
And, again, static classes cannot inherit non-static classes
wait
Why is your
LockScreenWindow
static anyway?thats what I wanna tell
that's why i suggested trying it
because it's not valid C#
so this question doesn't make sense
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/You don't need the
$code
, it's a command that makes the bot tell you how to post the codeoh
Just triple backtics,
cs
, new line, code, new line, triple backtickssee this
I dont want to instantiate each window i have inheriting from window class
Well, tough
Window
is not static, so no class that inherits it can be staticSo I make windows static?
Do you have access to the
Window
class?yes
Is it a class you yourself created?
yes
indeed
Then sure, make it
static
And see what happensi think you'll find that static and inheritance don't mix
DrawWindow is a virtual method here
and?
lockscreenwindow inherits from it
that code is not valid
static classes cannot inherit
But i want to call drawwindow of locksreen(overridem method)here without instantiatimg here
thats where I am stuck
idk what you want us to say
you can't call an instance method without an instance
see here
see what?
...i can see the literal code
what do you want us to see about it?
LockscreenWindow.DrawWindow();
It is statically called method
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
hmm
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
see jimmacle, Imagine if I have multiple(many)windows, will I call each of their functions by instantiating their respective classes?
yes
That would make an absolute mess
your current code seems like more of a mess tbh
Like mess what?
I am just trying to organise some code
why do you want windows to be static in the first place?
I didnt wanted it to be static
I was just testing
so what do you actually want from us?
What's messy about instantiating multiple classes?
ya know for instantiatimg classes we do it like this
MyClass test = new MyClass();
imagine i have so many classes like this
i don't see a problem
you're trying to avoid writing code that must be written
Also,
there are ways to shorten the code
There are also ways to reuse instances, should that be needed
aha
Either with a simple singleton or dependency injection
thats what I wanted!
well, it's not what you asked at all and we can't read minds
so sorry I am bad at explaining things
So you can, technically, do
and use it with
which you'd need to do in some capacity if windows need persistent state either way
yess
thanks a lot