473,747 Members | 2,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

?? 'new' and 'protected' Modifiers on Structs ??

Hi everyone,

Has anyone looked at section 18.1.1 of the C# spec? It indicates 'new' and
'protected' are valid modifiers on struct declarations. First, how can
'protected' be valid on a struct, since structs cannot be inherited? The
compiler gives an error (as I expect it should) if you try this:

protected struct MyStruct {}

so I'm wondering if the spec is wrong when it says 'protected' is a valid
struct-modifier.

The second point about section 18.1.1 is that it indicates 'new' is an
allowable modifier on a struct. Actually, 18.1.1 indicates, "The modifiers
of a struct declaration have the same meaning as those of a class
declaration", referring the reader to section 17.1.1 on class declaration
modifiers.

I looked at section 17.1.1 and sure enough, it says:

"The new modifier is permitted on nested classes.
It specifies that the class hides an inherited member
by the same name, as described in section 17.2.2. It
is a compile-time error for the new modifier to appear
on a class declaration that is not a nested class declaration."

My testing indicates 'new' is not allowed on class nor struct declarations
(nested or not). It seems this is how 'new' should be used as a class
modifier, according to the spec (sec. 17.1.1):

class MyClass {
public virtual int M() { return 1; }
}

class Outer {
new class Inner : MyClass { // ERROR on 'new' modifier
public new int M() { return 2; }
}
}

The above code gives a warning on the declaration of Inner.

So, am I way off base here? Is there something wrong with sections 18.1.1
and 17.1.1?

Thanks
--
Tom Baxter

Oct 31 '07 #1
13 2132
Tom Baxter wrote:
Hi everyone,

Has anyone looked at section 18.1.1 of the C# spec? It indicates 'new'
and 'protected' are valid modifiers on struct declarations. First, how
can 'protected' be valid on a struct, since structs cannot be inherited?
The compiler gives an error (as I expect it should) if you try this:

protected struct MyStruct {}

so I'm wondering if the spec is wrong when it says 'protected' is a
valid struct-modifier.

The second point about section 18.1.1 is that it indicates 'new' is an
allowable modifier on a struct. Actually, 18.1.1 indicates, "The
modifiers of a struct declaration have the same meaning as those of a
class declaration", referring the reader to section 17.1.1 on class
declaration modifiers.

I looked at section 17.1.1 and sure enough, it says:

"The new modifier is permitted on nested classes.
It specifies that the class hides an inherited member
by the same name, as described in section 17.2.2. It
is a compile-time error for the new modifier to appear
on a class declaration that is not a nested class declaration."

My testing indicates 'new' is not allowed on class nor struct
declarations (nested or not). It seems this is how 'new' should be used
as a class modifier, according to the spec (sec. 17.1.1):

class MyClass {
public virtual int M() { return 1; }
}

class Outer {
new class Inner : MyClass { // ERROR on 'new' modifier
public new int M() { return 2; }
}
}

The above code gives a warning on the declaration of Inner.
I think if you change your definition to actually hide a member (M) you
will remove that error.
class MyClass
{
public virtual int M() { return 1; }
}

class Outer : MyClass
{
public new class M : MyClass
{
}
}

class C2 : MyClass
{
public new struct M
{
}
}
Oct 31 '07 #2
And on the "protected" side - it again relates to things that subclass
the containing class - i.e.

public class BaseClass {
void TestBase() {
NestedStruct ns;
}
private struct NestedStruct { int a;}
}
public class SubClass : BaseClass {
void TestSub() {
NestedStruct ns; // **ERROR
}
}

The above is illegal, since SubClass cannot see BaseClass.Neste dStruct
(since it is private); however, change NestedStruct to be protected,
and any sublasses of BaseClass can now see it.

Marc

Oct 31 '07 #3
Thanks, Marc!! I appeciate!
"Marc Gravell" <ma**********@g mail.comwrote in message
news:11******** **************@ 50g2000hsm.goog legroups.com...
And on the "protected" side - it again relates to things that subclass
the containing class - i.e.

public class BaseClass {
void TestBase() {
NestedStruct ns;
}
private struct NestedStruct { int a;}
}
public class SubClass : BaseClass {
void TestSub() {
NestedStruct ns; // **ERROR
}
}

The above is illegal, since SubClass cannot see BaseClass.Neste dStruct
(since it is private); however, change NestedStruct to be protected,
and any sublasses of BaseClass can now see it.

Marc
--
Tom Baxter

Oct 31 '07 #4
I guess I spoke too soon! :)

This will compile:

class A {
protected struct B {}
}
but this will not:

struct A {
protected struct B {}
}
So, why would it be the case that a nested struct can be protected if it's
within a class but not if it's within a struct??

"Marc Gravell" <ma**********@g mail.comwrote in message
news:11******** **************@ 50g2000hsm.goog legroups.com...
And on the "protected" side - it again relates to things that subclass
the containing class - i.e.

public class BaseClass {
void TestBase() {
NestedStruct ns;
}
private struct NestedStruct { int a;}
}
public class SubClass : BaseClass {
void TestSub() {
NestedStruct ns; // **ERROR
}
}

The above is illegal, since SubClass cannot see BaseClass.Neste dStruct
(since it is private); however, change NestedStruct to be protected,
and any sublasses of BaseClass can now see it.

Marc
--
Tom Baxter

Oct 31 '07 #5
Tom Baxter <tl********@new sgroup.nospamwr ote:
I guess I spoke too soon! :)

This will compile:

class A {
protected struct B {}
}
but this will not:

