Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Get variable data from function to pass back to main for use.

Reply
 
Old 03-21-2009   #1 (permalink)
Big D


 
 

Get variable data from function to pass back to main for use.

Issue trying to solve: I have data in a function where I am reading an xml
file and need to pass back up main to use. I started out using an array but
if there is a better way please let me know. The program works just need
data in availiable in main to store in variables. Please note I did not post
xml function of reading data. it works - trying to keep thread some what
clean versus tons of script.

'******main*******
funReadEnvParametersXML strEnvironment 'function to pass in the
environment to pull from the xml file.

Dim TestArray0
Dim TestArray1

TestArray0 =myarray(0) 'this does work since I am not passing data up.
Don't know how to pass array data back up. comes back NULL now.
TestArray1 =myarray(1) 'this does work since I am not passing data up.
Don't know how to pass array data back up.




'------------------------------------------------------function that reads
in xml
Function funReadEnvParametersXML(strEnvironment)

Set xml = GetEnvParametersXML()
Set Environments = xml.getElementsByTagName("Environment")

For Each Environment In Environments
sEnvironmentId=Environment.Attributes.getNamedItem("Id").value
Counter = 0
Set variables = Environment.getElementsByTagName("variable")

numVariables=variables.length -1
ReDim myarray(numVariables)

If (sEnvironmentId = strEnvironment) Then
For i = 0 To variables.length -1
sName=variables(i).Attributes.getNamedItem("name").value

myarray(i) = sName
Next
WScript.Echo "Array 0........... " & myarray(0)
WScript.Echo "Array 1........... " & myarray(1)

'************************
'I have access to the array results here. How do I pas them back up to the
main.
'************************


End If
Next





End Function



My System SpecsSystem Spec
Old 03-21-2009   #2 (permalink)
T Lavedas


 
 

Re: Get variable data from function to pass back to main for use.

On Mar 21, 9:44*am, "Todd Vargo" <tlva...@xxxxxx> wrote:
Quote:

> Big D wrote:
Quote:

> > Issue trying to solve: I have data in a function where I am reading an xml
> > file and need to pass back up main to use. I started out using an array
> but
Quote:

> > if there is a better way please let me know. The program works just need
> > data in availiable in main to store in variables. Please note I did not
> post
Quote:

> > xml function of reading data. it works - trying to keep thread some what
> > clean versus tons of script.
>
Quote:

> > '******main*******
>
> You need to declare myarray() before calling the function to make it a
> global array.
>
> Dim myarray()
>
Quote:

> > funReadEnvParametersXML strEnvironment * *'function to pass in the
> > environment to pull from the xml file.
>
Quote:

> > Dim TestArray0
> > Dim TestArray1
>
Quote:

> > TestArray0 =myarray(0) * *'this does work since I am not passing data up.
> > Don't know how to pass array data back up. comes back NULL now.
> > TestArray1 =myarray(1) * *'this does work since I am not passing data up.
> > Don't know how to pass array data back up.
>
Quote:

> > '------------------------------------------------------function that reads
> > in xml
> > Function funReadEnvParametersXML(strEnvironment)
>
Quote:

> > Set xml = GetEnvParametersXML()
> > Set Environments = xml.getElementsByTagName("Environment")
>
Quote:

> > For Each Environment In Environments
> > *sEnvironmentId=Environment.Attributes.getNamedItem("Id").value
> > *Counter = 0
> > *Set variables = Environment.getElementsByTagName("variable")
>
Quote:

> > *numVariables=variables.length -1
> > *ReDim myarray(numVariables)
>
Quote:

> > *If (sEnvironmentId = strEnvironment) Then
> > * For i = 0 To variables.length -1
> > * *sName=variables(i).Attributes.getNamedItem("name").value
>
Quote:

> > * *myarray(i) = sName
> > * Next
> > * * * * WScript.Echo "Array 0........... " & myarray(0)
> > * * * * WScript.Echo "Array 1........... " & myarray(1)
>
Quote:

> > '************************
> > 'I have access to the array results here. How do I pas them back up to the
> > main.
> > '************************
>
Quote:

> > *End If
> > Next
>
Quote:

> > End Function
Todd,

Your proposal works, but a better way is to use the Function the way a
function is intended to be used. That is, simply equate the array
result to the name of the function, as in

