GithubHelp home page GithubHelp logo

ByRef Me for Structure about vblang HOT 3 OPEN

rskar-git avatar rskar-git commented on August 20, 2024 1
ByRef Me for Structure

from vblang.

Comments (3)

VBAndCs avatar VBAndCs commented on August 20, 2024

I like this.
I previously proposed to allow us to optionally add ByRef to ref arguments for better clarity of code. But, I don't think both proposals will happen as they need language changes which is not valid now.

from vblang.

DualBrain avatar DualBrain commented on August 20, 2024

I would argue that the underlying behavior is broken (or the documentation is, at the very least, misleading)...

Module ByrefTest

  Structure PDQ
    Public P As Integer
    Public D As Decimal
    Public Q As Long
    Sub FooBar()
      ByrefTest.FooBar(Me)
    End Sub
  End Structure

  Sub FooBar(ByRef dst As PDQ)
    dst.P = 123
  End Sub

  Sub Main()
    Dim o As PDQ = Nothing
    o.FooBar()
    ' Prints 0... expecting it to be 123.
    Console.WriteLine($"{o.P}")
  End Sub

End Module

I slightly modified the original code to reflect the point that, according to the documentation found at:

https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/byref?f1url=%3FappId%3DDev16IDEF1%26l%3DEN-US%26k%3Dk(vb.ByRef);k(DevLang-VB)%26rd%3Dtrue

It states that the ByRef keyword "specifies that an argument is passed in such a way that the called procedure can change the value of a variable underlying the argument in the calling code."

I don't see the need to have to specify ByRef in both places. If the method specifies that it is ByRef that should be enough. If you wanted to force it to be ByVal (for whatever reason), we already have a mechanism in place to override that behavior. The problem as I see it is that the documentation suggests one thing but the implementation clearly doesn't follow what the docs specify.

from vblang.

DualBrain avatar DualBrain commented on August 20, 2024

To further illustrate why I believe this is a bug...

Module ByrefTest

  Structure PDQ
    Public P As Integer
    Public D As Decimal
    Public Q As Long
    Sub FooBar()
      ByrefTest.FooBar(Me)
    End Sub
  End Structure

  Sub FooBar(ByRef dst As PDQ)
    dst.P = 123
  End Sub

  Sub Main1()
    Dim o As PDQ = Nothing
    o.FooBar() ' Call FooBar within the structure...
    Console.WriteLine($"{o.P}") ' Output: 0
    FooBar(o) ' Call FooBar outside of the structure...
    Console.WriteLine($"{o.P}") ' Output: 123
  End Sub

End Module

If I modify the sample so that FooBar is called slightly differently, I get two different results... event though I'm essentially executing the same code. The only difference is the usage of Me.

from vblang.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.