VB6 - Resource with object passing to procedures

When you declare a procedure and pass an object to it, e.g. Public Sub MakeVisible(item as Object) item.visible = true end sub How does VB handle the resource? when you call the procedure Call MakeVisible(Button1) Call MakeCisible(Form1) Does it make a copy of the object and pass to the procedure, or does the ByRef default actually just pass a refernce? Its the passing a whole copy of the object that I am worried about if it does do that.

2 Replies

DS David Smith November 19, 2024 01:37 PM UTC

In VB6, when you pass an object to a procedure, such as in your example Public Sub MakeVisible(item As Object), VB6 passes a reference to the object by default (ByRef). This means the procedure operates on the original object without creating a full copy, ensuring efficient memory use.

Explanation:

  1. ByRef Default Behavior:

    • In VB6, parameters are passed ByRef by default unless explicitly declared with ByVal.
    • Passing Button1 or Form1 to MakeVisible(item As Object) means the procedure receives a reference to these objects. Changes to their properties or methods in the procedure directly affect the original objects.
  2. No Full Object Copy:

    • VB6 does not create a deep copy of the object when passed as a parameter. Even if you use ByVal, only the reference itself is copied, not the underlying object. This ensures changes to the object's properties, like setting item.Visible = True, modify the actual instance.
  3. Efficiency and Risks:

    • This behavior optimizes resource use but requires careful handling. For instance, unintended changes to an object's state persist outside the procedure, which may lead to side effects.

Related Insight:

This concept is particularly relevant in VB6 to .NET conversion, where VB6's ByRef mechanism contrasts with .NET's enhanced type-safety and explicit reference-passing features. In .NET, you can use ref or out parameters in C#, or manage object references explicitly in VB.NET to align with VB6's behavior. Understanding how VB6 handles objects is crucial to ensuring a seamless migration process, avoiding pitfalls like unexpected behavior due to default reference-passing mechanisms​



MS Malini Selvarasu Syncfusion Team November 20, 2024 01:46 PM UTC

Hi ,

We believe that the previous update should have resolved your issue. If you encounter any new issues, please feel free to create a new forum, and we will be happy to assist you.
Regards,
Malini Selvarasu

Loader.
Up arrow icon