C#
C#

help

Root Question Message

Ayymoss
Ayymoss12/11/2022
✅ Determining a Configuration Type

I'm running into a problem trying to access a specific property of a configuration type. I'm unsure if I'm having this problem due to the layout of my configuration or just part of C# I've not yet ran into. 🙂

I have an enum VoteType with 4 members, Kick, Ban, Map, Skip, Mute.

I have a configuration for those relevant types KickConfiguration, BanConfiguration, MuteConfiguration etc these configurations inherit from BaseConfiguration

Each 'TYPE'Configuration has a inherited property Cooldown.

In my code, I need to access this cooldown specific to the vote type.

How do I do this? In the method I need it, I have access to the VoteType enum. I'm not sure if there's a way to infer the configuration type from this enum?

The full property 'path'(?) is...

_configuration.VoteConfiguration.BanConfiguration.Cooldown;
_configuration.VoteConfiguration.KickConfiguration.Cooldown;
// etc


Let's say I've got this method..

public static TimeSpan Cooldown(VoteType voteType) 
{
return new TimeSpan( /* What do I put here to get the per-voteType relevant cooldown?*/ );
}
Ero
Ero12/11/2022
If statements?
Ero
Ero12/11/2022
Or a switch expression?
Ayymoss
Ayymoss12/11/2022
I thought this, but it feels unnecessarily verbose.
Ayymoss
Ayymoss12/11/2022
Surely there's a smarter way to infer the type?
Ero
Ero12/11/2022
Dictionary?
Ayymoss
Ayymoss12/11/2022
Again. I don't want to make a static dict with all key and types. 😄 It's way to verbose and not scalable.
Ero
Ero12/11/2022
You don't really have any other options
Ayymoss
Ayymoss12/11/2022
I tried using Generics, but I'm not too familiar with them and I believe you need to already know the type beforehand?
Ayymoss
Ayymoss12/11/2022
I suppose this is a problem with the layout.

Do you have any suggestions on how I could redo my config with this per-votetype-configuration in mind?
Ero
Ero12/11/2022
Difficult to make any assumptions without seeing use cases and implementations and intentions
Ayymoss
Ayymoss12/11/2022
_configuration.VotePassPercentage
This was my old format, and is now broken. 🙂

I need to get
_configuration.VoteConfiguration.<Type>.VotePassPercentage now. Again, the enum is passed through to this method.
Ero
Ero12/11/2022
Yeah that doesn't answer anything
Ero
Ero12/11/2022
That's your current setup that you asked for suggestions to change
Ero
Ero12/11/2022
I'm saying it's impossible to tell you how to change it without more context
Ayymoss
Ayymoss12/11/2022
I'm trying to provide that context. Can you be more specific in what context you need?

I can start dumping entire code snippets out but I can't imagine this helpful.
phaseshift
phaseshift12/11/2022
Are you saying the property is not on the base class? If not, what is the problem?
phaseshift
phaseshift12/11/2022
Passing round an enum into lots of different methods expecting them to switch or otherwise iterate the options is often not a good pattern. Just pass a single thing that is set up appropriately
Ayymoss
Ayymoss12/11/2022
Possibly my problem. I'm unsure.
I'm using the enum to pass through to functions to know what the calling vote was. But I also need to get relevant configuration results from it.

It'd be nice at the top of the function to call like...

var voteConfig = <someway to retreive the specific voteconfig based off of the passed enum>;
// then do
voteConfig.Cooldown;


The property in question here is part of the BaseConfiguration class. But some properties will not be.
phaseshift
phaseshift12/11/2022
so you only need a dictionary/list of configs somewhere. you can even have the enum as a property on the actual configs
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy