CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2000
    Location
    Birmingham, England
    Posts
    2,520

    Passing by reference

    Hi

    I'm updating a vb class dll. One of the functions needs to take another parameter (integer) which it places an error value into. The function that called it then has access to the error code. This is what i have :



    Dim myProposal as Proposal, myAddnWork as AddWorkItems, _
    DropPointName$, _
    ShippingRouteName$
    Dim errVal as Integer

    ' Validate drop point
    If false = ValidDropPoint(sDropPointCiCode, me.FranchiseCode, DropPointName$, errVal) then
    If errVal = 1 then
    Err.Raise errorDropPointInvalid
    else
    If errVal = 2 then
    Err.Raise errorNoPrimaryDepot
    End If
    End If
    End If





    - and that function is declared as :



    public Function ValidDropPoint(byval strCiCode as string, byval strFranchCode, byref strName as Variant, byval errorValue as Integer) as Boolean
    Dim key as new SPKEYLib.CKey
    key.AddElement "P_CI_CODE", EKO_EQ, strCiCode
    key.And
    key.AddElement "P_FRANCH_CODE", EKO_EQ, strFranchCode

    ' Create new entity
    Dim stgcont as DATASTORAGELib.CCoContainerStg
    set stgcont = m_Dasp.CreateEntity(ENTITY_CUSTOMER_CI_CODE)
    Dim itemStg as IAccessDataStg
    set itemStg = stgcont.GetRootObjectStg

    Dim col as string
    Dim bResult as Boolean

    ' Invoke the function
    bResult = m_Dasp.Invoke(1, key, itemStg)
    col = itemStg.GetColumn("dp_type")

    Select Case col
    Case "A"
    errorValue = 1 ' Invalid Drop Point
    ValidDropPoint = false
    Case "B"
    errorValue = 2 ' Unable to Establish Primary Depot
    ValidDropPoint = false
    Case "C"
    errorValue = 0 ' No error
    ValidDropPoint = true
    strName = itemStg.GetColumn("drop_point_cust_name")
    Case else
    errorValue = 1 ' Invalid Drop Point
    ValidDropPoint = false
    End Select

    set itemStg = nothing
    set stgcont = nothing
    End Function





    With this code, the ValidDropPoint() function sets the value of errorValue correctly. However, upon return from the function, the value in errVal is always 0. Now i figured this would be because i am passing errVal 'ByVal', when i should be passing it ByRef - however, when i pass it ByRef(), the function generates a 'type mismatch' runtime error.

    Either this is because the call and the declaration of the function do not match when chnaging the declaration to ByRef, or that when i set errorValue to 0, 1 or 2, i am not doing this correctly for a an int that has been passed ByRef.

    I reckon this is about as basic a question as i could ask, but without knowing the fundamentals (i'm a vc++ developer) i'm hopelessly stuck.

    Please ... any ideas rewarded ...

    Jase



    <no witty trailer supplied>

    Jase

    www.slideshowdesktop.com
    View your images and photos on your desktop with ease using SlideShow Desktop, the desktop wallpaper manager for Microsoft Windows.
    ...

  2. #2
    Join Date
    Sep 1999
    Posts
    202

    Re: Passing by reference

    >the value in errVal is always 0. Now i figured this would be because i am passing errVal 'ByVal', when i should be passing it ByRef

    Correct, pass it ByRef.


    > - however, when i pass it ByRef(), the function generates a 'type mismatch' runtime error.

    I can see 'type mismatch' only with the third argument:
    third argument is 'byref strName as Variant', and you are passing string:
    Dim DropPointName$ ' this is a string


  3. #3
    Join Date
    Mar 2000
    Location
    Birmingham, England
    Posts
    2,520

    Re: Passing by reference

    Thanks Bruno,

    You're right. I have changed it to by ByRef now and it works fine. The runtime errors had been caused by me using


    byref errorValue as Integer





    it works if i do this :



    byref errVal





    not sure why i have to drop the integer part, but i do. Also, is the naming it errVal rather than errorValue important ? It is errVal in the function call also ...

    Thanks for the tip on the variant $ argument. This is legacy code i have inherited from another software company and i haven't written in vb before, so thanks for pointing out potential problems. I shall look into it.

    Jase

    <no witty trailer supplied>

    Jase

    www.slideshowdesktop.com
    View your images and photos on your desktop with ease using SlideShow Desktop, the desktop wallpaper manager for Microsoft Windows.
    ...

  4. #4
    Join Date
    Sep 1999
    Posts
    202

    Re: Passing by reference

    Jase, I am not sure you did it right. You should try NOT to use Variant variables for strings and numbers, but you should use specific data types (String, Integer, Long,...) instead.

    You should always try to use THE SAME data type - if some function argument is Integer, send Integer when calling the function.

    What happens if you DON'T use THE SAME data type:
    1. If passing is ByRef, you'll get compiler error
    2. If passing ByVal (one way), compiler will automatically insert conversion for you. (IOW, ByVal allows very bad code, e.g. passing strings when you need integer, or passing integer when argument should be string.)
    'Few declarations:
    Dim x As String ' string
    Dim y$ ' string (old style)
    Dim y& ' long (old style)
    Dim y% ' integer (old style)
    Dim y ' variant
    Dim x, y, z As String ' 2 variants and 1 string




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured