Home All Groups Group Topic Archive Search About

Constant - InDebugMode

Author
23 Sep 2006 4:05 AM
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

Author
23 Sep 2006 4:19 AM
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.

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


Show quoteHide quote
"Miro" <miron***@golden.net> wrote in message
news:exf65Us3GHA.1608@TK2MSFTNGP04.phx.gbl...
> 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
>
>
>
Author
23 Sep 2006 5:10 AM
Miro
Yes it did help,
and it makes sence now.

Thank you

Miro

Show quoteHide quote
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in
message news:C17A956A-F8BA-43E2-A99B-24CDC4D15095@microsoft.com...
> 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" <miron***@golden.net> wrote in message
> news:exf65Us3GHA.1608@TK2MSFTNGP04.phx.gbl...
>> 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
>>
>>
>>
>
Author
26 Sep 2006 10:14 AM
Phill W.
Miro wrote:
> 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.