C # Constant Change

I inherited a small Windows-based program written in C # that uses a constant (see below). I need to change this program, so "PROPERTY_NAME" can be "tasks" and "career".

    private const string PROPERTY_NAME = "jobs";

I assume that the constant is not intended to change, so I need to change this. The line above is set once at the beginning of the class file, and then PROPERTY_NAME is used in this file.

In the main form, I would like to add two switches 1, called "tasks", and one - "career", and then change the PROPERTY_NAME file in the class file based on the selected one. Do I need to pass switch status to a method in a class file? I remember reading that I can’t just read the value of the switch from the class file.

Thanks so much for your advice.

Jane

+3
source share
4 answers

My best (and easiest) guess (I could figure out cleaner things, but it's just for speed), without seeing any other part of the code, would be to delete constand add readonly, so there PROPERTY_NAMEwill be just a simple class member variable that cannot change outside the constructor.

In the constructor of the class, take the string parameter and get the code that creates an instance of this class, passing either in the "tasks" or in the "career" (possibly coming from the selected switch) and set the PROPERTY_NAMEvariable.

EDIT:

, enum, , PROPERTY_NAME , .

+7

, , - , . - , . , , . . , , .

+4

( ) readonly. . , .

-sa

+3

You cannot make two values ​​permanent. It looks like you need to create a field that stores the current property name and use it in your form. And you can launch such a field using the switch.

0
source

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


All Articles