'************************
'Function has the array here. It is pass back up to the main, thus ...

funReadEnvParametersXML = myarray

'************************

Then in the main body, the function is accessed thus ...

TestArray = funReadEnvParametersXML(strEnvironment)
wsh.echo TestArray(0), TestArray(1)

Tom Lavedas
=========
My System SpecsSystem Spec
Old 03-21-2009   #3 (permalink)
Todd Vargo


 
 

Re: Get variable data from function to pass back to main for use.

Big D wrote:
Quote:

> Issue trying to solve: I have data in a function where I am reading an xml
> file and need to pass back up main to use. I started out using an array
but
Quote:

> if there is a better way please let me know. The program works just need
> data in availiable in main to store in variables. Please note I did not
post
Quote:

> xml function of reading data. it works - trying to keep thread some what
> clean versus tons of script.
>
> '******main*******
You need to declare myarray() before calling the function to make it a
global array.

Dim myarray()
Quote:

> funReadEnvParametersXML strEnvironment 'function to pass in the
> environment to pull from the xml file.
>
> Dim TestArray0
> Dim TestArray1
>
> TestArray0 =myarray(0) 'this does work since I am not passing data up.
> Don't know how to pass array data back up. comes back NULL now.
> TestArray1 =myarray(1) 'this does work since I am not passing data up.
> Don't know how to pass array data back up.
>
>
>
>
> '------------------------------------------------------function that reads
> in xml
> Function funReadEnvParametersXML(strEnvironment)
>
> Set xml = GetEnvParametersXML()
> Set Environments = xml.getElementsByTagName("Environment")
>
> For Each Environment In Environments
> sEnvironmentId=Environment.Attributes.getNamedItem("Id").value
> Counter = 0
> Set variables = Environment.getElementsByTagName("variable")
>
> numVariables=variables.length -1
> ReDim myarray(numVariables)
>
> If (sEnvironmentId = strEnvironment) Then
> For i = 0 To variables.length -1
> sName=variables(i).Attributes.getNamedItem("name").value
>
> myarray(i) = sName
> Next
> WScript.Echo "Array 0........... " & myarray(0)
> WScript.Echo "Array 1........... " & myarray(1)
>
> '************************
> 'I have access to the array results here. How do I pas them back up to the
> main.
> '************************
>
>
> End If
> Next
>
>
>
>
>
> End Function
>
>
My System SpecsSystem Spec
Old 03-21-2009   #4 (permalink)
Todd Vargo


 
 

Re: Get variable data from function to pass back to main for use.

Todd Vargo wrote:
Quote:

> Big D wrote:
Quote:

> > Issue trying to solve: I have data in a function where I am reading an
xml
Quote:
Quote:

> > file and need to pass back up main to use. I started out using an array
> but
Quote:

> > if there is a better way please let me know. The program works just need
> > data in availiable in main to store in variables. Please note I did not
> post
Quote:

> > xml function of reading data. it works - trying to keep thread some what
> > clean versus tons of script.
> >
> > '******main*******
>
> You need to declare myarray() before calling the function to make it a
> global array.
>
> Dim myarray()
>
Quote:

> > funReadEnvParametersXML strEnvironment 'function to pass in the
> > environment to pull from the xml file.
This simple example should help...

Dim myarr()
foo
Wscript.Echo myarr(0) & "..." & myarr(1)

Function foo()
ReDim myarr(2)
myarr(0) = "abc"
myarr(1) = "xyz"
End Function

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

My System SpecsSystem Spec
Old 03-21-2009   #5 (permalink)
Todd Vargo


 
 

Re: Get variable data from function to pass back to main for use.

T Lavedas wrote:
Todd,

Your proposal works, but a better way is to use the Function the way a
function is intended to be used. That is, simply equate the array
result to the name of the function, as in

'************************
'Function has the array here. It is pass back up to the main, thus ...

funReadEnvParametersXML = myarray

'************************

Then in the main body, the function is accessed thus ...

TestArray = funReadEnvParametersXML(strEnvironment)
wsh.echo TestArray(0), TestArray(1)
=============================

Tom,

Of course, you are right. I was thrown off by the OP's code which led me to
believe the data in the array needed to be separated into multiple variables
at the main level since they were dimmed separately. Thanks again for your
POV.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

