Sign in to follow this  
Followers 0
Rad

Optional ByRef's in a func?

15 posts in this topic

#1 ·  Posted (edited)

Im making my own Time-Slider UDF's, and I came across a problem... I want it so you can read the time of the slider with this function, its just... It returns the HH:MM:SS, but I also want it to optionally set $iHour, $iMin, $iSec to the HH:MM:SS (So I dont have excess lines of code for splitting by the :'s)

Func _GUICtrlReadTimeSlider($vSlider, ByRef $iHour, ByRef $iMin, ByRef $iSec) ;ByRef should be optional
    Local $hour, $min, $sec
    _TicksToTime(GUICtrlRead($vSlider) * 1000, $Hour, $Min, $Sec)
    $iHour = $hour
    $iMin = $min
    $iSec = $sec
    Return $hour & ":" & $min & ":" & $sec
EndFunc
Edited by Rad

Share this post


Link to post
Share on other sites



The ByRef keyword is optional and means: (1) the parameter must a variable, and (2) the original variable is linked to the parameter, so that any changes to the parameter inside the function, would affect the original variable as well.


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

Yeah I figured that out looking at the _TicksToTime func. But, I want it so you can *optionally* have it set variables to the HH:MM:SS for you, that way you could use the message in a msgbox(), or by itself to set the variables alone

Share this post


Link to post
Share on other sites

Yeah I figured that out looking at the _TicksToTime func. But, I want it so you can *optionally* have it set variables to the HH:MM:SS for you, that way you could use the message in a msgbox(), or by itself to set the variables alone

Might want to look into @NumParams and set the others as optional with default values


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Well... That sort of helped but I could use a hint on what to do:

"@NumParams Number of parameters used in calling the user function."

I think this would work like:

_GUICtrlReadTimeSlider(1, 2, 3, 4)

@NumParams = 4

_GUICtrlReadTimeSlider(1, 2[,3[,4]]) ;[] = not included on script

@NumParams = 2

If I were to do that, how would I set the variables from the original script to the values in the UDF? Arent ByRef mandatory? If not, would ByRef $i = 5 set the original $i to 5 before the function is executes?

Or, would it only set $i = 5 if it were not included? If ByRef are mandatory then it wouldnt work... Ill test a few ways but I could sure use an explination lol

------

Func _GUICtrlReadTimeSlider($vSlider, $iHour = "-1", $iMin = "-1", $iSec = "-1")
    Local $hour, $min, $sec
    _TicksToTime(GUICtrlRead($vSlider) * 1000, $Hour, $Min, $Sec)
    If @NumParams = 4 Then
        $iHour = $hour
        $iMin = $min
        $iSec = $sec
    EndIf
    Return $hour & ":" & $min & ":" & $sec
EndFunc

;\/\/\/\/\/\/\/\/\/Does not set the variables in code below:

GUICtrlSetData($label, _GUICtrlReadTimeSlider($test, $hour3, $min3, $sec3))
msgbox(0,"",$hour3 & ":"& $min3 & ":"& $sec3)
Edited by Rad

Share this post


Link to post
Share on other sites

Well... That sort of helped but I could use a hint on what to do:

"@NumParams Number of parameters used in calling the user function."

I think this would work like:

_GUICtrlReadTimeSlider(1, 2, 3, 4)

@NumParams = 4

_GUICtrlReadTimeSlider(1, 2[,3[,4]]) ;[] = not included on script

@NumParams = 2

If I were to do that, how would I set the variables from the original script to the values in the UDF? Arent ByRef mandatory? If not, would ByRef $i = 5 set the original $i to 5 before the function is executes?

Or, would it only set $i = 5 if it were not included? If ByRef are mandatory then it wouldnt work... Ill test a few ways but I could sure use an explination lol

------

Func _GUICtrlReadTimeSlider($vSlider, $iHour = "-1", $iMin = "-1", $iSec = "-1")
    Local $hour, $min, $sec
    _TicksToTime(GUICtrlRead($vSlider) * 1000, $Hour, $Min, $Sec)
    If @NumParams = 4 Then
        $iHour = $hour
        $iMin = $min
        $iSec = $sec
    EndIf
    Return $hour & ":" & $min & ":" & $sec
EndFunc

;\/\/\/\/\/\/\/\/\/Does not set the variables in code below:

GUICtrlSetData($label, _GUICtrlReadTimeSlider($test, $hour3, $min3, $sec3))
msgbox(0,"",$hour3 & ":"& $min3 & ":"& $sec3)
I see what your wanting, not going to happen that way, the optional params would be copies of the data, not the variable itself.

Another option would be global vars for those or pass in the variable names byref, if the variable value <> -1 then use it.


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

Another option would be global vars for those or pass in the variable names byref, if the variable value <> -1 then use it.

With globals do you have to declare them in the original script? I dont think that would work as well

With byref, wouldnt _GUICtrlReadTimeSlider($vSlider) give you an error (incorrect number of params)? I already tried ByRef $iHour = -1, gave me an error

Share this post


Link to post
Share on other sites

With globals do you have to declare them in the original script? I dont think that would work as well

With byref, wouldnt _GUICtrlReadTimeSlider($vSlider) give you an error (incorrect number of params)? I already tried ByRef $iHour = -1, gave me an error

Set your variables to -1 if not being used, pass them in byref.

in the function you can check if they are set to -1.


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

Wait im so confused could I have an example?

If I use ByRef it requires there to be a parameter there... In case of a msgbox() I would like to have the parameters optional, and only return HH:MM:SS

But otherwise it should automatically be able to set those variables.

So you should require a variable, but if there is one it should change its value.

If I set to -1 and theres no variable as a parameter, then byref will fail because its not a variable

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

