Constant - InDebugMode

M

Miro

Instead of having Public Variables all over my code, I have decided to make
a class called "CONSTANT"
and move them all into it.

Public Class Constants
Private Const strMiro = "MIRO" ' This example Works
Private Const boolInDebugMode As Boolean =
System.Diagnostics.Debugger.IsAttached() 'Gets error

The error I get is that it underlines the
System.Diagnostics.Debugger.IsAttached() with a blue line and says
"constant expression is required". I cant seem to find how to fix this in
google nor msdn.

I have that variable, cause i want certain things to happen when I am
running my app thru the debugger vs, running
the exe when it is in "release" mode. So this way I can do a quick If Else
Endif statement to do certain things.
-Unless there is an easier way ? :)

Thanks,

Miro
 
J

Jay B. Harlow [MVP - Outlook]

Miro,
Have you tried ReadOnly?
Private Readonly boolInDebugMode As Boolean =
System.Diagnostics.Debugger.IsAttached() 'Gets error

A Const is a value that is known at compile time.

Readonly is like a constant in that the value itself cannot change, however
it is different in that it value is computed at runtime.
 
M

Miro

Yes it did help,
and it makes sence now.

Thank you

Miro

Jay B. Harlow said:
Miro,
Have you tried ReadOnly?
Private Readonly boolInDebugMode As Boolean =
System.Diagnostics.Debugger.IsAttached() 'Gets error

A Const is a value that is known at compile time.

Readonly is like a constant in that the value itself cannot change,
however it is different in that it value is computed at runtime.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Miro said:
Instead of having Public Variables all over my code, I have decided to
make a class called "CONSTANT"
and move them all into it.

Public Class Constants
Private Const strMiro = "MIRO" ' This example Works
Private Const boolInDebugMode As Boolean =
System.Diagnostics.Debugger.IsAttached() 'Gets error

The error I get is that it underlines the
System.Diagnostics.Debugger.IsAttached() with a blue line and says
"constant expression is required". I cant seem to find how to fix this
in google nor msdn.

I have that variable, cause i want certain things to happen when I am
running my app thru the debugger vs, running
the exe when it is in "release" mode. So this way I can do a quick If
Else Endif statement to do certain things.
-Unless there is an easier way ? :)

Thanks,

Miro
 
P

Phill W.

Miro said:
Instead of having Public Variables all over my code, I have decided to make
a class called "CONSTANT" and move them all into it.

Public Class Constants
Private Const strMiro = "MIRO" ' This example Works
Private Const boolInDebugMode As Boolean =
System.Diagnostics.Debugger.IsAttached() 'Gets error

Firstly, I don't think the above will achieve what you want.
The class is Public and accessible throughout your code, /but/ the
Constants /within/ that class are Private and accessible only within
that Class. Make them Public.

Secondly, I would suggest either using a Module to contain all of these,
or make your "Constants" Shared (that's probably implicit, but see
below). This means you don't need an /instance/ of the Constants class
anywhere (or pass one around); you can just code

If Constants.InDebugMode Then

Lastly, I would recommend using ReadOnly Properties over Constants.
"Constants" have a nasty habit of /changing/ over time and, by using
properties, you remove the need to recompile everything that uses that
"Constant".

So, from your code, you wind up with

Public Class Constants

Public Shared ReadOnly Property Miro() As String
Return "MIRO"
End Property

Public Shared ReadOnly Property InDebugMode() As Boolean
Get
Return System.Diagnostics.Debugger.IsAttached
End Get
End Property

End Class

HTH,
Phill W.
 

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