Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a getter for a const?

Tags:

c#

properties

Just curious, is there a way to have a getter for a constant variable? I have a sort of internal version number to ensure that two versions of a library are still speaking the same language, but I'd like the programmer to be able to check what version they're using. Right now I use:

 private const Int16 protocol_version = 1;
 public Int16 ProtocolVersion { get { return protocol_version; } }

But I'd prefer to do it with just the const if there's a way.

like image 221
cost Avatar asked Apr 28 '12 09:04

cost


People also ask

Does it matter where you put const?

To start you probably know that const can be used to make either an object's data or a pointer not modifiable or both. However you can also use the syntax: Object const *obj; // same as const Object* obj; The only thing that seems to matter is which side of the asterisk you put the const keyword.

When should you use the const keyword?

The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.

Does a getter return reference?

The reference-getter A reference-getter is a method that directly returns the reference to an attribute to access and edit it. This is peculiarly useful on object attributes so you can call non-const methods directly on them.

What is a const method in C++?

The const member functions are the functions which are declared as constant in the program. The object called by these functions cannot be modified. It is recommended to use const keyword so that accidental changes to object are avoided. A const member function can be called by any type of object.


2 Answers

You could declare a property with only a get accessor (without even declaring the set accessor, not even private):

private const Int16 protocol_version = 1;
public Int16 ProtocolVersion { 
    get { return protocol_version; } 
}

This is not the same as defining a constant only: the constant would be resolved at compile time, so if you update the library without recompiling the dependent program, the program would still see the "old" value. Consider this example:

// The class library
using System;

namespace MyClassLibrary {
    public class X {
        public const Int16 protocol_version = 1;
        public Int16 ProtocolVersion { get { return protocol_version; } }
    }
}

// The program
using System;
using MyClassLibrary;

class Program {
    static void Main(string[] args) {
        X x = new X();
        Console.WriteLine("Constant : {0}", X.protocol_version);
        Console.WriteLine("Getter: {0}", x.ProtocolVersion);
    }
}

Now, compile the first time and execute the program. You will see

Constant : 1
Getter : 1

Then, modify protocol_version to 2, and recompile the class library only, without recompiling the program, then put the new class library in the program folder and execute it. You will see:

Constant : 1
Getter : 2

The fact is that if it's just a constant, the value is replaced at compile time.

I think that what you are actually looking for is a static readonly variable: in that way, you will avoid the compile-time const replacement, and the variable will not be modifiable after initialization:

public static readonly Int16 protocol_version = 1;
like image 153
Paolo Tedesco Avatar answered Oct 07 '22 13:10

Paolo Tedesco


You have to keep in mind the reason for the existance of getters/setters. It is to control access to an encapsulated variable, specifically to control how a variable is changed and who can change it. Since a const is set only once and remains read-only on runtime there is no reason to create a property for it. Setting the constant to public is completely acceptable since it is not a private variable that needs to be protected.

If you really... really want to make it a property then just define it as a readonly property, skip the setter entirely:

public Int16 ProtocolVersion { get { return protocol_version; } }

But just so we are clear, I would say normally you would have public constants with the same coding style as properties:

public const Int16 ProtocolVersion = 1
like image 45
edvaldig Avatar answered Oct 07 '22 13:10

edvaldig