473,377 Members | 1,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,377 software developers and data experts.

Namespace elements cannot be explicitly declared as private, protected, or protected internal

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;
}
}

When I try to compile this i get the following error:
"Namespace elements cannot be explicitly declared as private,
protected, or protected internal"

When i make Class1 public i no longer get a compile error. Now correct
me if i'm wrong, but doesn't C# allow me to have private or protected
class structures? Am i missing something?

thanks

Jun 19 '06 #1
5 13113
<ne*******@gmail.com> a écrit dans le message de news:
11*********************@h76g2000cwa.googlegroups.c om...

| 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;
| }
| }
|
| When I try to compile this i get the following error:
| "Namespace elements cannot be explicitly declared as private,
| protected, or protected internal"
|
| When i make Class1 public i no longer get a compile error. Now correct
| me if i'm wrong, but doesn't C# allow me to have private or protected
| class structures? Am i missing something?

The message says it all. Classes can only be declared as private, protected
or protected internal when declared as nested classes, other than that, it
doesn't make sense to declare a class with a visibility that makes it
unusable, even in the same module.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jun 19 '06 #2
ne*******@gmail.com wrote:
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;
}
}

When I try to compile this i get the following error:
"Namespace elements cannot be explicitly declared as private,
protected, or protected internal"

When i make Class1 public i no longer get a compile error. Now correct
me if i'm wrong, but doesn't C# allow me to have private or protected
class structures? Am i missing something?


Private and protected classes must be nested inside another class.

What would it mean for a class to be private, yet declared at the
namespace level? How could any code access it?

-- Barry

--
http://barrkel.blogspot.com/
Jun 19 '06 #3
If the class is private, that means no other class can use it. Period!

If you want to stop public instantiation, use this pattern instead:

public class Class1
{
private Class1()
{
}
int i;
}

If you want it only used internally, try "internal" instead of public.

If you want a private class, the class must be inside of another class.

public class SomeContainingClass
{
private class Class1
{
int i;
}
}
--
Gregory A. Beamer

*************************************************
Think Outside the Box!
*************************************************
<ne*******@gmail.com> wrote in message
news:11*********************@h76g2000cwa.googlegro ups.com...
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;
}
}

When I try to compile this i get the following error:
"Namespace elements cannot be explicitly declared as private,
protected, or protected internal"

When i make Class1 public i no longer get a compile error. Now correct
me if i'm wrong, but doesn't C# allow me to have private or protected
class structures? Am i missing something?

thanks

Jun 20 '06 #4
ne*******@gmail.com wrote:
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;
}
}

When I try to compile this i get the following error:
"Namespace elements cannot be explicitly declared as private,
protected, or protected internal"

When i make Class1 public i no longer get a compile error. Now correct
me if i'm wrong, but doesn't C# allow me to have private or protected
class structures? Am i missing something?

thanks


top level classes cannot be private, they are "internal" by default, you
can just make them public to make them visible from outside your dll.
Jun 20 '06 #5
Cowboy (Gregory A. Beamer) <No************@comcast.netNoSpamM> wrote:
If the class is private, that means no other class can use it. Period!


<pedant mode>
Ónly if it's a top level class (as it was in the OP's question.)

The code below shows a private class being used within the body of the
class it is nested in:

using System;

class Test
{
static void Main()
{
PrivateClass pc = new PrivateClass();
}

private class PrivateClass
{
internal PrivateClass()
{
Console.WriteLine ("Hi there!");
}
}
}
</pedant mode>

Time for bed when I'm getting that pedantic, I think :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 22 '06 #6

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

Similar topics

2
by: wooks | last post by:
I want to apply 2 changes to the following xml <ROOTSTUB app="appname"/> 1. change the app attribute to an element. 2. put the ROOTSTUB element into a namespace that is passed as a global...
5
by: Christian Meier | last post by:
Hi dear programmers I looked for the difference between private and protected inheritance, but couldn't find anything. Here is my sample code: #include <iostream> using std::cout; using...
4
by: Jason Shohet | last post by:
A label on an ascx control has a corresponding declaration in the c# code-behind page. I was curious what would happen if I made that declaration 'PRIVATE' instead of 'PROTECTED'. The only things...
1
by: Yuan Ze | last post by:
The compiler can not compile the code boxing/unboxing value types declared in a MG C++ class. It reports compiler error 2248, 'member' : cannot access member declared in class 'class'. To use a...
3
by: Nad | last post by:
Hello, When I add a User Control to a web form and then try to declare it in the code as a private member it doesn't get instantiated. However with 'protected' it's OK! For example I get a...
2
by: pkpatil | last post by:
Hi, Can a private composite object in a class access private or protected members of base class? For e.g. class composite { void memberFunction(); };
4
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;
0
by: Eric | last post by:
Can someone please tell me how to rectify this. The full error is: sub 'Dispose' cannot be declared 'Overrides' because it does not override a sub in a base class. Followed by: 'Dispose' is...
1
by: amitsaxena1981 | last post by:
hi, i m building web custom control in asp.net2.0 with c# but whenever I drop this control it gives the error The namespace attribute cannot be an empty string.please suggest me . Thanks ...
1
by: Martynas | last post by:
Hello, i was codding a program with c# and i get an error called "cannot explicitly call operator or accessor" here is the code:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.