Public constants

P

Paolo

From a newbie,

As I understand it having public constants or variables is not a good
idea. So I coded a function instead -- I have a set of hexadecimals
for use in an API function. Instead of using the hexadecimal value in
my calling function, I use a more understandable string to refer to
them. My code is below. Note I could have used a public constant to
refer to the hexadecimal value, but I used a function instead. Is
there a better way other than using a public constant or my not so
elegant solution below? Any criticism regarding the code are gladly
accepted, as I said I am just starting out!

Thanks in advance,
Paolo

E-mail address is not checked. Send e-mail to
(e-mail address removed), eliminating the NOSPAM.



Public Declare Function MessageBeep _
Lib "user32" (ByVal wType As Long) As Long

Public Function DeclareHex(MB As String) As Long
On Error GoTo ErrHandler
Select Case MB
Case IconAsterisk, IconInformation
DeclareHex = &H40&
Case IconExclamation
DeclareHex = &H30&
Case IconQuersion
DeclareHex = &H20&
Case IconHand
DeclareHex = &H20&
Case Default
DeclareHex = &HFF
Case MotherboardSpeaker
DeclareHex = &HFFFFFF
End Select
End Function

Private Sub ElementID_KeyPress(KeyAscii As Integer)
On Error GoTo ErrHandler
Call MessageBeep(DeclareHex(Default))
Exit_ElementID_KeyPress:
Exit Sub
ErrHandler:
MsgBox "ElementID_KeyPress(KeyAscii As Integer)." & vbCrLf & vbCrLf
& _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear
Resume Exit_ElementID_KeyPress
End Sub
 
P

Paolo

Oops,

Forgot to put "" around my strings. Anyway, that aside, any comments?

E-mail address is not checked. Send e-mail to
(e-mail address removed), eliminating the NOSPAM.
 
M

Marshall Barton

Paolo said:
As I understand it having public constants or variables is not a good
idea. So I coded a function instead -- I have a set of hexadecimals
for use in an API function. Instead of using the hexadecimal value in
my calling function, I use a more understandable string to refer to
them. My code is below. Note I could have used a public constant to
refer to the hexadecimal value, but I used a function instead. Is
there a better way other than using a public constant or my not so
elegant solution below? Any criticism regarding the code are gladly
accepted, as I said I am just starting out!


Public Declare Function MessageBeep _
Lib "user32" (ByVal wType As Long) As Long

Public Function DeclareHex(MB As String) As Long
On Error GoTo ErrHandler
Select Case MB
Case IconAsterisk, IconInformation
DeclareHex = &H40&
Case IconExclamation
DeclareHex = &H30&
Case IconQuersion
DeclareHex = &H20&
Case IconHand
DeclareHex = &H20&
Case Default
DeclareHex = &HFF
Case MotherboardSpeaker
DeclareHex = &HFFFFFF
End Select
End Function

Private Sub ElementID_KeyPress(KeyAscii As Integer)
On Error GoTo ErrHandler
Call MessageBeep(DeclareHex(Default))
Exit_ElementID_KeyPress:
Exit Sub
ErrHandler:
MsgBox "ElementID_KeyPress(KeyAscii As Integer)." & vbCrLf & vbCrLf
& _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear
Resume Exit_ElementID_KeyPress
End Sub


As long as you are only using the API calls in VBA
procedures (and it's hard to imagine why you want to do
anything else), declaring the values as Public Const is
definitely the way to go. It's only when you move into
other environments (e.g. SQL) that the public
constants/variables are only accessible through functions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top