Page MenuHomeFeedback Tracker

wrong Method is private errors
Assigned, UrgentPublic

Description

I wanted to make more use of the private keyword to restrict access to some of my classes methods, but I noticed, that I get a Method '...' is private quite often when (from my knowledge) this should not be the case. I found two different occurrences when I could not access a private method, even when I wanted to call it from the same class I was in:

  • have a private method in a derived class and overwrite a method from the original class and try to call the private method from the overwritten method
  • have a static method try to access a private method from the same class

Idk if that's intended, but from my view it's not how private should work. The same applies to private variables

Details

Severity
Crash
Resolution
Open
Reproducibility
Always
Operating System
Windows 11 x64
Category
General
Steps To Reproduce
class BaseClass {

    void TestMethod() {}

}
class DerivedClass : BaseClass {

    private void ThePrivateMethod() {}

    override void TestMethod() {
        ThePrivateMethod(); // Error, because the Method is private ?
    }

    static DerivedClass FactoryMethod() {
        DerivedClass instance = new DerivedClass();
        instance.ThePrivateMethod(); // Error, because the Method is private ?
        return instance;
    }

}

Event Timeline

LBmaster created this task.Aug 24 2023, 9:32 PM
Geez changed the task status from New to Assigned.Aug 25 2023, 11:37 AM

I just ran into a very weird bug which makes no sense to me and seems like a compiler bug hidden somewhere in the deep, which might even be the cause for some server crashes.
When I put this in 3_Game:

class LBConfigBase2 : Managed {

	int test = 1;
}
class LBTest2 : LBConfigBase2 {
	
	private void abc() {
		abc(); // Error Here
	}

}

I get an error at the marked position, which makes no sense. A private method should be able to call itself ^^
But now it gets even weirder. When I change the order of the classes, the error disappears. So

class LBTest2 : LBConfigBase2 {
	
	private void abc() {
		abc();
	}

}
class LBConfigBase2 : Managed {

	int test = 1;
}

compiles just fine. When I remove the private keyword alltogether, I don't get any errors anymore. Also when I removee the test variable in the LBConfigBase2 class, the error disappears

LBmaster added a subscriber: Geez.Sep 2 2024, 8:38 AM

@Geez This has been open for a year now and it seems the first bug at least got fixed, but still using private is very impractical at the moment (see the second post), especially because using getters and setters adds additional cost, because invoking a method is rather slow in DayZ and there are zero optimizations done when compiling the code

Traqu added a subscriber: Traqu.EditedSep 3 2024, 3:33 AM

Shouldn't you use protected instead?
answering to the first issue but just saw your new comment

it's in the same class. Why would I use protected there. If the TestMethod was private and I get an error, that would make sense, but not here. ThePrivateMethod is private and I call it from the same class @Traqu