C
C#2y ago
MrScautHD

❔ Why can i not override a Method from a class that extend a Interface

public interface IRegistry {

public static readonly List<IRegistry> RegistryTypes = new();

public virtual void Initialize(ContentManager content) {

}

public virtual void FixedUpdate() {

}

public virtual void Draw() {

}
}
public interface IRegistry {

public static readonly List<IRegistry> RegistryTypes = new();

public virtual void Initialize(ContentManager content) {

}

public virtual void FixedUpdate() {

}

public virtual void Draw() {

}
}
public abstract class Registry : IRegistry {

protected T Register<T, B>(string name, Dictionary<string, B> registryList, T type) where T : B {
registryList.Add(name, type);

return type;
}

protected T RegisterLoad<T>(string name, Dictionary<string, T> registryList, ContentManager content, string path) {
T type = content.Load<T>(path);
registryList.Add(name, type);

return type;
}
}
public abstract class Registry : IRegistry {

protected T Register<T, B>(string name, Dictionary<string, B> registryList, T type) where T : B {
registryList.Add(name, type);

return type;
}

protected T RegisterLoad<T>(string name, Dictionary<string, T> registryList, ContentManager content, string path) {
T type = content.Load<T>(path);
registryList.Add(name, type);

return type;
}
}
Here i try to override the Method "Initialize" but it does not work, is there a way to override it?
public class ClientFontRegistry : Registry {

// REGISTRY LIST
public static readonly Dictionary<string, Font> Fonts = new();

// REGISTRIES
public static Font Fontoe { get; private set; }


public void Initialize(ContentManager content) {
Fontoe = this.RegisterLoad("fontoe", Fonts, content, "font/fontoe.ttf");
}
}
public class ClientFontRegistry : Registry {

// REGISTRY LIST
public static readonly Dictionary<string, Font> Fonts = new();

// REGISTRIES
public static Font Fontoe { get; private set; }


public void Initialize(ContentManager content) {
Fontoe = this.RegisterLoad("fontoe", Fonts, content, "font/fontoe.ttf");
}
}
87 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MrScautHD
MrScautHDOP2y ago
i want it optional
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MrScautHD
MrScautHDOP2y ago
to override
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MrScautHD
MrScautHDOP2y ago
toolset?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MrScautHD
MrScautHDOP2y ago
public virtual void Draw() {

}
public virtual void Draw() {

}
dev told me its right to use it like that if i want it optional but yea i can remove the public
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Pobiega
Pobiega2y ago
If it's optional to provide it shouldn't be in the interface
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MrScautHD
MrScautHDOP2y ago
what should i use then?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MrScautHD
MrScautHDOP2y ago
but it is a register system so i not need everytime Draw method but when i need it i can just override it and it get called
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MrScautHD
MrScautHDOP2y ago
then i need to implement all methods what if i just need 1
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
333fred
333fred2y ago
Here i try to override the Method "Initialize" but it does not work, is there a way to override it?
What does this mean?
MrScautHD
MrScautHDOP2y ago
but then i can not call it like:
// INIT REGISTRY
foreach (IRegistry registry in IRegistry.RegistryTypes) {
registry.Initialize(this.Content);
}
// INIT REGISTRY
foreach (IRegistry registry in IRegistry.RegistryTypes) {
registry.Initialize(this.Content);
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MrScautHD
MrScautHDOP2y ago
^^ then would it not auto cast to the right type ... i need everytime to cast it
333fred
333fred2y ago
What do you mean when you say it does not work?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MrScautHD
MrScautHDOP2y ago
?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
333fred
333fred2y ago
You said "it does not work"
MrScautHD
MrScautHDOP2y ago
not if it in a interface
333fred
333fred2y ago
What do you mean by that Do you get an error? Do you not see the thing that you expected to be called called?
MrScautHD
MrScautHDOP2y ago
no i can not override the interface methods when i before use a class that extend from it
333fred
333fred2y ago
What do you mean by that???????
MrScautHD
MrScautHDOP2y ago
what?
333fred
333fred2y ago
When you say you can't override interface methods?
MrScautHD
MrScautHDOP2y ago
public class ClientFontRegistry : Registry {

// REGISTRY LIST
public static readonly Dictionary<string, Font> Fonts = new();

// REGISTRIES
public static Font Fontoe { get; private set; }


public override void Initialize(ContentManager content) {
Fontoe = this.RegisterLoad("fontoe", Fonts, content, "font/fontoe.ttf");
}
}
public class ClientFontRegistry : Registry {

// REGISTRY LIST
public static readonly Dictionary<string, Font> Fonts = new();

// REGISTRIES
public static Font Fontoe { get; private set; }


public override void Initialize(ContentManager content) {
Fontoe = this.RegisterLoad("fontoe", Fonts, content, "font/fontoe.ttf");
}
}
333fred
333fred2y ago
Why do you say that? What observable output are you basing this on?
MrScautHD
MrScautHDOP2y ago
look c# does can not handle the interface method if i do ClientFontRegistry : Registry : IRegistry Registry does brake it...
333fred
333fred2y ago
I am trying to help you out here, but just saying "it does not work" without actually saying what is not working is not helping me here
MrScautHD
MrScautHDOP2y ago
wtf
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MrScautHD
MrScautHDOP2y ago
no error message it just not work
333fred
333fred2y ago
Ok... then what is happening
MrScautHD
MrScautHDOP2y ago
it not allow me to override the methods from my interface
333fred
333fred2y ago
I do not believe you that this code has no error messages 🙂
MrScautHD
MrScautHDOP2y ago
because i before use Registry to extend from it
MrScautHD
MrScautHDOP2y ago
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
333fred
333fred2y ago
This is not the code I replied to
MrScautHD
MrScautHDOP2y ago
yea if i use override yes
MrScautHD
MrScautHDOP2y ago
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
333fred
333fred2y ago
Alright. So, after pulling a bunch of teeth here, let me see if I have this right. Code that effectively looks like this:
using System;

IRegistry registry = new ClientFontRegistry();
registry.Initialize();

public interface IRegistry
{
public virtual void Initialize()
{
Console.WriteLine("IRegistry");
}
}

public abstract class Registry : IRegistry
{
}

public class ClientFontRegistry : Registry
{
public void Initialize()
{
Console.WriteLine("ClientFontRegistry");
}
}
using System;

IRegistry registry = new ClientFontRegistry();
registry.Initialize();

public interface IRegistry
{
public virtual void Initialize()
{
Console.WriteLine("IRegistry");
}
}

public abstract class Registry : IRegistry
{
}

public class ClientFontRegistry : Registry
{
public void Initialize()
{
Console.WriteLine("ClientFontRegistry");
}
}
You expected to print ClientFontRegistry, but actually printed IRegistry Is that correct?
MrScautHD
MrScautHDOP2y ago
yes i mean yea Registry does brake it
333fred
333fred2y ago
Ok. Reimplement IRegistry on ClientFontRegistry What is happening here is that Registry needs to implement IRegistry, and IRegistry provides a default. So that's it. No more virtual dispatch occurs
MrScautHD
MrScautHDOP2y ago
yep now would it work but i wanna use the methods of Registry too ouf
333fred
333fred2y ago
You could reabstract Initialize in Registry if you wanted as well
MrScautHD
MrScautHDOP2y ago
yea...
333fred
333fred2y ago
And force inheritors of Registry to provide a non-default implementation
MrScautHD
MrScautHDOP2y ago
maybe is a static method then better from Registry or wait is it possible to extend from 2 things? like 1 interface
333fred
333fred2y ago
Extend? No
MrScautHD
MrScautHDOP2y ago
and one normal class like java 😄
333fred
333fred2y ago
You can implement multiple interfaces But you can only have 1 base class
MrScautHD
MrScautHDOP2y ago
oh
public class ClientFontRegistry : Registry, IRegistry {

// REGISTRY LIST
public static readonly Dictionary<string, Font> Fonts = new();

// REGISTRIES
public static Font Fontoe { get; private set; }


public override void Initialize(ContentManager content) {
Fontoe = this.RegisterLoad("fontoe", Fonts, content, "font/fontoe.ttf");
}
}
public class ClientFontRegistry : Registry, IRegistry {

// REGISTRY LIST
public static readonly Dictionary<string, Font> Fonts = new();

// REGISTRIES
public static Font Fontoe { get; private set; }


public override void Initialize(ContentManager content) {
Fontoe = this.RegisterLoad("fontoe", Fonts, content, "font/fontoe.ttf");
}
}
so that works thxx you helped me
333fred
333fred2y ago
It does, but it also possibly doesn't
MrScautHD
MrScautHDOP2y ago
rider tells me that it get called so im sure it works let me try it yepp thxxx for our help
333fred
333fred2y ago
If Registry overrode functionality, you'll lose it
MrScautHD
MrScautHDOP2y ago
public class Registry {

protected T Register<T, B>(string name, Dictionary<string, B> registryList, T type) where T : B {
registryList.Add(name, type);

return type;
}

protected T RegisterLoad<T>(string name, Dictionary<string, T> registryList, ContentManager content, string path) {
T type = content.Load<T>(path);
registryList.Add(name, type);

return type;
}
}
public class Registry {

protected T Register<T, B>(string name, Dictionary<string, B> registryList, T type) where T : B {
registryList.Add(name, type);

return type;
}

protected T RegisterLoad<T>(string name, Dictionary<string, T> registryList, ContentManager content, string path) {
T type = content.Load<T>(path);
registryList.Add(name, type);

return type;
}
}
thats why i not call it anymore from it
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MrScautHD
MrScautHDOP2y ago
that would work too but i think that way i has now is better
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MrScautHD
MrScautHDOP2y ago
But thx for our help!
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MrScautHD
MrScautHDOP2y ago
public interface IRegistry {

public static readonly List<IRegistry> RegistryTypes = new();

virtual void Initialize(ContentManager content) {

}

virtual void FixedUpdate() {

}

virtual void Draw() {

}
}
public interface IRegistry {

public static readonly List<IRegistry> RegistryTypes = new();

virtual void Initialize(ContentManager content) {

}

virtual void FixedUpdate() {

}

virtual void Draw() {

}
}
public class Registry {

protected T Register<T, B>(string name, Dictionary<string, B> registryList, T type) where T : B {
registryList.Add(name, type);

return type;
}

protected T RegisterLoad<T>(string name, Dictionary<string, T> registryList, ContentManager content, string path) {
T type = content.Load<T>(path);
registryList.Add(name, type);

return type;
}
}
public class Registry {

protected T Register<T, B>(string name, Dictionary<string, B> registryList, T type) where T : B {
registryList.Add(name, type);

return type;
}

protected T RegisterLoad<T>(string name, Dictionary<string, T> registryList, ContentManager content, string path) {
T type = content.Load<T>(path);
registryList.Add(name, type);

return type;
}
}
public class ClientFontRegistry : Registry, IRegistry {

// REGISTRY LIST
public static readonly Dictionary<string, Font> Fonts = new();

// REGISTRIES
public static Font Fontoe { get; private set; }

public void Initialize(ContentManager content) {
Fontoe = this.RegisterLoad("fontoe", Fonts, content, "font/fontoe.ttf");
}
}
public class ClientFontRegistry : Registry, IRegistry {

// REGISTRY LIST
public static readonly Dictionary<string, Font> Fonts = new();

// REGISTRIES
public static Font Fontoe { get; private set; }

public void Initialize(ContentManager content) {
Fontoe = this.RegisterLoad("fontoe", Fonts, content, "font/fontoe.ttf");
}
}
im more fan of this, it looks way cleaner
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MrScautHD
MrScautHDOP2y ago
oh ok
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
333fred
333fred2y ago
?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
333fred
333fred2y ago
$tias
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y ago
Orannis#3333
You can implement multiple interfaces
React with ❌ to remove this embed.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MrScautHD
MrScautHDOP2y ago
// INIT REGISTRY
foreach (IRegistry registry in IRegistry.RegistryTypes) {
registry.Initialize(this.Content);
}
// INIT REGISTRY
foreach (IRegistry registry in IRegistry.RegistryTypes) {
registry.Initialize(this.Content);
}
i call it like that... that would just ignore this methods because
public static readonly List<IRegistry> RegistryTypes = new();
public static readonly List<IRegistry> RegistryTypes = new();
i get the right type but c# still use the default classes
Thinker
Thinker2y ago
Why does Registry implement IRegistry? I think what is happening is that ClientFontRegistry inherits from Registry, Registry implements IRegistry, so when you try to implement the methods from IRegistry on FontClientRegistry then it just ignores those since the interface is already implemented in the base class.
Florian Voß
Florian Voß2y ago
I'm seeing a bit of a violation here against the interface seggregation principle. Do not depend on interfaces you don't need. Your have two types that each use this interface but none of them fully uses it. What you've done is essentially you have created an interface which should partially be implemented by Registry and partially implemented by ClientFontRegistry. Just make two smaller interfaces having one get implemented by Registry and one get implemented by ClientFontRegistry.
333fred
333fred2y ago
Remember to $close when the question has been answered 🙂
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server