Unused constants warnings.

E

ESPNSTI

Hi,

Is there a way for the compiler to warn about constants that are not used?
For example in the code below, could the compiler (with some sort of
attribute setting or something like that) throw an error or warning stating
that the DBParameters.Column2 isnt used?

Thanks,
Erik

____________________________________________________________________________
__________

public class DBParameters
{
public const string Column1 = "Column1";
public const string Column2 = "Column2"; // Throw a compiler error /
warning stating this isn't used.
public const string Column3 = "Column3";
}

public class Entity
{

int column1;
int column2;
int column3;

[DBParameter(Name = DBParameters.Column1)]
public int Column1
{
get { return m_column1; }
set { m_column1 = value; }
}

public int Column2
{
get { return m_column2; }
set { m_column2 = value; }
}

[DBParameter(Name = DBParameters.Column3)]
public int Column3
{
get { return m_column3; }
set { m_column3 = value; }
}
}
 
B

Bruce Wood

No, there is no way the compiler can tell that the constant is not
used, because you've declared it "public." There is no way for the
compiler to know that some other code in some other assembly isn't
referencing this assembly and using that constant.

That said, it surprised me to find that the compiler doesn't complain
if you declare a private constant and then don't use it. It complains
for private fields, but not constants. How odd.
 
M

Michael C

Bruce Wood said:
No, there is no way the compiler can tell that the constant is not
used, because you've declared it "public." There is no way for the
compiler to know that some other code in some other assembly isn't
referencing this assembly and using that constant.

That said, it surprised me to find that the compiler doesn't complain
if you declare a private constant and then don't use it. It complains
for private fields, but not constants. How odd.

Makes sense, it would be a pain if it warned about unused constants.
 
B

Bruce Wood

Makes sense, it would be a pain if it warned about unused constants.

Why? If you're not using something, why put it in the code? Remember,
we're talking about situations in which the constant really is unused:
it's private, so no other assembly and no other class can refer to it,
and the owning class doesn't use it. The compiler should issue a
warning so that you can delete the thing... it's not good for anything
other than confusing people reading the code. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top