-
Nov 18th, 2020, 11:39 PM
#1
Thread Starter
Member
[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)
-
Nov 19th, 2020, 07:56 AM
#2
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"
-
Nov 19th, 2020, 08:32 AM
#3
Thread Starter
Member
Re: What is the best way to use a Constant
Originally Posted by DataMiser
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 ..
-
Nov 19th, 2020, 10:00 AM
#4
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.
-
Nov 19th, 2020, 10:19 AM
#5
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.
-
Nov 19th, 2020, 10:55 AM
#6
Thread Starter
Member
Re: What is the best way to use a Constant
Originally Posted by dilettante
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
-
Nov 19th, 2020, 11:20 AM
#7
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
-
Nov 19th, 2020, 11:27 AM
#8
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.
-
Nov 19th, 2020, 11:48 AM
#9
Thread Starter
Member
Re: What is the best way to use a Constant
Originally Posted by dilettante
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 ..
-
Nov 19th, 2020, 11:48 AM
#10
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.
-
Nov 19th, 2020, 11:53 AM
#11
Thread Starter
Member
Re: What is the best way to use a Constant
Originally Posted by baka
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..
-
Nov 19th, 2020, 02:46 PM
#12
Re: What is the best way to use a Constant
Originally Posted by HKcom
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.
-
Nov 20th, 2020, 03:12 AM
#13
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
-
Nov 20th, 2020, 04:07 AM
#14
Thread Starter
Member
Re: What is the best way to use a Constant
Originally Posted by westconn1
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|