My System SpecsSystem Spec
Old 03-26-2009   #6 (permalink)
Big D


 
 

Re: Get variable data from function to pass back to main for use.

I implemented per your feedback and have it working but ran into a few other
concerns of what I was doing...so I slightly changed implementation

Removed array and decided to initialize in the function itself by doing the
following.

I tried using the execute statement and maybe it's not the correct way. My
intent was running execute command to set variable. I think I need to
intialalise variable in the main but unsure of how to do.

Still the same goal: need to set variable in function so I can use it in
main.

For Each Environment In Environments
sEnvironmentId=Environment.Attributes.getNamedItem("Id").value
Set variables = Environment.getElementsByTagName("variable")

numVariables=variables.length -1
ReDim myArray(numVariables)

If (sEnvironmentId = strEnvironment) Then
For i = 0 To variables.length -1
sName=variables(i).Attributes.getNamedItem("name").value
sValue=variables(i).Attributes.getNamedItem("value").value

Execute("str" & sName & "=" & sValue)
<--------------------------------Added this line. Get name and value, append
str becuase it's a string. The name is unique. need to set to use in main.
'WScript.Echo strStoresADDomain
Next
End If
Next

"Todd Vargo" <tlvargo@xxxxxx> wrote in message
news:Otp$K5iqJHA.3864@xxxxxx
Quote:

> Todd Vargo wrote:
Quote:

>> Big D wrote:
Quote:

>> > Issue trying to solve: I have data in a function where I am reading an
> xml
Quote:
Quote:

>> > file and need to pass back up main to use. I started out using an array
>> but
Quote:

>> > if there is a better way please let me know. The program works just
>> > need
>> > data in availiable in main to store in variables. Please note I did not
>> post
Quote:

>> > xml function of reading data. it works - trying to keep thread some
>> > what
>> > clean versus tons of script.
>> >
>> > '******main*******
>>
>> You need to declare myarray() before calling the function to make it a
>> global array.
>>
>> Dim myarray()
>>
Quote:

>> > funReadEnvParametersXML strEnvironment 'function to pass in the
>> > environment to pull from the xml file.
>
> This simple example should help...
>
> Dim myarr()
> foo
> Wscript.Echo myarr(0) & "..." & myarr(1)
>
> Function foo()
> ReDim myarr(2)
> myarr(0) = "abc"
> myarr(1) = "xyz"
> End Function
>
> --
> Todd Vargo
> (Post questions to group only. Remove "z" to email personal messages)
>

My System SpecsSystem Spec
Old 03-26-2009   #7 (permalink)
Todd Vargo


 
 

Re: Get variable data from function to pass back to main for use.

Big D wrote:
Quote:

> I implemented per your feedback and have it working but ran into a few
other
Quote:

> concerns of what I was doing...so I slightly changed implementation
>
> Removed array and decided to initialize in the function itself by doing
the
Quote:

> following.
>
> I tried using the execute statement and maybe it's not the correct way. My
> intent was running execute command to set variable. I think I need to
> intialalise variable in the main but unsure of how to do.
>
> Still the same goal: need to set variable in function so I can use it in
> main.
>
> For Each Environment In Environments
> sEnvironmentId=Environment.Attributes.getNamedItem("Id").value
> Set variables = Environment.getElementsByTagName("variable")
>
> numVariables=variables.length -1
> ReDim myArray(numVariables)
>
> If (sEnvironmentId = strEnvironment) Then
> For i = 0 To variables.length -1
> sName=variables(i).Attributes.getNamedItem("name").value
> sValue=variables(i).Attributes.getNamedItem("value").value
>
> Execute("str" & sName & "=" & sValue)
> <--------------------------------Added this line. Get name and value,
append
Quote:

> str becuase it's a string. The name is unique. need to set to use in main.
> 'WScript.Echo strStoresADDomain
> Next
> End If
> Next
Sorry, I do not follow. It would be better if you post the code that works
and then describe the alterations that you want to make to it.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Using a variable from within a function PowerShell
Set a variable in a function then pass it off? PowerShell
Why the Value not able to Pass to a Function PowerShell
how to pass *properties* to a function PowerShell
Howto: pass all args to a sub function? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46