experchange > scripting.vbscript

R.Wieser (05-09-19, 09:23 AM)
Hello all,

I'm using timestamps and wanted to display them in a certain way. Though
when I took a look at "FormatDateTime" I found I could only have it return
one of five pre-defined formats, none of which served my purpose. So I
wrote my own, using a formatting string.

The thing is, the name of the system function is quite appropriate for what
my function is doing: FormatDateTime.

tl;dr:
Can I have the VBScript engine pick the right function depending on the
types of the provided arguments ?

I don't think its possible, but it doesn't hurt to ask. :-)

Regards,
Rudy Wieser
GS (05-09-19, 11:20 AM)
> Hello all,
[..]
> I don't think its possible, but it doesn't hurt to ask. :-)
> Regards,
> Rudy Wieser


Whenever I name a VB UDF after an existing one I use an underscore like so...

Public Function Format_DateTime$(iFormat%)

...where iFormat specifies the index of the pre-defined (string) formats to use
according to their (named) position in an Enum. You can remove the Type symbols
for use with VBS.
Evertjan. (05-09-19, 11:39 AM)
"R.Wieser" <address> wrote on 09 May 2019 in
microsoft.public.scripting.vbscript:

> Hello all,
> I'm using timestamps and wanted to display them in a certain way.
> Though when I took a look at "FormatDateTime" I found I could only have
> it return one of five pre-defined formats, none of which served my
> purpose. So I wrote my own, using a formatting string.
> The thing is, the name of the system function is quite appropriate for
> what my function is doing: FormatDateTime.
> tl;dr:
> Can I have the VBScript engine pick the right function depending on the
> types of the provided arguments ?


Sorry, the "the VBScript engine" just executes VBScript.

> I don't think its possible, but it doesn't hurt to ask. :-)


However you can skip the MS-inbuild ideas and build your own:

function dattime(x)
dattime = two(day(x))&"-"&two(month(x))&"-"&year(x)&" "&two(hour(x))
&":"&two(minute(x))
end function

function two(x)
two = right("0" & x, 2)
end function
R.Wieser (05-09-19, 11:44 AM)
GS,

> Whenever I name a VB UDF after an existing one I use an underscore like
> so...


Yes, naming the function differently is ofcourse the easiest solution.
Currently I've named my function "GetDateFormatted" - which does pretty much
cover what its doing.

But I really would like to know if VBScript knows (how to handle) function
overloading.

.... Or even if it knows how to replace a system function and still be able
to call the replaced one (something I saw used in another langueage).

Regards,
Rudy Wieser
GS (05-09-19, 08:33 PM)
> GS,
> Yes, naming the function differently is ofcourse the easiest solution.
> Currently I've named my function "GetDateFormatted" - which does pretty much
> cover what its doing.
> But I really would like to know if VBScript knows (how to handle) function
> overloading.
> ... Or even if it knows how to replace a system function and still be able to
> call the replaced one (something I saw used in another langueage).
> Regards,
> Rudy Wieser


My impression is that MS is looking to deprecate VBS in favor of something more
suitable for web apps and so I doubt they'll want to 'upgrade' VBS with
function/method overloading features. Home-grown is the way to go as I see it!

Now you can build your own overloaded functions for C# (and some of the other
languages), but not to work/run like a VBS script.
R.Wieser (05-10-19, 09:15 AM)
GS,

> My impression is that MS is looking to deprecate VBS in favor of something
> more suitable for web apps and so I doubt they'll want to 'upgrade' VBS
> with function/method overloading features.


I think there is some kind of misunderstanding here. I just wanted to know
if its (or something like it) available /now/.

> Home-grown is the way to go as I see it!


So ? Any other methods available to create the same effect (even just to
see if its possible) ?

Regards,
Rudy Wieser
Mayayana (05-10-19, 02:41 PM)
"R.Wieser" <address> wrote

| So ? Any other methods available to create the same effect (even just to
| see if its possible) ?
|

VB has optional parameters, but not VBS. Personally
I think it's a very dumb idea, lending itself to errors.
So I've never actually used an optional parameter.
But that's just my preference.

I have used multi-purpose functions. For instance:

ProcessThisText(sTheText, TheTarget)

Where TheTarget would probably be a numeric value.
For instance, if I needed to same function to prepare
text strings for multiple text windows, TheTarget could
be used to specify which window it goes to.

I'm curious what you want to do. It's hard for me
to imagine a necessary purpose for such tricky ambiguity
in function design.
R.Wieser (05-10-19, 06:11 PM)
Mayayana,

> I'm curious what you want to do.


In that case I would suggest you (re)read my initial message. I explained
it there.

But in short: To add functionality to an already existing function. In this
case, a formatting string to complement a few numeric options.

