How do I declare a constant variable with a value of a function call? (1 Viewer)

yamus

Member
Local time
Today, 13:37
Joined
Aug 12, 2020
Messages
81
Hello
I have a VBA module within which i need constants to be initialized with the output of a function
I got an error doing that and then i knew that constants should be initialized on run-time
So, is there any solution or approach to deal with my requirement?
Thanks in advance
 

Mike Krailo

Well-known member
Local time
Today, 08:37
Joined
Mar 28, 2020
Messages
1,030
There is no such thing as a constant variable. You have constants and you have variables. In your case, you should be using variables.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:37
Joined
May 7, 2009
Messages
19,169
can you post your function and the constant
or whichever is related to your function?
 

plog

Banishment Pending
Local time
Today, 07:37
Joined
May 11, 2011
Messages
11,611
Constants are declared (called into existence, e.g. Const MyConst As Integer) and initialized (given a value, e.g. = 42) in one motion. You can't seperate the 2 actions. So declaring a constant means initializing it. Further, constants can't be declared inside a function/sub.


So, to Mike's point, if you want to initialize something inside a function you must use a variable.
 
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Today, 08:37
Joined
May 21, 2018
Messages
8,463
Maybe a static variable is what you want?

Static Variables​

Variables do have a lifetime.
Indicates that the local variable is preserved between calls.
A static variable can be thought of as a local variable with memory.
A static variable is a local variable whose lifetime is the lifetime of the entire module and not the procedure where it is declared.
In fact static variables retain their values as long as the code module is active. There does not have to be any code running all the time.
Therefore a static variable has the scope of a local variable but the lifetime of a module level variable.
Makes a variable persistent even after the procedure has completed.
The next execution of the routine can access the previous value.
It is the lifetime of a variable that determines its existence, the scope determines its visibility.
A static variable is defined within a procedure and although it has procedure-level scope it actually has module-level lifetime
This means that you can only use the variable inside the procedure in which it is declared but it value is maintained between calls to the procedure
You can also declare a procedure as Static in which case all the variables declared inside the procedure are treated as static.
 
Last edited:

yamus

Member
Local time
Today, 13:37
Joined
Aug 12, 2020
Messages
81
Constants are declared (called into existence, e.g. Const MyConst As Integer) and initialized (given a value, e.g. = 42) in one motion. You can't seperate the 2 actions. So declaring a constant means initializing it. Further, constants can't be declared inside a function/sub.


So, to Mike's point, if you want to initialize something inside a function you must use a variable.
Here is an example of my code

private const HORZRES as Long = 1920

I want to make it something like this

private const HORZRES as Long = screenResWidth

screenResWidth is a function that returns Long output
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 08:37
Joined
May 21, 2018
Messages
8,463
As pointed out you cannot do that, and there is a reason. Constants are compiled into the code at compile time. Therefore it cannot take a variable assignment since at compile time that is unknown. This is the reason for having constants instead of just making everything a variable.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:37
Joined
May 7, 2009
Messages
19,169
from this function:
Code:
#If VBA7 Then
    Declare PtrSafe Function GetSystemMetrics32 Lib "User32" _
    Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
#Else
    Declare Function GetSystemMetrics32 Lib "User32" _
    Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
#End If
Function ScreenRes() As String
    Dim w As Long, h As Long
    w = GetSystemMetrics32(0) ' width in points
    h = GetSystemMetrics32(1) ' height in points
    ScreenRes = w & "x" & h
End Function

Public Function HorizontalRes() As Long
HorizontalRes = CLng(GetSystemMetrics32(0))
End Function

Public Function VerticalRes() As Long
VerticalRes = CLng(GetSystemMetrics32(1))
End Function

debug.print "the Horizontal Resolution is: " & HorizontalRes()
debug.pring "the vertical resolution is: " & VerticalRes()
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 07:37
Joined
Feb 28, 2001
Messages
26,996
As noted by others, you cannot do that. Here is why.

Functions require code to be running in order to define a value.

Constants are defined at a time when code CANNOT be running - because the code has yet to be completely compiled.

Use a variable or just call the function directly. You can declare variables as public or private and if you don't change them, they are EFFECTIVELY if not actually constant. Variables don't change on their own unless you have a catastrophic code failure that ends up causing a RESET to occur or unless you have sloppy code.
 

Users who are viewing this thread

Top Bottom