cristianbuse / VBA-StateLossCallback

A class that allows a callback when state is lost
MIT License
12 stars 4 forks source link

Declare Enum LongLong for more compact code? #2

Closed SanbiVN closed 1 year ago

SanbiVN commented 1 year ago

hi @cristianbuse Can you explain me, why not declare Enum LongLong compatibility. To write more compact code as below. Is there an overhead during code compilation?

Code in your class module:

#If Win64 Then
Public Sub InitByAddress(ByVal memAddress As LongLong, Optional ByVal argText As String)
#Else
Public Sub InitByAddress(ByVal memAddress As Long, Optional ByVal argText As String)
#End If

End Sub

Why can't it be like this:

#If VBA7 = 0 Then
    Private Enum LongLong
       [_]
    End Enum
#End If

Public Sub InitByAddress(ByVal memAddress As LongLong, Optional ByVal argText As String)

End Sub

I don't mean just in your project, but maybe bigger projects!

cristianbuse commented 1 year ago

Hi @SanbiVN ,

I was fine using the:

#If VBA7 = 0 Then
    Private Enum LongPtr
       [_]
    End Enum
#End If

trick because I only want to avoid many #If declarations for VBA6 because my target is VBA7 to be honest. Moreover, LongPtr already suggests that it's Long on x32. However, I dislike the fact that under the Locals window (while debugging) you still see the data type as LongPtr when using such an enum declaration. Again, not a big deal for me as I target VBA7.

In VBA7 I am 100% against showing a data type called LongLong which is actually Long. It's ambiguous, not clean and most of all misleading.

SanbiVN commented 1 year ago

Hi @Cristianbuse!

I understand the goal of declaring Enum LongPtr and LongLong, is to make the declaration in code more concise, but cross-platform compatible. As you said, in the code, the declarations are LongLong so it would make more sense to replace all of them to LongPtr, right?

SanbiVN commented 1 year ago

Hi @Cristianbuse! I'm so sorry, I carelessly forgot to translate into your language

cristianbuse commented 1 year ago

Hi @SanbiVN ,

As you said, in the code, the declarations are LongLong so it would make more sense to replace all of them to LongPtr, right?

Yes and no. 😄. Sometimes, you really want to directly check for x64 and write code that is specific to x64 only. In that case there is no need to go through the extra step of using LongPtr - example here.

In your example (the InitByAddress method in this repo) you are correct, I could just use LongPtr 😄. But would still not declare a LongLong enum.

However, since I declared the LongPtr enum as private, I cannot use it for the InitByAddress method wich is Public. So, I could have used what I already have, which is this:

#If Win64 Then
Public Sub InitByAddress(ByVal memAddress As LongLong, Optional ByVal argText As String)
#Else
Public Sub InitByAddress(ByVal memAddress As Long, Optional ByVal argText As String)
#End If

End Sub

or simply this:

#If VBA7 Then
Public Sub InitByAddress(ByVal memAddress As LongPtr, Optional ByVal argText As String)
#Else
Public Sub InitByAddress(ByVal memAddress As Long, Optional ByVal argText As String)
#End If

End Sub

They would both work fine. Thanks for your feedback.

SanbiVN commented 1 year ago

Hi @cristianbuse! I got it. You explained the problem very clearly