Hi!
I am working on a function that would validate dates. I am aware of IsDate(), but it doesn't really suit my use case as it only accepts Date types and the two inbuilt conversion functions DateSerial and DateValue, will convert invalid dates into valid dates, by addding the excess months and days into years ( for example 01.13.2020 becomes 01.01.2021). Therefore i am writing my own implementation.
As part of this i have a funciton that strips the string into day, month and year and converts them to integers:
Function formatDate(dateString As String) As Integer()
Dim day As String
Dim month As String
Dim year As String
day = Left(dateString, 2)
month = Mid(dateString, 4, 2)
year = Right(dateString, 4)
Dim dateArr As Integer(2)
dateArr(0) = cInt(day)
dateArr(1) = cInt(month)
dateArr(2) = cInt(year)
formatDate = dateArr
End Function
This doesn't compile since the paranthesis after integer in the function declaration is seen as a syntax error. As far as i can tell the language browser doesn't mention anything about how to return arrays in functions. The way to do it in VBA would be as i tried in the function above, but I guess IDEAscript is based on an earlier version of VBA that doesn't support this behaviour. So is returning arrays in functions even possible in IDEAscript? I think I could solve my issue by declaring dateArr as a global variable, but I think returning an array would be cleaner, if i ever wanted to use my validateDate script as a custom function. I am also just plainly curious about the matter, because I imagine returning arrays could be useful in other scenarios.
huslatun
Offline
Last seen: 2 months 4 weeks ago
Joined: 07/05/2022 - 03:01
Seems like using the global keyword results in a syntax error wherever i use it. ( i have tried all these different scenarios one by one)
Dim arr(2) As Integer ' does not result in a syntax error
Sub Main
Global Const int1 = 2 ' does result in a syntax error
Global arr1(2) As Integer ' does result in a syntax error
Call test
End Sub
Function test
Global int2 As Integer ' does result in a syntax error
Global arr2(1) As Integer ' does result in a syntax error
End Function
IDEA help has a section on creating global arrays, where they use the Global keyword to create an array inside a procedure. However, I am only able to create a global array using Dim outside the procedure level
Hi huslatun,
In your scenario you can't declare global variables within function and sub. The need to be declared before the Sub Main function. This is why you are getting the syntax error. You can only declare local variables within functions.
Hi brian,
Then I am confused as to when the Global keyword should be used. To my knowledge, declaring variables above sub Main makes them into global variables. As such the global keyword isn't needed in that scenario. When should 'Global' be used?
The Global keyword is optional, I personally never use it. You can have it so that it is evident that the variable is global or not. Not sure if it can be used anywhere else.
Hi Hustlam,
I have taken your code and updated so it works as you want:
I moved your array definition to the sub main as you want to have the array defintion from the calling function or sub routine. I added the array variable to the function call and defined it as part of the formatDate parameters. I also added an s to your variables as Day, Month and Year and VBA keywords are can't be used as a variable. That is why they were gold in the editor. Hopefully this is working as expected.
Thank you brian,
Moving the array definition to sub Main is a better solution than mine (having the array as a global variable). Does this mean that arrays cannot be returned by functions? If it does, just passing them in as arguments is a neat workaround, that should work in most use cases.
Sorry about the variable names being in conflict with VBA keywords, in the original script they were named in a different languages, and when i posted here i translated them to english so it would be easier to understand.
I think arrays can but the array is so limited in the IDEAScripting language I usually try to use them the least possible in my scripts.