-
Sep 9th, 2017, 11:09 AM
#1
Thread Starter
Addicted Member
New logging patterns a new function what is wrong with
Hi to all. Today I was dealing with new signature patterns for my new project logs. I sent the code below. I need a professional advice for my projects sake. I am currently dealing with my new project making me more insane for code accomplishment time I have restricted time but need for some assist… Just sharing my code now:
Code:
Public Function ReturnLogStream(Optional pType As Integer = 0) As String
Dim retvalue As String
Dim mm As Integer
Dim dd As Integer
Dim yyyy As Integer
Dim hh As Integer
Dim min As Integer
Dim msec As Integer
Dim d As Date
Dim t As Double
Dim tt As Integer
d = Now
mm = Month(d)
dd = Day(d)
yyyy = Year(d)
hh = Hour(d)
min = Minute(d)
t = Timer
tt = Val(Mid(Str(t), InStr(Str(t), ".") + 1))
msec = tt
Select Case pType
Case 0:
retvalue = Trim(GetNValueVB6(dd, 2)) + "." + Trim(GetNValueVB6(mm, 2)) + "." + Trim(Str(yyyy)) + " " + Trim(GetNValueVB6(hh, 2)) + Trim(GetNValueVB6(min, 2)) + "." + Trim(GetNValueVB6(msec, 3))
Case 1:
retvalue = Trim(GetNValueVB6(mm, 2)) + "/" + Trim(GetNValueVB6(dd, 2)) + "/" + Trim(Str(yyyy)) + " " + Trim(GetNValueVB6(hh, 2)) + Trim(GetNValueVB6(min, 2))
Case 2:
retvalue = Trim(Str(yyyy)) + Trim(GetNValueVB6(mm, 2)) + Trim(GetNValueVB6(dd, 2)) + Trim(GetNValueVB6(hh, 2)) + Trim(GetNValueVB6(min, 2)) + Trim(GetNValueVB6(msec, 3))
Case Else:
End Select
ReturnLogStream = retvalue
End Function
Public Function GetNValueVB6(ByVal pValue As Integer, ByVal pDigit As Integer) As String
Dim s As String
Dim l As Integer
l = pDigit - Len(Trim(Str(pValue)))
If l > 0 Then
s = String(l, "0") + Trim(Str(pValue))
ElseIf l = 0 Then
s = Trim(Str(pValue))
ElseIf l < 0 Then
s = "e" + Trim(Str(pValue))
End If
GetNValueVB6 = s
End Function
Regards
algea
-
Sep 9th, 2017, 11:30 AM
#2
Fanatic Member
Re: New logging patterns a new function what is wrong with
Here's a little advise. Stop using + for string concatenations. Use the &
-
Sep 9th, 2017, 11:34 AM
#3
Fanatic Member
Re: New logging patterns a new function what is wrong with
Here's another. Quit using Trim at every call to GetNValueVB6. Make sure that the function returns a value that doesn't need to be trimmed
-
Sep 9th, 2017, 12:19 PM
#4
Addicted Member
Re: New logging patterns a new function what is wrong with
Originally Posted by Code Dummy
Here's a little advise. Stop using + for string concatenations. Use the &
In some cases the answer will be different when use a "&" instead of a "+".
In concatenation, yes, but not when adding.
When use "+" as a universal, the programmer almost never made a mistake.
I do not see any speed issues or memory loss etc. I think it's rather a personal preference.
Programmers are very patient people....while programming....but don't expect them to be patient all the time
-
Sep 9th, 2017, 12:39 PM
#5
Re: New logging patterns a new function what is wrong with
It would help if the OP would give us some idea of what problem he/she is experiencing.
What output is the code generating vs what output is expected.
My guess is that it is a simple fix but I do not want to waste my time trying to figure out what type of problem is there when the OP could have stated that at the beginning and made our job easy.
-
Sep 9th, 2017, 02:50 PM
#6
Re: New logging patterns a new function what is wrong with
Originally Posted by Inside
In some cases the answer will be different when use a "&" instead of a "+".
In concatenation, yes, but not when adding.
When use "+" as a universal, the programmer almost never made a mistake.
I do not see any speed issues or memory loss etc. I think it's rather a personal preference.
You may not see any speed issue, but using "+" to concatenate strings is technically wrong.
"+" is for addition (i.e. summing) and "&" is for string concatenation in this context.
VB will compensate for the programmer using the wrong operator, "+", by calling extra functions to see if the two strings arguments to the operator can be converted to numerical values or not, and then choose to either do a number of conversions, a summation, and another conversion or to concatenate the two strings.
If a, b and c are strings and you have the line:
a = b & c
then VB allocates the memory for a to hold the strings b and c and copies them to a and you're done.
If you have the line:
a = b + c
then VB has to loop through the characters in b to see if b can be converted to a number.
If b can be converted to a number, then VB saves that number and loops through the characters in c to see if it can be converted to a number.
If c can be converted to a number, then VB adds the numbers from the b and c conversions together, then calls another function to convert the resulting number back to a string and assigns it to a.
If you meant to concatenate two strings containing digits then using "+" would not do what you want.
If b or c were not convertible to numbers so you know that "+" will concatenate, VB of course wouldn't know that until it tried to do a numerical conversion first and failed so will waste some time every time you use "+" to concatenate.
VB probably shouldn't be making these choices for you when you use the wrong operator, but it was designed early on to be "easier" by rather than failing, make an assumption and either add or concatenate based on the content of the string.
Having to examine the contents of the strings to make this decision adds extra work and is what makes using "+" to concatenate a wrong choice for general efficiency. The programmer using the wrong operator and counting on VB to make a reasonable choice might be just an example of ignorance rather than an example of a personal choice. Why would someone chose for VB to do extra work for every concatenation?
-
Sep 9th, 2017, 02:59 PM
#7
Addicted Member
Re: New logging patterns a new function what is wrong with
Originally Posted by passel
...VB probably shouldn't be making these choices for you when you use the wrong operator, but it was designed for...
un-knowledge'd people like me
Programmers are very patient people....while programming....but don't expect them to be patient all the time
-
Sep 9th, 2017, 03:05 PM
#8
Re: New logging patterns a new function what is wrong with
I would also say stop using Trim() and Str()
Use CStr() instead of those two when needed.
Also never use Trim() when Trimming is needed use Trim$() instead
Be aware however that Str(3) will result in " 3" which I assume in why you are using Trim() on it to get rid of that leading space.
CStr(3) will return "3" and no need to use Trim() or Trim$() on it.
Also if you are expecting only leading spaces you should use LTrim$() rather than Trim() or Trim$()
That said I do not see the logic in the functions anyway. A simple Format$() would make more sense.
-
Sep 9th, 2017, 03:22 PM
#9
Re: New logging patterns a new function what is wrong with
Originally Posted by passel
it was designed early on to be "easier" by rather than failing
I would argue that it was designed that way to be compatible with earlier (VB-DOS, GWBasic, PDS-Basic, etc) code that didn't have the "&" operator. Let's not forget that VB6 has a rich history, and does a phenomenal job of being backward compatible with an extremely strong compatibility rating all the way back to its origins.
And sure, when Altair BASIC was originally written, they just didn't have the foresight to make every operator perfectly copacetic with the way it should be used. But they did a pretty darned good job.
So, I do applaud Microsoft for working hard to stay backward compatible through VB6. Furthermore, I applaud them for many cleanups in the language to make things work as they should, such as the addition of the "&" operator.
I must say that, early on, I had quite a few "+" operators for string concatenation, just because so much of my code was "brought forward". However, these days, I wouldn't dare imagine using "+" for string concatenation, and that's been cleaned up on every line of my code many years ago.
Basically, I agree with Passel (and others), that it's not appropriate to use the "+" operator for string concatenation. It's confusing, and should be avoided.
Regarding the OP's use of Trim(), the primary thing that I'd recommend is the use of Trim$() rather than Trim(). That way, you'd avoid a great deal of typecasting in and out of variants to strings. In fact, using Trim() (no $) just further confuses the use of the "+" operator, because the "+" will work on variants quite differently depending on their internal type.
Best Regards,
Elroy
EDIT1: Here's a test, and try not to cheat. What's each of the following Message Boxes going to report? This is an illustration of why keeping "&" and "+" separate is strongly advised:
Code:
Option Explicit
Private Sub Form_Load()
Dim v1 As Variant
Dim v2 As Variant
Dim v3 As Variant
Dim v4 As Variant
v1 = 3
v2 = 4
v3 = CStr(v1)
v4 = CStr(v2)
MsgBox v1 + v2
MsgBox v3 + v4
MsgBox v1 + v4
MsgBox v3 + v2
MsgBox v1 & v2
End Sub
Last edited by Elroy; Sep 9th, 2017 at 03:31 PM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Sep 9th, 2017, 03:25 PM
#10
Fanatic Member
Re: New logging patterns a new function what is wrong with
Originally Posted by Inside
un-knowledge'd people like me
I hope you now understand as I have seen where you have made issues about + vs & on other threads in preference to the + sign
-
Sep 9th, 2017, 08:31 PM
#11
Re: New logging patterns a new function what is wrong with
There was a coder where I used to work back in the 90s who when he first heard that & was the new preference for strings without thinking did a global replace of + with &. Needless to say it completely hosed the project. Good thing there was a backup.
-
Sep 9th, 2017, 11:11 PM
#12
Addicted Member
Re: New logging patterns a new function what is wrong with
Originally Posted by Code Dummy
I hope you now understand as I have seen where you have made issues about + vs & on other threads in preference to the + sign
I do.
Something I have to implement in future use of programming.
Perfect coding have a long learning curve. Especially where everyone agree on a standard.
Programmers are very patient people....while programming....but don't expect them to be patient all the time
-
Sep 11th, 2017, 11:24 AM
#13
Re: New logging patterns a new function what is wrong with
Do you not get the same results from these?
Code:
? Format$(Now, "dd/mm/yyyy HH:nn:ss.fff" )
? Format$(Now, "mm/dd/yyyy HH:nn:ss.fff" )
? Format$(Now, "yyyymmddHHnnssfff" )
Sorry but, to me, this looks like a lot of Reinventing the Wheel.
Regards, Phill W.
-
Sep 15th, 2017, 10:28 AM
#14
Thread Starter
Addicted Member
Re: New logging patterns a new function what is wrong with
Dear all,
Thanks for your advice. I saw my mistake inside the code. I have an optimization error. Simon says that I am only a coder not a programmer. Possibly joining inside your classroom with new mates would gain new experience. Sorry for my delay for the next message… Many thanks
Yours faithfully;
Regards
algea
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|