# Check for string value of global variable

**URL:** https://resource.dopus.com/t/check-for-string-value-of-global-variable/28929
**Category:** Help & Support
**Created:** [May 23, 2018, 12:46pm UTC](https://resource.dopus.com/t/check-for-string-value-of-global-variable/28929 "2018-05-23T12:46:57Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![kanu](https://resource.dopus.com/letter_avatar_proxy/v4/letter/k/48db29/32.png) [@kanu](https://resource.dopus.com/u/kanu)
#### Post date: [May 23, 2018, 12:46pm UTC](https://resource.dopus.com/t/check-for-string-value-of-global-variable/28929/1 "2018-05-23T12:46:57Z")

</div>

Hello,  
I have a global variable that I assigned it a string value by using the command  
@set $glob:MyGlob="TheValue"  
What is the right command to check if MyGlob="WheteverValue" in a VBS script?

Thank you

---

<div class="post-metadata">

### Author: ![Jon](https://resource.dopus.com/user_avatar/resource.dopus.com/jon/32/12713_2.png) [@Jon](https://resource.dopus.com/u/Jon)
#### Post date: [May 23, 2018, 1:01pm UTC](https://resource.dopus.com/t/check-for-string-value-of-global-variable/28929/2 "2018-05-23T13:01:28Z")

</div>

`if DOpus.vars.Get("MyGlob") = "TheValue" Then ...`

---

<div class="post-metadata">

### Author: ![kanu](https://resource.dopus.com/letter_avatar_proxy/v4/letter/k/48db29/32.png) [@kanu](https://resource.dopus.com/u/kanu)
#### Post date: [May 23, 2018, 1:22pm UTC](https://resource.dopus.com/t/check-for-string-value-of-global-variable/28929/3 "2018-05-23T13:22:10Z")

</div>

Thank you Jon but I receive an error:  
Error at line 3, position 2  
Invalid procedure call or argument: 'DOpus.vars.Get' (0x800a0005)

This the code that I am using in a button

```vbscript
Option Explicit
Function OnClick(ByRef clickData)
	if DOpus.vars.Get("MyGlob") = "TheValue" Then 
	  dopus.output "HaveIt"
	End If
End Function

```

---

<div class="post-metadata">

### Author: ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)
#### Post date: [May 23, 2018, 1:33pm UTC](https://resource.dopus.com/t/check-for-string-value-of-global-variable/28929/4 "2018-05-23T13:33:07Z")

</div>

Is the variable actually set? You'll get an error if you try to use `Get` on a variable that doesn't exist.

You can test if it exists first to avoid the error:

```vbscript
Option Explicit

Function OnClick(ByRef clickData)

	If DOpus.vars.Exists("MyGlob") Then
		If DOpus.vars.Get("MyGlob") = "TheValue" Then
			DOpus.Output "MyGlob = ""TheValue"""
		Else
			DOpus.Output "MyGlob <> ""TheValue"". MyGlob = """ & DOpus.vars.Get("MyGlob") & """"
		End If
	Else
		DOpus.Output "MyGlob is not set to any value"
	End If
End Function

```

Also note that the way you are setting the value will set it to `"TheValue"` including quotes, not to `TheValue`.

If you don't want the quotes, just use:

```nohighlight
@set $glob:MyGlob=TheValue

```

PS: Remember not to combine the `Exists` and `Get` calls into a single line in VBScript. This won't work, which is one thing I hate about VBScript. 🙂

```auto
' WON'T WORK:
If DOpus.vars.Exists("MyGlob") And DOpus.vars.Get("MyGlob") = "TheValue" Then 

```

(Similar is fine in JScript, on the other hand.)

---

<div class="post-metadata">

### Author: ![kanu](https://resource.dopus.com/letter_avatar_proxy/v4/letter/k/48db29/32.png) [@kanu](https://resource.dopus.com/u/kanu)
#### Post date: [May 23, 2018, 1:42pm UTC](https://resource.dopus.com/t/check-for-string-value-of-global-variable/28929/5 "2018-05-23T13:42:49Z")

</div>

Thank you Leo you are a life saver it is working properly now.

---

<div class="post-metadata">

### Author: ![kanu](https://resource.dopus.com/letter_avatar_proxy/v4/letter/k/48db29/32.png) [@kanu](https://resource.dopus.com/u/kanu)
#### Post date: [May 23, 2018, 5:31pm UTC](https://resource.dopus.com/t/check-for-string-value-of-global-variable/28929/6 "2018-05-23T17:31:15Z")

</div>

Leo, I would need another help from you regarding a global variable with a string value.  
I would like to make the string variable persistent that it would exist even after restart of the program. The following is the declaration of the variable that I made in a button: @set $glob:MyVar=TheStringValue persist  
But after a restart of the program the variables does not exist. I have been looking in the help file but I couldn't find how to do it.

Thanks in advance for your help

---

<div class="post-metadata">

### Author: ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)
#### Post date: [May 23, 2018, 6:19pm UTC](https://resource.dopus.com/t/check-for-string-value-of-global-variable/28929/7 "2018-05-23T18:19:39Z")

</div>

![200dpi_wc11447_dowshlp](https://resource.dopus.com/uploads/default/original/3X/7/3/73ca2e3651b635bf7050780dd85d2a061dd37778.png)

---

<div class="post-metadata">

### Author: ![kanu](https://resource.dopus.com/letter_avatar_proxy/v4/letter/k/48db29/32.png) [@kanu](https://resource.dopus.com/u/kanu)
#### Post date: [May 23, 2018, 6:27pm UTC](https://resource.dopus.com/t/check-for-string-value-of-global-variable/28929/8 "2018-05-23T18:27:53Z")

</div>

I was looking at this chapter but I missed the last sentence.

Thank you
