xsharp.eu • Private vs. protect
Page 1 of 1

Private vs. protect

Posted: Mon Jun 17, 2019 5:32 am
by wriedmann
Hello,
when moving C# code to X# I have encountered something I don't understand.
AFAIK the "private" statement in C# is to translate to "protected" in X# because "private" has another meaning in xBase.
But the following code does not compiles in X# (Core):

Code: Select all

static class MyClass
static protected MyVar as string
static protected method MyMethod() as void
	return
end class
as the compiler says

Code: Select all

warning XS1057: 'MyClass.MyVar': static classes cannot contain protected members
warning XS1057: 'MyClass.MyMethod()': static classes cannot contain protected members
But when I change the code to

Code: Select all

static class MyClass
static private MyVar as string
static private method MyMethod() as void
	return
end class
it compiles....
Wolfgang

Private vs. protect

Posted: Mon Jun 17, 2019 5:57 am
by lumberjack
Hi Wolfgang,
wriedmann wrote:when moving C# code to X# I have encountered something I don't understand.
AFAIK the "private" statement in C# is to translate to "protected" in X# because "private" has another meaning in xBase.
But the following code does not compiles in X# (Core):

Code: Select all

static class MyClass
static protected MyVar as string
static protected method MyMethod() as void
	return
end class
as the compiler says

Code: Select all

warning XS1057: 'MyClass.MyVar': static classes cannot contain protected members
warning XS1057: 'MyClass.MyMethod()': static classes cannot contain protected members
But when I change the code to

Code: Select all

static class MyClass
static private MyVar as string
static private method MyMethod() as void
	return
end class
it compiles....
From the help file:

Code: Select all

The PRIVATE statement is NOT available in the Core and Vulcan dialects and will only be enabled if the -memvar commandline option is specified
I think there was changes to support VFP and other languages. Is the c# private not hidden in X#?

Private vs. protect

Posted: Mon Jun 17, 2019 5:57 am
by orangesocks
Hi,
I think that you cannot use the protected keyword with static classes because they cannot be inherited like normal classes.
See here https://stackoverflow.com/questions/774 ... ic-classes
In my understanding the protected keyword is used to limit the visibility of a variable/method only to derived classes.
HTH
Regards Giuseppe

Private vs. protect

Posted: Mon Jun 17, 2019 6:03 am
by robert
Wolfgang,
wriedmann wrote:Hello,
when moving C# code to X# I have encountered something I don't understand.
AFAIK the "private" statement in C# is to translate to "protected" in X# because "private" has another meaning in xBase.
The X# equivalent of PRIVATE is HIDDEN but we also allow PRIVATE. The remark about PRIVATE in the documentation relates to the private STATEMENT and NOT the private visibility MODIFIER for instance variables/fields and methods.

PROTECTED means that a method or field can also be seen by subclasses, but you can't inherit from STATIC classes so that is meaningless for a static class.

Robert

Private vs. protect

Posted: Mon Jun 17, 2019 6:08 am
by wriedmann
Hi Robert,

thank you very much for this clarification.
I will change my code accordingly (replace "private" by "hidden").
Wolfgang

Private vs. protect

Posted: Mon Jun 17, 2019 11:24 am
by Chris
Wolfgang, PRIVATE should work just fine, too.

Private vs. protect

Posted: Mon Jun 17, 2019 11:45 am
by wriedmann
Hi Chris,

yes, it compiled too, but for better documentation I prefer to use "hidden".

Wolfgang

Private vs. protect

Posted: Mon Jun 17, 2019 1:40 pm
by FFF
+1