Micro Focus QTP (UFT) Forums
Regarding Global Variable in QTP - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: UFT / QTP Beginners (https://www.learnqtp.com/forums/Forum-UFT-QTP-Beginners)
+--- Thread: Regarding Global Variable in QTP (/Thread-Regarding-Global-Variable-in-QTP)



Regarding Global Variable in QTP - 10610329 - 03-21-2011


i want to create a global variable and use it across different action and methods.

I used environment variable but i m not able to do it.
I want to create a Excel application object and used it in different action .
Can you provide me the code or logic.


RE: Regarding Global Variable in QTP - basanth27 - 03-21-2011

Can you please list out the steps you did to create an environment variable?


RE: Regarding Global Variable in QTP - 10610329 - 03-21-2011

Code:
set objExcel=createobject("excel.application")
environment.value("wbk")=objExcel.workbooks.open("somepath")

now i wnt to use this wbk object in my function libaray

public function a()
  set objworkbooks=environment.value("wbk")
  set objworksheets=objworkbooks.sheets
end function

but it show some error objworkbooks dnt hve sheets method


RE: Regarding Global Variable in QTP - basanth27 - 03-21-2011

Two things,
1. I dont understand your objective. What are you trying to achieve? what do you want to store in your environment variable?
2. did you create the environement variable under test settings->Environment->Userdefined?


RE: Regarding Global Variable in QTP - 10610329 - 03-21-2011

My objective is to create a object.suppose i wnt to create a database connection.now i m not going to create database connection every time .so i create one and used it whrever it needed.For that purpose i m trying to create global variable which i create in one script and used it in different functions.


RE: Regarding Global Variable in QTP - basanth27 - 03-22-2011

If you want to create an object and use it across use it as a function. You can call this function in your various actions.
Here is how environment variable works,

Code:
Under Action 1:
                     Environment.Value("oStringValue") = "LearnQTP"
In Action 2, Action 3, Action 4..etc etc. you can access this stored value as,
  oMyVar = Environment.Value("oStringValue")
msgbox oMyVar ' this will give you LearnQTP which is the value the variable holds.