My private class field is accessible from outside class?

I’ve tested this in Mercury and Oxygen, using Fire.

This is the example (Mercury) code I used:

Module Program

  Sub Main(args as String())
    Dim test As New TestClass
    test.test1 = "456"
    Console.WriteLine(test.test1)
  End Sub
  
  Public Class TestClass
    Private test1 As String
    Public test2 As String
  End Class

End Module

This shouldn’t be working, but it does. The code is working and the output shows “456”. I only get a warning saying “Did you mean test2?”.

In VS this would result in an expected error. What am I missing here?

I believe VB uses a lighter visibility model, where “Private” is visible in the entire file. @Theo69, is that correct?

That said, this case in particular one is further complicated by the fact that TestClass is a nested class inside the program class (modules are static classes); that might affect this, too…

—marc

I had the same problem using Oxygen, where I used a separate class file; so in that case not nested.

I never had this problem with VB using VS.

NB: Autocomplete doesn’t show the private members, but I can still ignore autocomplete and just enter/user the private member.

Can I see a full testcase for this? I cannot reproduce that:

namespace ClassLibrary24;

type
  Class1 = public class
  private
    fFoo: String;
  protected
  public
  end;

  Class2 = public class
  private
  protected
  public
    
    method x;
    begin
      var c := new Class1;
      c.fFoo := "Test"; // E470 member "fFoo" on type "Class1" cannot be called because it is private
    end;
    
  end;

end.

fails as expected…

I can’t reproduce it either now. I made a test in Oxygen after I found the problem in Mercury. Sadly, I don’t have that test project anymore. I’m sure I’m did something wrong/stupid. It was late.

So for now the problem seems to be only in Mercury (.NET Core).

I thought that too, but I think that changed somewhere in the history.
In the current VB (2015 and highter), private is real private.

I could be wrong, but in my memory it was always the case in .NET. Maybe it was different in my good friend VB6?

Could be. I don’t have any older VB versions installed at this moment to be able to test.

Maybe you had Delphi Compatibility turned on? in Delphi, “private” means “unit”.

We’ll get that fixed then, thanx!

1 Like

Thanks, logged as bugs://84871

Don’t know. Not deliberately at least … :slight_smile:

I looked that setting up just now, and indeed turning it on would make it work (not being treated as really private).

Anyhow, forget about Oxygen. If I encountered it again I’ll try to keep the test project …

2 Likes

bugs://84871 got closed with status fixed.