struct A {
protected struct B {}
}
So, why would it be the case that a nested struct can be protected if it's
within a class but not if it's within a struct??
Structs cannot contain protected members at all - because you can't
derive from a struct. The fact that the member itself is a struct is
just coincidental - you'll see the same behaviour if B is a variable or
a method.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 31 '07 #6
You can't inherit from a struct - in particular, you can't inherit
from struct A; hence "protected" is meaningless. You would get similar
(actually just a warning, not an error) if you marked class A as
"sealed": new protected member declared in sealed class.

Marc

Oct 31 '07 #7
It turns outy that a struct *can* be marked as protected but only of its
immediate parent is a class. If its immediate parent is a struct, it cannot
be protected. So, this does not compile:

class A {
class B {
struct C {
protected struct Z { }
}
}
}

while this does:

struct A {
struct B {
class C {
protected struct Z { }
}
}
}

So, why would it be the case that a nested struct can be protected if it's
within a class but not if it's within a struct??
"Marc Gravell" <ma**********@g mail.comwrote in message
news:11******** **************@ 50g2000hsm.goog legroups.com...
And on the "protected" side - it again relates to things that subclass
the containing class - i.e.

public class BaseClass {
void TestBase() {
NestedStruct ns;
}
private struct NestedStruct { int a;}
}
public class SubClass : BaseClass {
void TestSub() {
NestedStruct ns; // **ERROR
}
}

The above is illegal, since SubClass cannot see BaseClass.Neste dStruct
(since it is private); however, change NestedStruct to be protected,
and any sublasses of BaseClass can now see it.

Marc
--
Tom Baxter

Oct 31 '07 #8
On 2007-10-31 16:35:19 -0700, "Tom Baxter" <tl********@new sgroup.nospamsa id:
So, why would it be the case that a nested struct can be protected if
it's within a class but not if it's within a struct??
You are not focusing on the important part. Any class can be
inherited. So any declaration within a class may have the "protected"
access.

It doesn't matter where the class is. You can declare it in a struct
if you like, the rule still applies.

Class C can't be "protected" , because it's a member of a struct.
Struct B can't be "protected" because it's a member of a struct. But
Struct Z is a member of a class. Thus, it can be "protected" .

It's the same exact rule that applies to the general case that started
this thread. Nothing's different about this most recent example.

Pete

Nov 1 '07 #9
How stupid of me! It's rather embarrassing, actually.

Thank you Jon, Marc and Pete. I sincerely appreciate you taking the time to
respond.

Jon, I am looking forward to your Manning book. Do you have a ballpark time
frame as to when it will be available in hard copy?

"Tom Baxter" <tl********@new sgroup.nospamwr ote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
>I guess I spoke too soon! :)

This will compile:

class A {
protected struct B {}
}
but this will not:

struct A {
protected struct B {}
}
So, why would it be the case that a nested struct can be protected if it's
within a class but not if it's within a struct??

"Marc Gravell" <ma**********@g mail.comwrote in message
news:11******** **************@ 50g2000hsm.goog legroups.com...
>And on the "protected" side - it again relates to things that subclass
the containing class - i.e.

public class BaseClass {
void TestBase() {
NestedStruct ns;
}
private struct NestedStruct { int a;}
}
public class SubClass : BaseClass {
void TestSub() {
NestedStruct ns; // **ERROR
}
}

The above is illegal, since SubClass cannot see BaseClass.Neste dStruct
(since it is private); however, change NestedStruct to be protected,
and any sublasses of BaseClass can now see it.

Marc

--
Tom Baxter
--
Tom Baxter

Nov 1 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7597
by: Screwball | last post by:
<?php class myClass { var $myVar; .. .. } ?> In php.ini I have error_reporting = E_ALL | E_STRICT
2
6061
by: Kolozs, √Āron | last post by:
Hi everybody, The C# compiler reports a Compiler Error CS0052 in the following situation: I declared a type marked as "internal": namespace MyNamespace { internal class MyInteralClass
11
3842
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
3
1220
by: Shawn B. | last post by:
Greetings, I'm trying to create a Managed C++ library that extends the console features beyond System::Console capabilities. I chose MC++ over C++/CLI because I need it to work on the 1.1 framework. I haven't programmed C++ in years and I'm very very new to Managed C++. Thus, I'm having issues getting even the simplest thing to work, and not for the fact, that finding good information about using API calls and such from within a...
7
6403
by: My4thPersonality | last post by:
1st, simple question, is there a protected in C#? I mean a comparable keyword like the C++ protected, which defines a member as accessable for decendants, but not for the outside world. Then, a more complicated question, is privat in C#, then comparable with protected, or private in C++. Or to say it in other words, if I declare a member private in C#, can I access it in a decendant class?
8
16356
by: Jordan | last post by:
AFAIK there are two ways to expose members of a base class to a derived or child class: 1. declare the members public in the base class 2. declare them as 'protected' in the base class Is either of these methods generally recommended over the other? If yes, why? Thanks!
4
6301
by: newbie120 | last post by:
Hi all maybe its just been a long day, but i have a question about call access modifiers in C#. Consider the following code. namespace Application { private class Class1 { int i;
13
2823
by: Clive Dixon | last post by:
I am refactoring some code to move some base classes into a separate assembly. One of these base classes has a member property which is 'protected internal'. However when I move these base classes to another assembly, the compiler complains that the override in the derived class, also declared as 'protected internal', is trying to change the access modified from 'protected', which is clearly not the case. (I have checked the metadata in...
3
2283
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses these structs. A typical usage case will be as ff (note the code below is Pseudocode and WILL NOT compile) //example structs (I have left out the ctors/dtors etc for brevity sake) struct MyStructA
0
8978
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8818
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9305
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8231
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectóplanning, coding, testing, and deploymentówithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4588
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4860
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2771
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.