This variable has not been assigned a value Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

This variable has not been assigned a value

Post by Ecimeric » 14 May 2021, 13:17

Code: Select all

CurrentSizeTick := A_TickCount
LastSize := "", LastSizeTick = "" ; new line
How can I resolve this error with LastSize and LastSizeTick in this script without inadvertently disabling the Kb/s displayed?

Also, as I am running the script from a menu that ExitApps, how can I avoid inadvertently exiting the script when I reopen the menu?
User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: This variable has not been assigned a value

Post by boiler » 14 May 2021, 14:00

Where/for what specifically is it citing that warning? It’s not clear to me what the overall script is from your link. Can you post the overall script?

By the way, you need to change the = to := on your last assignment unless you really want to assign a pair of quotation marks to it instead of an empty string.
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: This variable has not been assigned a value

Post by SKAN » 14 May 2021, 14:10

boiler wrote:
14 May 2021, 14:00
you need to change the = to := on your last assignment unless you really want to assign a pair of quotation marks to it instead of an empty string.
It is assignment and allowed. Bad practice though.
https://www.autohotkey.com/docs/Variables.htm#comma
User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: This variable has not been assigned a value

Post by boiler » 14 May 2021, 14:33

I never noticed that. Thanks for pointing it out. Not a fan of it, though. :D
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: This variable has not been assigned a value

Post by SKAN » 14 May 2021, 15:07

boiler wrote:
14 May 2021, 14:33
Not a fan of it, though. :D
Neither me. I had to say "and allowed" because doc doesn't mention it is "deprecated".
:)
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: This variable has not been assigned a value

Post by Ecimeric » 14 May 2021, 15:18

Code: Select all

	Url            = http://download.adlice.com/RogueKiller/RogueKiller.exe
	DownloadAs     = RogueKiller.exe
	Overwrite      := True 
	UseProgressBar := True
	DownloadFile(Url, DownloadAs, Overwrite, UseProgressBar)
	

;=================== Start Functions =====================================================================

DownloadFile(UrlToFile, SaveFileAs, Overwrite := True, UseProgressBar := True) {
    ;Check if the file already exists and if we must not overwrite it
      If (!Overwrite && FileExist(SaveFileAs))
          Return
    ;Check if the user wants a progressbar
      If (UseProgressBar) {
          ;Initialize the WinHttpRequest Object
            WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1") ;Errors on Windows 7 if DefaultSecureProtocols has not been set
          ;Download the headers
            WebRequest.Open("HEAD", UrlToFile)
            WebRequest.Send()
          ;Store the header which holds the file size in a variable:
            FinalSize := WebRequest.GetResponseHeader("Content-Length")
          ;Create the progressbar and the timer
            Progress, H80, , Downloading..., %UrlToFile%
            SetTimer, __UpdateProgressBar, 100
      }
    ;Download the file
      UrlDownloadToFile, %UrlToFile%, %SaveFileAs%
    ;Remove the timer and the progressbar because the download has finished
      If (UseProgressBar) {
          Progress, Off
          SetTimer, __UpdateProgressBar, Off
      }
    Return
    
    ;The label that updates the progressbar
      __UpdateProgressBar:
          ;Get the current filesize and tick
            CurrentSize := FileOpen(SaveFileAs, "r").Length ;FileGetSize wouldn't return reliable results
            CurrentSizeTick := A_TickCount
            LastSize := "", LastSizeTick := "" ; new line
          ;Calculate the downloadspeed
            Speed := Round((CurrentSize/1024-LastSize/1024)/((CurrentSizeTick-LastSizeTick)/1000)) . " Kb/s"
          ;Save the current filesize and tick for the next time
            LastSizeTick := CurrentSizeTick
            LastSize := FileOpen(SaveFileAs, "r").Length
          ;Calculate percent done
            PercentDone := Round(CurrentSize/FinalSize*100)
          ;Update the ProgressBar
            Progress, %PercentDone%, %PercentDone%`% Done, Downloading...  (%Speed%), Downloading %SaveFileAs% (%PercentDone%`%)
      Return
}
User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: This variable has not been assigned a value

Post by boiler » 14 May 2021, 18:23

I’m not able to reproduce that message, even if I add #Warn to the script.
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: This variable has not been assigned a value  Topic is solved

Post by safetycar » 15 May 2021, 09:46

@Ecimeric It's quite difficult to understand you if you just copy a code without saying a word...
So, you put that line there because you wanted to get rid of the warning message.
(Which as @boiler said, this type of message can happen because you had a #warn setting active.)

You wanted to put a declaration to fix it, but you are putting that in the middle of a timer subroutine, resetting the values before even using them.
You have to make the declaration outside the timer, in this case a good place to put your new line is just as the first line of the function DownloadFile.
Ecimeric
Posts: 130
Joined: 11 Jan 2017, 02:23

Re: This variable has not been assigned a value

Post by Ecimeric » 15 May 2021, 16:50

Thank you; the variables are only used in the timer subroutine, so I had not thought of declaring them elsewhere.

As I am running the script from a menu that ExitApps, how can I avoid inadvertently exiting the script when I reopen the menu?
Last edited by Ecimeric on 16 May 2021, 01:38, edited 1 time in total.
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: This variable has not been assigned a value

Post by safetycar » 16 May 2021, 00:51

Sounds like a different problem, I think it'd be better that you made a new post with more information for that.
faizanmazhar00
Posts: 1
Joined: 30 Jan 2022, 09:20

Re: This variable has not been assigned a value

Post by faizanmazhar00 » 30 Jan 2022, 09:27

SKAN wrote:
14 May 2021, 14:10
boiler wrote:
14 May 2021, 14:00
you need to change the = to := on your last assignment unless you really want to assign a pair of quotation marks to it instead of an empty string.
It is assignment and allowed. Bad practice though.
https://www.autohotkey.com/docs/Variables.htm#comma
ok
Post Reply

Return to “Ask for Help (v1)”