Constant - InDebugMode

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Miro

    #1

    Constant - InDebugMode

    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.Diagnost ics.Debugger.Is Attached() 'Gets error

    The error I get is that it underlines the
    System.Diagnost ics.Debugger.Is Attached() 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



  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Constant - InDebugMode

    Miro,
    Have you tried ReadOnly?
    Private Readonly boolInDebugMode As Boolean =
    System.Diagnost ics.Debugger.Is Attached() '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" <mironagy@golde n.netwrote in message
    news:exf65Us3GH A.1608@TK2MSFTN GP04.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.Diagnost ics.Debugger.Is Attached() 'Gets error
    >
    The error I get is that it underlines the
    System.Diagnost ics.Debugger.Is Attached() 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
    >
    >
    >

    Comment

    • Miro

      #3
      Re: Constant - InDebugMode - solved

      Yes it did help,
      and it makes sence now.

      Thank you

      Miro

      "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @tsbradley.netw rote in
      message news:C17A956A-F8BA-43E2-A99B-24CDC4D15095@mi crosoft.com...
      Miro,
      Have you tried ReadOnly?
      >
      > Private Readonly boolInDebugMode As Boolean =
      >System.Diagnos tics.Debugger.I sAttached() '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" <mironagy@golde n.netwrote in message
      news:exf65Us3GH A.1608@TK2MSFTN GP04.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.Diagnos tics.Debugger.I sAttached() 'Gets error
      >>
      >The error I get is that it underlines the
      >System.Diagnos tics.Debugger.I sAttached() 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
      >>
      >>
      >>
      >

      Comment

      • Phill W.

        #4
        Re: Constant - InDebugMode

        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.Diagnost ics.Debugger.Is Attached() '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.InDeb ugMode 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.Diagnost ics.Debugger.Is Attached
        End Get
        End Property

        End Class

        HTH,
        Phill W.

        Comment

        Working...