How can I define a custom named class to a string class?

Hi guys
in my code, nearly all objects here have a type variable to tell the code what this object is, which helps me a lot in ordering and working on those stuff.
However, all of them i have used the class String, and because of that, sometimes when I’m with a badly named string variable I need to take some time to check whether the string variable is a type variable or not, and that is annoying.
I tried searching online, they all say something like:

using custom_type_class = System.String;

However, the class i want, i wish it can be identified with just declaring in one file, but the line I mentioned above needs to be declared every file, still making it annoying.
So my question is, is there anyway I can define a customly named class to a string class, or works exactly the same as a string class, with simple declaring?

Im not begging for answers, im just trying to improve the code.
If there‘s no better solution, please just tell me, and I will use the best one I’ve got (the one above) :sob:
Thank you all for helping me

This is a bad way to classify the objects in your game.

If your objects are plain C# classes/structs etc. you are better off using interfaces to do the classifications etc. using them.

If your objects are gameobjects you are better off either using the built-in tags and layers system or making a single component that has a serializefield of an enum. This enum will act as a tag for you to identify the object type(I never needed to do this)

this just an alias, instead of typing new System.String(…), you can do new custom_type_class(…). This is also weird, because System.String already has an alias, which is called “string”

I really can’t tell what you are trying to achieve with

Either give a more concrete example or look up one of kurt dekker’s posts in the forums that explain how to learn coding and stuff

Cheers

Hi
Thanks for replying
My constant variables in the code was like:

public const string type_twall = "Unbreakable wall";//Terrain wall type messsage
//----pretend other script below-----
string objecttype = type_twall;

But however, i wanna make it be like this:

public const Type type_twall = "Unbreakable wall";//Terrain wall type messsage
//----pretend other script below-----
Type objecttype = type_twall;

Where the type class works exacly the same as the string:
It can store a string,
And when i want to get a type variable, i get the type string directly with the variable, instead of accessing properties of the variable, like objecttype.getType() (i can do this with a class, but i dont think it’s yet the best option?) or something simular.

You can use Type.GetType() and pass a string + optionally the assembly as parameters to get a type from string

I’d still recommend somehow changing your logic though, this architecture most likely smells

1 Like