Announcement

Collapse
No announcement yet.

Am I stupid? (I know. Loaded Question)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Am I stupid? (I know. Loaded Question)

    I have been using PBDLL and PBCC for what seems like forever.
    I consider myself pretty good. I have written numerous
    heavy duty programs that have been running for years.
    With the help of these forums, I can't even begin to count
    all the things you guys have helped me with and taught me.

    I ran into something the other day and for some reason I am
    drawing a total blank. (Blank Look On Face. Dha!!!!)
    If I have declared 2 variables:
    Dim stX as String
    Dim szY as ASCIIZ * 100

    If I want to CAST the stX variable to be a ASCIIZ all I need
    to do is:
    stX = "Something"
    szY = stX
    Now the string information is in the ASCIIZ variable.
    However, what if I want to go the other way.
    szY = "Something"
    stX = szY
    This should work and it does.
    Now the real question.
    When a function is expecting a parameter to be String but the
    variable is a ASCIIZ, how can I CAST it on the fly to be
    a String.
    Function Hello(stX as String) as Long

    ' This will result in an error. Param type mismatch
    Result = Hello(szY) << Parm should be of type String

    I know I can first move the szY into stX but the program I
    am working on would require alot of this.
    Is there any way to just convert it.
    Example: Result = Hello(Trim$(szY))
    This will work, however, any leading or trailing spaces will be
    trimmed off. NOT what I want.
    I guess the short version of this whole thing is:
    Is there a function for:
    String = MakeStringFromASCCIZ(asciiz)

    Thanks


    ------------------
    Ben Clark
    [email protected]
    If at first you don't succeed, destroy all evidence that you tried.

  • #2
    Put parenthesis around the asciiz variable. E.G.

    Code:
    '
    '  testasc.bas compile with pbdll6
    '
    #dim all
    #compile exe
    
    Sub ShowMsg(z as String)
       MsgBox z
    End Sub
    
    Function PBMain()
       
       Dim z as ASCIIZ * 100
       z = "This is a test"
       ShowMsg (z)
       
    End Function
    Best Regards,
    Don

    ------------------
    dickinson.basicguru.com
    Don Dickinson
    www.greatwebdivide.com

    Comment


    • #3
      I.o.w. use it ByVal

      I 'always' make use of strings in the parameters.
      Now you can use asciiz and strings both.

      Function Hello( ByVal s AS String ) As ...

      Had to much trouble using asciiz's as parameter(!!)


      ------------------
      hellobasic

      Comment


      • #4
        I was remiss in that there are more options ...

        using ByVal parameters
        Code:
        Sub ShowMsg(ByVal z as String)
           MsgBox z
        End Sub
        
        Function PBMain()
           
           Dim z as ASCIIZ * 100
           z = "This is a test"
           
           '- Nothing special is needed because the
           '  parameter is declared ByVal
           '
           ShowMsg z
           
        End Function
        use a ByRef parameter (default)
        Code:
        Sub ShowMsg(z as String)
           MsgBox z
        End Sub
        
        Function PBMain()
           
           Dim z as ASCIIZ * 100
           z = "This is a test"
           ShowMsg ByCopy z
           ' *OR*
           ShowMsg (z)
        End Function
        --Don

        ------------------
        dickinson.basicguru.com
        Don Dickinson
        www.greatwebdivide.com

        Comment


        • #5
          I have a related question about asciiz parameters. If there's a sub like this:

          Sub ShowMsg(z as ASCIIZ)

          and I pass it a string like this ...

          Dim s as String
          s = "This is a string"
          ShowMsg ByCopy s

          is the s guaranteed to have a trailing nul because it's passed by copy. It seems to work for me, but to be safe I usually do this ...
          ByCopy s + $nul

          Is that necessary?

          Thanks
          Don

          ------------------
          dickinson.basicguru.com
          Don Dickinson
          www.greatwebdivide.com

          Comment


          • #6
            Thanks for the quick response guys.
            Wow!


            ------------------
            Ben Clark
            [email protected]
            If at first you don't succeed, destroy all evidence that you tried.

            Comment


            • #7
              posted by don dickinson:
              is the s guaranteed to have a trailing nul because it's passed by copy. it seems to work for me, but to be safe i usually do this ...
              bycopy s + $nul

              is that necessary?
              don,

              i asked basically the same question a while ago. the answer appears
              to be that is not necessary as long as:

              1) pb continues to use windows ole string engine, and
              2) windows continues to add null bytes.

              check out: http://www.powerbasic.com/support/pb...ad.php?t=18118



              ------------------
              bernard ertl
              Bernard Ertl
              InterPlan Systems

              Comment


              • #8
                hmmm, that thread seems to indicate that it's safe to do if you trust M$ not to change what they're doing. I think I'd agree with Tom's last message and add my own $nul to the end.
                Thanks,
                Don

                ------------------
                dickinson.basicguru.com
                Don Dickinson
                www.greatwebdivide.com

                Comment


                • #9
                  I do like to write paranoid code. However, further research suggests to me
                  that adding the $NUL is probably overkill. Yes, it's conceivable that it may
                  some day be needed, but it's really pretty safe to rely on the current
                  handling. Of course, that depends on your comfort level, and adding the $NUL
                  does emphasize the ASCIIZ nature of the string, which is nice for documentation
                  purpose.

                  The bottom line is, it works either way. Do whatever suits you best.

                  ------------------
                  Tom Hanlin
                  PowerBASIC Staff

                  Comment


                  • #10
                    Don, when you write BYCOPY S + $NUL, you are creating an expression, and this is going to be passed BYCOPY anyway, so you can omit the BYCOPY keyword and receive the identical result.

                    ------------------
                    Lance
                    PowerBASIC Support
                    mailto:[email protected][email protected]</A>
                    Lance
                    mailto:[email protected]

                    Comment


                    • #11
                      Hey Lance, thanks - good point. Never thought of that even though it was sitting under my nose.

                      Best Regards,
                      Don

                      ------------------
                      dickinson.basicguru.com
                      Don Dickinson
                      www.greatwebdivide.com

                      Comment

                      Working...
                      X