Private vs. protect

This forum is meant for questions and discussions about the X# language and tools
Post Reply
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Private vs. protect

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
lumberjack
Posts: 727
Joined: Fri Sep 25, 2015 3:11 pm
Location: South Africa

Private vs. protect

Post 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#?
______________________
Johan Nel
Boshof, South Africa
User avatar
orangesocks
Posts: 40
Joined: Thu Nov 12, 2015 2:41 pm

Private vs. protect

Post 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
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Private vs. protect

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Private vs. protect

Post by wriedmann »

Hi Robert,

thank you very much for this clarification.
I will change my code accordingly (replace "private" by "hidden").
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Chris
Posts: 4910
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Private vs. protect

Post by Chris »

Wolfgang, PRIVATE should work just fine, too.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Private vs. protect

Post by wriedmann »

Hi Chris,

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

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Private vs. protect

Post by FFF »

+1
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
Post Reply