Results 1 to 14 of 14

Thread: [RESOLVED] What is the best way to use a Constant

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2020
    Posts
    35

    Resolved [RESOLVED] What is the best way to use a Constant

    Hi Gys
    What if I want to use a Constant the right way

    should I use it Globally

    Code:
     Dim someValue as String = "sValue"
     Public someValue as String = "sValue"
    Or
    Inside a class

    Code:
    Property Get someValue(): someValue = "sValue": End Property
    Or
    Inside a Class

    Code:
    Enum X
    O200 = 200
    O201
    O202
    End Enum
    
    Public Function C(X As Integer) As String
    C = "0" & X
    End Function
    
    
    Dim CodeCg As Class1
    Debug.Print CodeCg.C(X.O200)

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: What is the best way to use a Constant

    Well it really depends on how you plan to use it within your program.
    Normally they would be defined in a module and be public but always defined using the CONST keyword.

    Code:
    Public Const SomeString As String = "SomeValue"

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2020
    Posts
    35

    Re: What is the best way to use a Constant

    Quote Originally Posted by DataMiser View Post
    Well it really depends on how you plan to use it within your program.
    Normally they would be defined in a module and be public but always defined using the CONST keyword.

    Code:
    Public Const SomeString As String = "SomeValue"
    Is it safe to use it globally ..

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: What is the best way to use a Constant

    HKcom,

    You're really not asking about Constants (Const declaration) as much as you're asking about scoping. And scoping applies to both variables and constants.

    The general rule-of-thumb is to scope everything as narrowly as possible. However, constants are a bit different in this respect.

    For me, if it's some constant I'm only going to use once, I'll typically declare it (and scope it) inside the actual procedure where it's being used. Also, sometimes I'll have a module that's dedicated to a certain group of procedures (for instance, my procedures related to monitors). In this case, I'll probably have constants declared at the module level (but declared with the Dim or Private keywords).

    And, then there are totally generic constants, many that you get with the various libraries included within your program. For instance, the vbCritical, vbOkCancel, etcetera MsgBox constants come to mind. Those come from the VB6 library, but you may have your own constants that are best viewed from a program-wide scope.

    So, the answer is, it all depends on what you're trying to do.

    Hope That Helps,
    Elroy

    EDIT: Two more things: 1) sure, it's absolutely safe to use (project wide) global constants, and 2) VB6 doesn't allow variable declaration and assignment in one statement ... I wish it did, but it doesn't. It only allows that with true constants.
    Last edited by Elroy; Nov 19th, 2020 at 10:03 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: What is the best way to use a Constant

    Global constants are like wearing socks with sandals. You can do it, but there are very few cases where it is appropriate.

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2020
    Posts
    35

    Re: What is the best way to use a Constant

    Quote Originally Posted by dilettante View Post
    Global constants are like wearing socks with sandals. You can do it, but there are very few cases where it is appropriate.
    From a hacker's point of view ... what will be your approach

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: What is the best way to use a Constant

    I think he'll wear shoes.

    How does this have anything to do with a hackers point of view? Hacker has several possible meanings when it comes to programming, but only one of them seems to have anything to do with the use of global variables, and it is probably the least used definition of the word: One who programs only casually and without much skill. If that's the definition you are using, then I'd say that you probably use global variables in EVERY situation.
    My usual boring signature: Nothing

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: What is the best way to use a Constant

    Ideally you give data, including constants, only as much exposure as necessary. Since things aren't ideal we often end up over-exposing data, but that doesn't imply this should be a programmer's default behavior.

    There isn't any simple rule to scoping data because we have limited tools. In general just keep it as local as possible.

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2020
    Posts
    35

    Re: What is the best way to use a Constant

    Quote Originally Posted by dilettante View Post
    Ideally you give data, including constants, only as much exposure as necessary. Since things aren't ideal we often end up over-exposing data, but that doesn't imply this should be a programmer's default behavior.

    There isn't any simple rule to scoping data because we have limited tools. In general just keep it as local as possible.
    Totally agree
    Personally, I hate global variables and constants .. I might be wrong .. I almost always use properties in classes to return them ..

  10. #10
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: What is the best way to use a Constant

    is this a question about Constants or encryption?
    a hacker, depending how good he/she is, will be able to hack almost anything.
    as long your program is doing anything in "memory" the hacker can monitor it.

    if this constants is like a password, its a bad idea to add it as constants.
    better use a complicated function that will "create" this "password" mathematically and only when needed. thats the minimum u need, and often the password gets leaked before anyone try to hack it anyway.

    for my needs, I use an encrypted data, that I pre-encrypt with a tool that is never exposed outside my computer.
    this data is decrypted by the main program, with a non-constant/non-string and dynamical variables to decrypt it. but, since its available in memory, he hacker could do a memory dump here.

  11. #11

    Thread Starter
    Member
    Join Date
    Nov 2020
    Posts
    35

    Re: What is the best way to use a Constant

    Quote Originally Posted by baka View Post
    is this a question about Constants or encryption?
    a hacker, depending how good he/she is, will be able to hack almost anything.
    as long your program is doing anything in "memory" the hacker can monitor it.

    if this constants is like a password, its a bad idea to add it as constants.
    better use a complicated function that will "create" this "password" mathematically and only when needed. thats the minimum u need, and often the password gets leaked before anyone try to hack it anyway.

    for my needs, I use an encrypted data, that I pre-encrypt with a tool that is never exposed outside my computer.
    this data is decrypted by the main program, with a non-constant/non-string and dynamical variables to decrypt it. but, since its available in memory, he hacker could do a memory dump here.
    Would you please give us some code as example..

  12. #12
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: What is the best way to use a Constant

    Quote Originally Posted by HKcom View Post
    Is it safe to use it globally ..
    Well in general yes but again it depends on the project and how you need to use it.
    In small projects there really isn't an issue. In larger ones there is more of an issue if for no reason other than wasted memory.
    In general you should define variables, constants, objects in the scope they will be used and only make them global when they need to be global.

  13. #13
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: What is the best way to use a Constant

    Dim someValue as String = "sValue"
    Public someValue as String = "sValue"
    this would not be valid in vb6 anyway, give some error
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  14. #14

    Thread Starter
    Member
    Join Date
    Nov 2020
    Posts
    35

    Re: What is the best way to use a Constant

    Quote Originally Posted by westconn1 View Post
    this would not be valid in vb6 anyway, give some error
    Sorry Guys
    I forgot the keyword Const:

    Code:
    Const someValue As String = "sValue"
    thank you all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width