New logging patterns a new function what is wrong with-VBForums
Results 1 to 14 of 14

Thread: New logging patterns a new function what is wrong with

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    207

    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

  2. #2
    Addicted Member
    Join Date
    Apr 2017
    Posts
    181

    Re: New logging patterns a new function what is wrong with

    Here's a little advise. Stop using + for string concatenations. Use the &

  3. #3
    Addicted Member
    Join Date
    Apr 2017
    Posts
    181

    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

  4. #4
    Addicted Member
    Join Date
    Aug 2017
    Location
    South Africa (in the middle)
    Posts
    159

    Re: New logging patterns a new function what is wrong with

    Quote Originally Posted by Code Dummy View Post
    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

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,154

    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.

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    3,982

    Re: New logging patterns a new function what is wrong with

    Quote Originally Posted by Inside View Post
    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?

  7. #7
    Addicted Member
    Join Date
    Aug 2017
    Location
    South Africa (in the middle)
    Posts
    159

    Re: New logging patterns a new function what is wrong with

    Quote Originally Posted by passel View Post
    ...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

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,154

    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.

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    3,112

    Re: New logging patterns a new function what is wrong with

    Quote Originally Posted by passel View Post
    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 04:31 PM.

  10. #10
    Addicted Member
    Join Date
    Apr 2017
    Posts
    181

    Re: New logging patterns a new function what is wrong with

    Quote Originally Posted by Inside View Post
    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

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,154

    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.

  12. #12
    Addicted Member
    Join Date
    Aug 2017
    Location
    South Africa (in the middle)
    Posts
    159

    Re: New logging patterns a new function what is wrong with

    Quote Originally Posted by Code Dummy View Post
    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

  13. #13
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    571

    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.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    207

    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.