_Main()

Func _Main()
    Local $iHour = -1, $iMin = -1, $iSec = -1
    ; your code here for the gui
    ;
    _GUICtrlReadTimeSlider($vSlider, $iHour, $iMin, $iSec)
    
EndFunc

Func _GUICtrlReadTimeSlider($vSlider, ByRef $iHour, ByRef $iMin, ByRef $iSec)
    Local $hour, $min, $sec
    _TicksToTime(GUICtrlRead($vSlider) * 1000, $Hour, $Min, $Sec)
     If $iHour <> -1 Then $iHour = $hour
     If $iMin <> -1 Then $iMin = $min
     If $iSec <> -1 Then $iSec = $sec
    Return $hour & ":" & $min & ":" & $sec
EndFunc

Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

Yeah see that works but its not what I was asking... I want it so your able to put just

Msgbox(0,"",_GUICtrlReadTimeSlider($vSlider))

I guess its not possible to combine the two, so Ill just make it require the variables... Oh well :/

Share this post


Link to post
Share on other sites

Yeah see that works but its not what I was asking... I want it so your able to put just

Msgbox(0,"",_GUICtrlReadTimeSlider($vSlider))

I guess its not possible to combine the two, so Ill just make it require the variables... Oh well :/

Why not just remove ByRef?

Func _GUICtrlReadTimeSlider($vSlider, $iHour = @HOUR, $iMin = @MIN, $iSec = @SEC);Or set the $iHour ... etcc to whatever you want it to be.
    Local $hour, $min, $sec
    ;_TicksToTime(GUICtrlRead($vSlider) * 1000, $Hour, $Min, $Sec)
     If $iHour <> -1 Then $iHour = $hour
     If $iMin <> -1 Then $iMin = $min
     If $iSec <> -1 Then $iSec = $sec
    Return $hour & ":" & $min & ":" & $sec
EndFunc
Or are you saying you want Global options without having to declare anything Global? I'll admit, I got lost quickly in this thread.

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Im so bad at explaining things, but they make perfect sense to me ;)

Ok, heres what I wanted, but I already have it done so its mandatory that you include your varaibles... But oh well Ill explain it anyway

--

Im making a media player, I wanted an easier way to handle the seek-bar, so I created this little UDF called "_GUICtrlCreateTimeSlider" which so far works fine

Now when you 'read' that slider, it would just give you a pretty random number "ex, 1205"

So I made a function that not only returns the correct time, but in HH:MM:SS

"_GUICtrlReadTimeSlider"

Now, to make things EVEN EASIER *not*, you include your 3 time variables, and it sets them for you, along with returning HH:MM:SS at the end.

This worked fine! There was 1 problem, which because Im so lazy... even though I will spend alot longer finding a faster way to do something than I could just doing it the long way (if that made sense)...

You couldnt, for example, simply use the HH:MM:SS returned (ex, Msgbox(0,"",_GUICtrlReadTimeSlider($MySlider)) - It requires the variables.

So I decide, ok Ill make them optional... = -1

Now the ByRef wont work, because ByRef's are mandatory you cant give them the 'optional' default setting...

So now its either, I only return HH:MM:SS, or I force you to include all 3 variables every time

---

Thats pretty much it, not even a big deal, I thought it would have been something simple... guess not :whistle:

Share this post


Link to post
Share on other sites

Im so bad at explaining things, but they make perfect sense to me :P

Ok, heres what I wanted, but I already have it done so its mandatory that you include your varaibles... But oh well Ill explain it anyway

--

Im making a media player, I wanted an easier way to handle the seek-bar, so I created this little UDF called "_GUICtrlCreateTimeSlider" which so far works fine

Now when you 'read' that slider, it would just give you a pretty random number "ex, 1205"

So I made a function that not only returns the correct time, but in HH:MM:SS

"_GUICtrlReadTimeSlider"

Now, to make things EVEN EASIER *not*, you include your 3 time variables, and it sets them for you, along with returning HH:MM:SS at the end.

This worked fine! There was 1 problem, which because Im so lazy... even though I will spend alot longer finding a faster way to do something than I could just doing it the long way (if that made sense)...

You couldnt, for example, simply use the HH:MM:SS returned (ex, Msgbox(0,"",_GUICtrlReadTimeSlider($MySlider)) - It requires the variables.

So I decide, ok Ill make them optional... = -1

Now the ByRef wont work, because ByRef's are mandatory you cant give them the 'optional' default setting...

So now its either, I only return HH:MM:SS, or I force you to include all 3 variables every time

---

Thats pretty much it, not even a big deal, I thought it would have been something simple... guess not ;)

I think I caught all that... not that I recommend this, but you could have always cheated :whistle:
_Main()

Func _Main()
    Local $MySliderInformation = _MySliderInfo($vSlider)
    ToolTip($MySliderInformation, 0, 0);or return or whatever your doing.
EndFunc

Func _GUICtrlReadTimeSlider($vSlider, ByRef $iHour, ByRef $iMin, ByRef $iSec)
    Local $hour, $min, $sec
    _TicksToTime(GUICtrlRead($vSlider) * 1000, $Hour, $Min, $Sec)
     If $iHour <> -1 Then $iHour = $hour
     If $iMin <> -1 Then $iMin = $min
     If $iSec <> -1 Then $iSec = $sec
    Return $hour & ":" & $min & ":" & $sec
EndFunc

Func _MySliderInfo($vSlider, $iHour = Default, $iMin = Default, $iSec = Default) ;Default would be whatever you wanted it to be
    Return _GUICtrlReadTimeSlider($vSlider, $iHour, $iMin, $iSec)
EndFunc

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Innovative... i like it! Ill give it a try later, it looks like it will work. Smart ^^

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0