Sign in to follow this  
Followers 0
erebus

Variable declaration

7 posts in this topic

I don't know if I use it right but check the following code please:

$useme = StringSplit($useme, "-")
If @error = 1 Then
   MsgBox(0, $app & " " & $ver, "Invalid String")
   Exit
EndIf

If Not IsDeclared("useme[5]") Then
   MsgBox(0, $app & " " & $ver, "Invalid String")
   Exit
EndIf

The idea is to check if the fifth splitted string is declared (so as to be sure that the string is of the correct form). What is wrong?

Share this post


Link to post
Share on other sites



This should work:

$useme = StringSplit($useme, "-")
If @error = 1 Then
  MsgBox(0, $app & " " & $ver, "Invalid String")
  Exit
EndIf

If Not IsDeclared($useme[5]) Then
  MsgBox(0, $app & " " & $ver, "Invalid String")
  Exit
EndIf

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Unfortunately not...

Tried with:

If Not IsDeclared($useme[5]) Then
 MsgBox(0, $app & " " & $ver, "Invalid String")
 MsgBox(0, "", $useme[5])
 Exit
EndIf

...to verify.

Edited by erebus

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I don't understand what you want to accomplish.

If you see the message boxes, it means that the variable name stored in $useme[5], is not declared.

Do you want to declare the variable with the name stored in $useme[5]?

If you say "yes" use this:

$useme = StringSplit($useme, "-")
If @error = 1 Then
  MsgBox(0, $app & " " & $ver, "Failed to split the string!")
  Exit
EndIf

If Not IsDeclared($useme[5]) Then
  MsgBox(0, $app & " " & $ver, "Variable is not declared." & @CRLF & _
  "Just wanted to let you know.")
  Assign($useme[5], "");<-- This function will only work if you use the unstable version
  Exit
EndIf
Edited by SlimShady

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Ok let me explain it better.

- I want to split a string into 5 vars.

- If the fifth (AND not the 6th) var exists means that the input string format is correct (xxx-xxx-xxx-xxx-xxx).

If I put a condition like "If $useme[5] = "" Then...", it leads to an error when the $useme[5] variable does not exist (and the initial string format is incorrect). That is I want the IsDeclared function.

However when I use the "If not IsDeclared($useme[5])" condition, it always considers the $useme[5] as not declared (even if it is created from the array and has a string assigned).

I hope this is clear now :/

Thank you for your time..

P.S. I _do_ use the latest unstable.

Edited by erebus

Share this post


Link to post
Share on other sites

Ok let me explain it better.

- I want to split a string into 5 vars.

- If the fifth (AND not the 6th) var exists means that the input string format is correct (xxx-xxx-xxx-xxx-xxx).

If I put a condition like "If $useme[5] = "" Then...", it leads to an error when the $useme[5] variable does not exist (and the initial string format is incorrect). That is I want the IsDeclared function.

However when I use the "If not IsDeclared($useme[5])" condition, it always considers the $useme[5] as not declared (even if it is created from the array and has a string assigned).

I hope this is clear now :/

Thank you for your time..

P.S. I _do_ use the latest unstable.

Just check the count that StringSplit sets up ( element [0] ) - If the count is less than 5 then the 5th bit is missing.

Share this post


Link to post
Share on other sites

Just check the count that StringSplit sets up ( element [0] ) - If the count is less than 5 then the 5th bit is missing.

Doh... What If I tell you that I had completely forgotten element's[0] feature? Thanks a lot Jon, that was exactly what I needed.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0