> It's hard for me to imagine a necessary purpose
> for such tricky ambiguity in function design.


:-) Than I hope you never try your hand at a language like one of the C
{something} variants, as they are rather common there.

But to give an example:
FormatDateTime(date, value) 'system function
FormatDateTime(date, formatting string) 'my function

Regards,
Rudy Wieser
GS (05-10-19, 07:13 PM)
> But to give an example:
> FormatDateTime(date, value) 'system function
> FormatDateTime(date, formatting string) 'my function


This is what I was trying to pass along!
Mayayana (05-10-19, 07:21 PM)
"R.Wieser" <address> wrote

| But to give an example:
| FormatDateTime(date, value) 'system function
| FormatDateTime(date, formatting string) 'my function
|

I guess I don't get it. Overloading would be to
have one function work differently depending on
parameters. You're just asking if you can use
an existing keyword or function name and somehow
hope that WSH doesn't notice.
GS (05-10-19, 07:28 PM)
> GS,
>> My impression is that MS is looking to deprecate VBS in favor of something
>> more suitable for web apps and so I doubt they'll want to 'upgrade' VBS
>> with function/method overloading features.

> I think there is some kind of misunderstanding here. I just wanted to know
> if its (or something like it) available /now/.


My reply to your original post suggested 1 possible approach for "something
like it"!
>> Home-grown is the way to go as I see it!

> So ? Any other methods available to create the same effect (even just to
> see if its possible) ?

In reference to my suggestion to pass an index value, the function would
process that via a Select Case struct to return the desired format from a
single function.

I'm a big fan of developing reusable code! That said, this approach will work
in any programming language, including those that support overloading.
Adversely, code written in languages that support overloading will need to be
re-written to be usable in those (like VB_) that don't support it.
GS (05-10-19, 08:06 PM)
> "R.Wieser" <address> wrote
> I guess I don't get it. Overloading would be to
> have one function work differently depending on
> parameters. You're just asking if you can use
> an existing keyword or function name and somehow
> hope that WSH doesn't notice.


Overloading in the current C languages supports multiple methods with the same
name but different parameters; - which combo of params you use determines which
version the compiler uses.

I've been doing similar in VB_ projects for years by passing an action index to
a single function to tell it what to do; - the function may also have other
args but the only one I use for Format_DateTime() is an index to tell it which
date or time format to return.

I'm in the process of converting all my stuff to C# and so far all is going
well. While it's possible to convert my multi-task functions to overloaded
ones, I don't yet see their point (other than to simplify naming and persist
further support of unnecessary bloating)! Single multi-task functions use
'Case' branching for actions to take for a common purpose. For example, my
GetInfo_FromWMI() function contains all the code necessary for all the various
WMI queries I use for my licensing methodology to return a delimited string for
a license profile MachineID property consisting of:

Make, Model, Mainboard SerialNumber,BIOS SerialNumber

The process involves using GoTo statements to control the order the info is
collected from the various WMI classes when the 'Case' statements don't
necessarily follow any order. This has been working well for 15+ years and so I
don't see any point in 'breaking' it just because the .NET Framework supports
the 'new and improved' feature of method overloading.
R.Wieser (05-10-19, 08:46 PM)
Mayayana,

> You're just asking if you can use an existing keyword or
> function name and somehow hope that WSH doesn't notice.


Nope. The whole idea is that it /does/ notice, but will call the correct
function depending on the ammount and types of used arguments.

In the example I gave I expect the first call to go to the system function
as it expects a number as the second argument and the second one to mine as
it expects a string there.

Regards,
Rudy Wieser
R.Wieser (05-10-19, 08:52 PM)
"GS" <gs> wrote in message news:qp51

Garry, you should be carefull how you're quoting.

In the above I said whats behind the ">>>" prefixes, Mayayana said whats
behind the ">>" ones.

The way you made it look is that someone you didn't mention said whats
behind the ">>>" prefixes, and /I/ said whats behind the ">>" ones. :-(

Regards,
Rudy Wieser
R.Wieser (05-10-19, 08:57 PM)
GS,

>> But to give an example:
>> FormatDateTime(date, value) 'system function
>> FormatDateTime(date, formatting string) 'my function

> This is what I was trying to pass along!


WHAT where you trying to pass along ?

Because when I declare /my/ "FormatDateTime" function I will lose access to
the system function. Something I do NOT want. Which I tried to make
clear in my second post with "and still be able to call the replaced one".

So, all I need now is how to call the origional, replaced function. Can
you at least pass /that/ along ? 'Cause without it being able to replace
a(n origional) system function is /absolutily worthless/ to me.

Regards,
Rudy Wieser