-1

We have a system that manages generic physical resources. There are over 500 individual resources. The system is used for many different things and to make the software easier to write we use aliases.

For example, a physical resource TG67I9 is given an alias of "RightDoor". When code is written RightDoor is used instead of TG67I9 making the code more readable. This alias list is loaded as a text file with references to resources and their aliases. This system uses literally hundreds of different alias lists to reference the same physical resources.

This type of setup has two major shortcomings. First, when resources are called using their aliases, they are passed in as strings. Door.Open("RightDoor") for example. This does not give any tooltips or smart anything making the code more difficult to write. It basically requires constantly referencing the alias list. Is it RightDoor or Right_Door or right-door or... you get the idea. The second is that there is no validation of parameters until execution. All the compiler knows is that a string is passed in and then it's happy. Only when the code is run, the function tries to access the resource through its alias and fails because it can't find right-door because it's supposed to be RightDoor. An error is displayed. This requires tedious debugging and running the code over and over to weed out any bad aliases.

Is there a better way to do this? Such that an alias list can be made with a cross-reference of physical resources to their alias names and after the list is made that tooltips could appear suggesting resources. (Assume that a new system could be written from scratch)

I'm using the latest .NET with VisualStudio 2017 and C# to write the code.

vini_i
  • 325
  • 1
  • 7
  • 14
  • Does this alias list need to loaded at runtime? If not, you could generate an enum definition from the list at or before compile-time. – Blorgbeard Nov 08 '17 at 01:05
  • You could make 2 enumerations that map to each other's values and just cast between the two? I don't know enough about your situation to know if that's actually a good suggestion. – Matt Gregory Nov 08 '17 at 01:08
  • With T4 you can generate code. f.i. generate for each alias from text file a property and you can use RightDoor.Open() – Sir Rufo Nov 08 '17 at 01:14
  • is TG67I9 a string "TG67I9" or something else? – Slai Nov 08 '17 at 01:20
  • @Slai Yes TG67I9 is a string. It is more or less the address of the resource. The alias file the contains many lines like TG67I9->RightDoor. They don't have to be any particular order or even have to be complete. If you only need to use three resources then write three lines. If you need 300 then write 300 lines. – vini_i Nov 08 '17 at 01:24
  • @Blorgbeard There is a main program that governs operation. The alias list is handed to it before running. When the custom code makes a call to Door.Open("RightDoor") the call is passed into the main software, that software using the alias list internally translates RightDoor to TG67I9. Once it knows the resource, it then acts accordingly to the function call. – vini_i Nov 08 '17 at 01:32

2 Answers2

1

The simplest approach is most likely a "string enum":

public class Resources {
    public const string 
        LeftDoor  = "TG67I8", 
        RightDoor = "TG67I9";
}

Sample use:

Door.Open(Resources.RightDoor);

Hovering over .RightDoor in VS shows a tooltip (constant) string Resources.RightDoor = "TG67I9"
Right-clicking .RightDoor and selecting Find All References will show where the variable is used.


Another option can be adding the strings in the Resources section of the Project Properties, and then:

using YourProjectNameSpace.Properties;
...
Door.Open(Resources.RightDoor);

That is a bit slower, because the resource(s) are retrieved at run-time, but allows to load the resources from a custom external file separate from the executable.

Slai
  • 22,144
  • 5
  • 45
  • 53
  • VB.Net version https://stackoverflow.com/questions/12312325/define-string-enum-in-vb-net/16200092#16200092 – Slai Nov 08 '17 at 03:02
  • With a T4 template you can generate the Resources class from the alias text file – Sir Rufo Nov 08 '17 at 09:10
1

Use a static class with constants. I have done the same many times and still do. Plus .NET does this as well.

public static class PhysicalResources
{
    public const string One = "Uno";
    public const string Two = "Deux";
    // ...
}
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64