How can I inherit Object or Values in a Function? (In Qbasic there was a "Common" direction what is get the specified values for the function, but what is it in VBScript?)
Somewhere on this forum I read about this.
My exactly problem, that I create a file out of the function, and I want to write in the file during the function.
Could explain me, or suggesting anything document or sample, that how can I declare a varibale in a Function, and how does the function run? How can I give values to the function from the Main porgram, and reverse?
I am really impressed by your patience and willingness to go the extra 15 miles to help out a stranger, Zifter.
No sarcasm, irony, or joking around on my part. Just honest respect.
I don't think... no, I *know* that I couldn't stick with it this long.
It's not always easy to stay patient. But that is an advantage of helping people through a forum. You can first take a coffee, smoke a cigarette or two, relax and rephrase any not-so-friendly sentences...
quote:
ORIGINAL: reloader Hi Zifter,
Could explain me, or suggesting anything document or sample, that how can I declare a varibale in a Function, and how does the function run? How can I give values to the function from the Main porgram, and reverse?
If you define a variable in your script, then that variable is known in the whole script. Also in the functions. If you define a variable in a function, then that variable is only known within the function itself. You can pass the value of a variable to a function or you can pass (a pointer to) a variable object. You do this respectively with"ByVal" and ByRef". Check the following two examples:
Thanks for the fast reply, It's clear now, but what about the objects?:
(The favorite code of yours:) )
Option Explicit Dim objXMLDoc Set objXMLdoc = CreateObject("Msxml2.DOMDocument.5.0") objXMLDoc.async = False
objXMLDoc.load("P:\audits2.xml")
fnEchoChildsAndAttributes objXMLDoc.documentElement Set objXMLDoc = Nothing Dim fso, Mf Set fso = CreateObject("Scripting.FileSystemObject") Set Mf = fso.CreateTexFile("P:\Parser4\test.txt", True)
Function fnEchoChildsAndAttributes(objTempNode)
Dim objTempChildNode
Dim objTempAttribute ...
If I setting fso and Mf in the function, I get an error message as "Permission Denied". I think it wants to recreate, but than what is the solution, because if I setting outside the function I get an error message: "Object Required".
I think accordingly variables inherited in the function too(as u write the prev. reply), but the objects.(?)
You get the message "Permission Denied" when another program (could be the same script) is holding the file. You can't write to a file that is in use. The message "Object Required" should be self explaining.
If you post your complete script, we can take a look where the error might be located. The part of the script you posted looks ok to me.
Here is the code((error message is 'object required 'Mf' ')):
Option Explicit Dim objXMLDoc Set objXMLdoc = CreateObject("Msxml2.DOMDocument.5.0") objXMLDoc.async = False
objXMLDoc.load("P:\audits2.xml")
fnEchoChildsAndAttributes objXMLDoc.documentElement Set objXMLDoc = Nothing Dim fso, Mf Set fso = CreateObject("Scripting.FileSystemObject") Set Mf = fso.Createtextfile("P:\Parser4\test.txt", True)
Function fnEchoChildsAndAttributes(objTempNode)
Dim objTempChildNode
Dim objTempAttribute
If Not objTempNode.ChildNodes Is Nothing Then If Not objTempNode.ChildNodes.Length = 0 Then For Each objTempChildNode In objTempNode.ChildNodes If IsNull(objTempChildNode.NodeValue) Then WScript.Echo objTempChildNode.NodeName Else WScript.Echo objTempChildNode.NodeName & ": " & objTempChildNode.NodeValue If objTempChildNode.NodeName <> "rth" Then Mf.WriteLine "<" & objTempChildNode.NodeName & ">"
End If fnEchoChildsAndAttributes(objTempChildNode) Next End If End If If Not objTempNode.Attributes Is Nothing Then If Not objTempNode.Attributes.Length = 0 Then For Each objTempAttribute In objTempNode.Attributes WScript.Echo objTempAttribute.Name & ": " & objTempAttribute.Value
Mf.WriteLine "<" & objTempAttribute.Name & ">" Next End If End If End Function
You get the error message because you declare and create the two objects "fso" and "Mf" after you call the function. So the function can't 'know' these two objects. Move those statements before the call to the function. Like this:
< Message edited by Zifter -- 9/25/2005 8:40:50 PM >
There are several ways of removing the duplicates out of an array. In this case I would use a dictionary object. These objects are two dimensional kind of arrays, with the extra that they don't allow duplicate items. So you define a dictionary object. Try to add each element of the array to this dictionary object. When you try to add an element that is already in the dictionary, an error will be raised. You can skip this error raising by putting "On Error Resume Next" and "On Error Goto 0" around the filling of the dictionary object. Here is a small example.