0

I have the below class

 public class OracleStoredProcedures
{
    public static readonly string GET_ALL_PRODUCTS_BY_ID = $"{PRODUCT_PACKAGE_NAME}.GET_ALL_PRODUCTS_BY_ID";

    public static readonly string GET_LATEST_PRODUCTS = $"{PRODUCT_PACKAGE_NAME}.GET_LATEST_PRODUCTS";

    public static readonly string GET_ADDRESS_BY_ID = $"{ADDRESS_PACKAGE_NAME}.GET_ADDRESS_BY_ID";

    private static readonly string PRODUCT_PACKAGE_NAME = "DATABASE.PRODUCT_PKG";

    private static readonly string ADDRESS_PACKAGE_NAME = "DATABASE.ADDRESS_PKG";
}

I am having the issue when trying to call the field

var getProducts = OracleStoredProcedures.GET_LATEST_PRODUCTS;

So, I did google search and found the same issue here Order of Static Constructor/Initializers in C#

In my case, these are just packages and stored procedures, not doing any complex things. I am having multiple packages and stored procedures and I just gave two as examples.

So, what is the best way to write and call these from anywhere in the code base ?

Chatra
  • 2,989
  • 7
  • 40
  • 73
  • You said you have a “problem” and have “the issue.” Mind telling us what the issue *is*? – John Wu Apr 28 '19 at 18:17
  • 1
    Why do the last 2 fields (`PRODUCT_PACKAGE_NAME` and `ADDRESS_PACKAGE_NAME`) have to be defined as `static readonly` when they can merely set as `const` and solve your problem along the way? – haim770 Apr 28 '19 at 18:19
  • @haim770 That solves the problem I had. but can you please explain more. – Chatra Apr 28 '19 at 18:21
  • Just change the order of the fields..... declare these first:PRODUCT_PACKAGE_NAME, ADDRESS_PACKAGE_NAME..... Seems like in this scenario order matters – Jonathan Alfaro Apr 28 '19 at 18:27

1 Answers1

3

Static initializers within a class are executed in textual order. Currently the interpolated string literals are being formatted while PRODUCT_PACKAGE_NAME and ADDRESS_PACKAGE_NAME have their default values of null. So you could just rewrite this as (fixing the names to be more conventional at the same time):

public class OracleStoredProcedures
{
    private static readonly string ProductPackageName = "DATABASE.PRODUCT_PKG";    
    private static readonly string AddressPackageName = "DATABASE.ADDRESS_PKG";

    public static readonly string GetAllProductsById = $"{ProductPackageName}.GET_ALL_PRODUCTS_BY_ID";
    public static readonly string GetLatestProducts = $"{ProductPackageName}.GET_LATEST_PRODUCTS";
    public static readonly string GetAddressById = $"{AddressPackageName}.GET_ADDRESS_BY_ID";
}

Alternatively, you could make the two package names const fields, at which point they don't need to be initialized separately. In fact, all of these could be declared as const if you use string concatenation instead of interpolation:

public class OracleStoredProcedures
{
    private const string ProductPackageName = "DATABASE.PRODUCT_PKG";    
    private const string AddressPackageName = "DATABASE.ADDRESS_PKG";

    public const string GetAllProductsById = ProductPackageName + ".GET_ALL_PRODUCTS_BY_ID";
    public const string GetLatestProducts = ProductPackageName + ".GET_LATEST_PRODUCTS";
    public const string GetAddressById = AddressPackageName + ".GET_ADDRESS_BY_ID";
}

At that point, the compiler will validate that you aren't introducing a cycle between the constants.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194