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 - syntax error in a class

Reply
 
Old 07-15-2009   #1 (permalink)
Heinz


 
 

syntax error in a class

Hello,

I'm trying to create a simple class that can display the current date using
WMI functions.

When I use the following code I get an error
Syntax error in line strComputer = "."


Why could this line cause a syntax error ?

Here is the code :

class current_date

private strComputer
public month
public day
public year

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_LocalTime")

For Each objItem in colItems

month = objItem.Month
day = objItem.Day
year = objItem.Year

Next


end class

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

dim test
set test = new current_date

wscript.echo test.month
wscript.echo test.day
wscript.echo test.year




Thank you

Heinz



My System SpecsSystem Spec
Old 07-15-2009   #2 (permalink)
Richard Mueller [MVP]


 
 

Re: syntax error in a class

Heinz wrote:
Quote:

>
> I'm trying to create a simple class that can display the current date
> using WMI functions.
>
> When I use the following code I get an error
> Syntax error in line strComputer = "."
>
>
> Why could this line cause a syntax error ?
>
> Here is the code :
>
> class current_date
>
> private strComputer
> public month
> public day
> public year
>
> strComputer = "."
>
> Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
> Set colItems = objWMIService.ExecQuery("Select * From Win32_LocalTime")
>
> For Each objItem in colItems
>
> month = objItem.Month
> day = objItem.Day
> year = objItem.Year
>
> Next
>
>
> end class
>
> '**********************
>
> dim test
> set test = new current_date
>
> wscript.echo test.month
> wscript.echo test.day
> wscript.echo test.year
>
VBScript classes can define properties and methods, but don't have
executable code outside of methods. They are not subroutines or functions in
themselves, although they can define them within. This version worked for
me, where I encapsulated the executable code in a Sub. Note I had to call
the Sub to have the public properties assigned values:
===============
class current_date

public month
public day
public year

Public Sub GetDate
strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer &
"\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From
Win32_LocalTime")

For Each objItem in colItems
month = objItem.Month
day = objItem.Day
year = objItem.Year
Next
End Sub

end class

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

dim test
set test = new current_date

test.GetDate

wscript.echo test.month
wscript.echo test.day
wscript.echo test.year

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 07-15-2009   #3 (permalink)
Heinz


 
 

Re: syntax error in a class

"Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> schrieb im
Newsbeitrag news:u5BjMpVBKHA.1488@xxxxxx
Quote:

> Heinz wrote:
>
Quote:

>>
>> I'm trying to create a simple class that can display the current date
>> using WMI functions.
>>
>> When I use the following code I get an error
>> Syntax error in line strComputer = "."
>>
>>
>> Why could this line cause a syntax error ?
>>
>> Here is the code :
>>
>> class current_date
>>
>> private strComputer
>> public month
>> public day
>> public year
>>
>> strComputer = "."
>>
>> Set objWMIService = GetObject("winmgmts:\\" & strComputer &
>> "\root\cimv2")
>> Set colItems = objWMIService.ExecQuery("Select * From Win32_LocalTime")
>>
>> For Each objItem in colItems
>>
>> month = objItem.Month
>> day = objItem.Day
>> year = objItem.Year
>>
>> Next
>>
>>
>> end class
>>
>> '**********************
>>
>> dim test
>> set test = new current_date
>>
>> wscript.echo test.month
>> wscript.echo test.day
>> wscript.echo test.year
>>
>
> VBScript classes can define properties and methods, but don't have
> executable code outside of methods. They are not subroutines or functions
> in
> themselves, although they can define them within. This version worked for
> me, where I encapsulated the executable code in a Sub. Note I had to call
> the Sub to have the public properties assigned values:
> ===============
> class current_date
>
> public month
> public day
> public year
>
> Public Sub GetDate
> strComputer = "."
>
> Set objWMIService = GetObject("winmgmts:\\" & strComputer &
> "\root\cimv2")
> Set colItems = objWMIService.ExecQuery("Select * From
> Win32_LocalTime")
>
> For Each objItem in colItems
> month = objItem.Month
> day = objItem.Day
> year = objItem.Year
> Next
> End Sub
>
> end class
>
> '**********************
>
> dim test
> set test = new current_date
>
> test.GetDate
>
> wscript.echo test.month
> wscript.echo test.day
> wscript.echo test.year
>
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab - http://www.rlmueller.net
> --
>
>

great, thank you


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
syntax error in command prompt? General Discussion
Help with syntax error in command prompt? Gaming
Generic Interface syntax in VS 2005 using Old syntax .NET General
Runtime Error: LIne:3 Syntax Error Live Messenger
Vista System Restore Syntax error 0x800700B Vista hardware & devices


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