VBS - the value of variable in the for loop? [duplicate] - vbscript

Consider the below code:
Option Explicit
Dim Count : Count = 4
'Case-I
Sub Display1()
MsgBox(Count)
End Sub
'Case-II
Sub Display2(Count)
MsgBox(Count)
End Sub
Confusion
here should the variable Count will be accessible to the both
method or only to the Display2 method?
If the variable count is not accessible with Display1 method,then
is there any way to make it useable within any sub or function
without passing it to that Sub or Function
Now if I updated the Countvariable from the Display2 sub will
that value can be used to other function or sub provided that
VBScript has global variable declarations.

Variable count is declared outside of any function or sub-procedure, hence its scope is global here. Global variable can be used in any function or sub-procedure and its value can be change in anywhere in program flow.
Now if you make call to this procedure and run the .vbs file, message box will display 4 in both cases.
Option Explicit
Dim Count : Count = 4
'Case-I
Sub Display1()
MsgBox(Count)
End Sub
'Case-II
Sub Display2(Count)
MsgBox(Count)
End Sub
Call Display1()
Call Display2(Count)
If you change value of count variable in any function or sub procedure, its value will change for other procedure also as its scope is global.
Option Explicit
Dim Count : Count = 4
'Case-I
Sub Display1()
Count = 5 ''value changed here
MsgBox(Count)
End Sub
'Case-II
Sub Display2(Count)
MsgBox(Count)
End Sub
Call Display1()
Call Display2(Count)
In above case, both message box will display 5

Related

Vbscript multiple Statements for single Return Statement

I am developing a VBScript like follows
Dim a
Set a = New Class1
a("text").doSomething 'Has to execute doSomething in Class1
a("text").anotherSomething 'Has to execute doSoemthing in Class2
class Class1
Dim b
Dim c
public default Function init(str)
Set b = New Class2
Set c = New Class3
'Some more operations to perform
If **What is the condition can be?** Then
Set init = c
Else
Set init = b
End If
End Function
End class
class Class2
public Function doSomething()
'Stuff to do something
End Function
End class
class Class3
public Function anotherSomething()
'Stuff to do something
End Function
End class
Here, Object "a" has parameters and this parameter can be same.
So, i can't keep is parameter "=" or "<>"
And, I can't place those functions in Class1.
So, What can be that condition which can decide.
It looks like you need to leverage delegators. Basically you need internal objects that are of the type you want to access methods that would be in your parent classes in OOP.
Here is an example:
Class ScreenPoint
'' Properties
'Ancestor Point2D
Private P2D
'Point color
Private Color
'----------------------
'' Methods
'Constructor - called automatically
Private Sub Class_Initialize()
Set P2D = new Point2D
End Sub
'----------------------
'Destructor - called automatically
Private Sub Class_Terminate()
Set P2D = Nothing
End Sub
'----------------------
'A pair of methods to access private property X
Property Get X
X = P2D.X
End Property
Property Let X(ByVal in_X)
P2D.X = in_X
End Property
'----------------------
'A pair of methods to access private property Y
Property Get Y
Y = P2D.Y
End Property
Property Let Y(ByVal in_Y)
P2D.Y = in_Y
End Property
'----------------------
End Class
If you are lost, try reading the source article here: http://automation-beyond.com/2008/11/16/oop-vbscript-2/

I am trying to make a program to remind me when i need to do something but i just cant get it to work

I don't know what im doing wrong...
If someone could help me that would be awesome, i've been at this for the past 3 hours and cant find any solution (newbie alert)
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim SAPI
SAPI = CreateObject("SAPI.spvoice")
SAPI.Speak("I will remind you at " & TextBox2.Text & TextBox1.Text)
'Me.Visible = False
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim SAPI
SAPI = CreateObject("SAPI.spvoice")
If TimeOfDay = TextBox2.Text Then SAPI.Speak(TextBox1.Text)
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs)
End Sub
End Class

Can we write class inside VBScript function?

Function CWSLogin(strApplicationPath,strUser,strMember,strPassword)
Class TryCatchFinally
Private Sub Class_Initialize 'Try
End Sub
End Class
End Function
No. Functions are in classes where they become known as methods.

assign object array to class attribute vba

I have maintain array of class objects using another class member variable. I was unable to set the object array to the class variable. Able to call the setter method but gives error "can't assign a array"
Class module AA
private c as integer
private d as integer
Class module B
private a(50) as AA
public sub setA(byref a1() as AA)
a = a1 ' this assignment not work.
end sub
public function getA() as AA()
getA = a ' this work
end function
Module main
dim tags() as A
dim tag as A
with B
Redim preserve tags(ubound(.getA()) ' get the class module b array and set the element as 50
for i =0 to ubound(tags)
if tags(i) is nothing then
tag.setC(3)
tag.setD(5)
tags(i) = tag
end if
next i
.setA tags ' call the setter method but gives error can't assign a array
end with
You cannot assign to an array if you declare the size of the array. Try something like this:
Class module B
private a() as AA
public sub setA(byref a1() as AA)
a = a1 ' this should work now.
end sub
public function getA() as AA()
getA = a ' this work
end function
public sub class_initialize
redim a(50)
end sub

Scope of a variable in VBScript

Consider the below code:
Option Explicit
Dim Count : Count = 4
'Case-I
Sub Display1()
MsgBox(Count)
End Sub
'Case-II
Sub Display2(Count)
MsgBox(Count)
End Sub
Confusion
here should the variable Count will be accessible to the both
method or only to the Display2 method?
If the variable count is not accessible with Display1 method,then
is there any way to make it useable within any sub or function
without passing it to that Sub or Function
Now if I updated the Countvariable from the Display2 sub will
that value can be used to other function or sub provided that
VBScript has global variable declarations.
Variable count is declared outside of any function or sub-procedure, hence its scope is global here. Global variable can be used in any function or sub-procedure and its value can be change in anywhere in program flow.
Now if you make call to this procedure and run the .vbs file, message box will display 4 in both cases.
Option Explicit
Dim Count : Count = 4
'Case-I
Sub Display1()
MsgBox(Count)
End Sub
'Case-II
Sub Display2(Count)
MsgBox(Count)
End Sub
Call Display1()
Call Display2(Count)
If you change value of count variable in any function or sub procedure, its value will change for other procedure also as its scope is global.
Option Explicit
Dim Count : Count = 4
'Case-I
Sub Display1()
Count = 5 ''value changed here
MsgBox(Count)
End Sub
'Case-II
Sub Display2(Count)
MsgBox(Count)
End Sub
Call Display1()
Call Display2(Count)
In above case, both message box will display 5

Resources