VBScript Object Required When Trying to set DateTime

后端 未结 2 844
感动是毒
感动是毒 2020-12-20 08:47

I am new to VBScript and can\'t figure out why I\'m getting an Object Required error with my code. It\'s very simple right now, I\'ve just begun it:

<         


        
2条回答
  •  旧巷少年郎
    2020-12-20 09:31

    The first mistake is to use Set when assigning a non-object to a variable. The last entry in the 'Related' list “Object required” when using Set in an assignment deals with it.

    >> Set dt = "a string"
    >>
    Error Number:       424
    Error Description:  Object required [because Set wants an object to assign]
    

    No Set, no problem:

    >> dt = "a string"
    >> WScript.Echo dt
    >>
    a string
    

    Removing the Set will reveal the next problem: Unless you defined a suitable class and an instance named 'DateTime' of your own, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") will fail with the same error 424.

    >> dt = Nix.Now.ToString("")
    >>
    Error Number:       424
    Error Description:  Object required [to use its .Now member]
    

    Do a search here for ways to format a date in VBScript. (First hit for "[vbscript] format date": date format in VBS)

    The () in the .Write call should be removed; they are not parameter list (), but 'pass me per value' (). See this answer and follow the link to Eric Lippert's article.

提交回复
热议问题