⬆️ ⬇️

The task of creating sequential numeric codes for numbering messages in the source code in Visual Studio (ex. C #)

Hello



Let me share with you a small life hack that I have been successfully using for about a couple of years - creating sequential numeric codes for text messages in the source code in the process of directly editing the source code in Visual Studio:



image



I began to think about this task when several dozen messages to the user / error / exception handlers began to be "formed" in the code and it became impossible to put up with the fact that when receiving the next message in runtime it became difficult to navigate where it came from. There was a strong desire to number all messages with consecutive indices that would be displayed at the beginning of each message, but so that this sequence could be created while typing the source code using the keyboard only , on the fly (not to be distracted from the encoding process).



image



This is done as follows:

')

  1. Create enum for error codes.
  2. Special syntax for codes in enum: "_number". (at the beginning of the number there is an underscore, because enum still requires symbolic names).
  3. "Digitizer" of the format "_number" into the actual number.
  4. The magic of incrementing a numerical code on the fly.


1.2:



/// " "    public enum MCodes{ _000, _001, _002, } 


3: "Digitizer" of the format "_number" in the actual number



 static class _MCodeExtensions{ /// mini formar error message -    . public static string mfem(this MCodes mcode) { //string str = $"{nameof(rcode)} = {rcode}, {nameof(mcode)} = {mcode}"; int val = Int32.Parse(mcode.ToString().Substring(1)); string str = $"{nameof(mcode)} = {val}"; return str; } } 


4. Magic



The magic is to use the features of IntelliSense for Visual Studio:



image





In fact, these actions are performed quite quickly (slow motion):



image





Using



“Usually” underlined numbers are rarely used in the source code, so finding this number using Ctrl-F (search in the current file) or Ctrl-Shift-F (search in the entire project) will accurately indicate the location of the error.



(Of course, you can open enum, find the code, press Shift-F12, but this is the right long way from the discharge ...)



disadvantages



1. If you copy pieces of code with inserted error codes, then, naturally, the error codes will no longer be unique. To combat them, you need to periodically review the enum MCodes with a check that some code is used no more than once

image





Shortcuts F12 and Shift-F12 help a lot.



2. You can make a mistake in the dialed format and write not "_number", but something else that cannot be converted to a number. Yes, there will be an exception.



Conclusion



Numbering messages, especially about errors, sometimes provides an invaluable service in debugging and finding the reasons for the appearance of different messages. I hope that this method will help to simplify this task.



It seems that this is a minimum of body movements?



PS



This is my extremely subjective attitude to error codes, but suddenly it will help you with something. It can be used not only for incrementing error codes, but also for other sequences. Naturally, customization of the solution to your taste.



The reason for using the number format in enum in the form of "number" is that int is actually hidden behind enum, and the enum member is numbered from the beginning of the sequence (it can be assigned syntactically, but this is not done automatically through IntelliSense, and you don’t have to spend time on it I want to). And also the meaning of this member depends on the location. And if the location changes, then the number will already be different. Therefore, the value itself is ignored everywhere in the code.



A bit of theory about Enum Enums .

Source: https://habr.com/ru/post/461125/



All Articles