default accessibility
-
With .NET 2.0, it's noted that accessibility of classes need not be defined, since classes can be inherited within the same assembly. When the superclass is given accessibility as public, the program runs successfully. However setting the inherited class as public, error messages are displayed: public class myClass : TestConsole.MainConsole Error: Inconsistent accessibility: base class 'TestConsole.MainConsole' is less accessible than class 'TestConsole.myClass' What is the default accessibility level when it is not explicitly defined? For example the following code is successfully built:
Program.cs
using System;
using System.Collections.Generic;
using System.Text;namespace TestConsole
{// public class MainConsole
// program is built successfully when using this line instead of the bottom oneclass MainConsole // default scope for this class?? { public const int myAge = 27; public int returnZero() { return 0; //return (number1 + number2); } static void Main(string\[\] args) { Console.WriteLine("Start program"); } }
myClass.cs
using System;
using System.Collections.Generic;
using System.Text;namespace TestConsole
{
class myClass : TestConsole.MainConsole
{
private int Age;
public myClass()
{
//initialize Age to zero (long-winded)
Age = base.returnZero();
}
}
}Jon
-
With .NET 2.0, it's noted that accessibility of classes need not be defined, since classes can be inherited within the same assembly. When the superclass is given accessibility as public, the program runs successfully. However setting the inherited class as public, error messages are displayed: public class myClass : TestConsole.MainConsole Error: Inconsistent accessibility: base class 'TestConsole.MainConsole' is less accessible than class 'TestConsole.myClass' What is the default accessibility level when it is not explicitly defined? For example the following code is successfully built:
Program.cs
using System;
using System.Collections.Generic;
using System.Text;namespace TestConsole
{// public class MainConsole
// program is built successfully when using this line instead of the bottom oneclass MainConsole // default scope for this class?? { public const int myAge = 27; public int returnZero() { return 0; //return (number1 + number2); } static void Main(string\[\] args) { Console.WriteLine("Start program"); } }
myClass.cs
using System;
using System.Collections.Generic;
using System.Text;namespace TestConsole
{
class myClass : TestConsole.MainConsole
{
private int Age;
public myClass()
{
//initialize Age to zero (long-winded)
Age = base.returnZero();
}
}
}Jon
coz the Child Class is Default (Private) u change Change class myClass : TestConsole.MainConsole into Public class myClass : TestConsole.MainConsole
-
With .NET 2.0, it's noted that accessibility of classes need not be defined, since classes can be inherited within the same assembly. When the superclass is given accessibility as public, the program runs successfully. However setting the inherited class as public, error messages are displayed: public class myClass : TestConsole.MainConsole Error: Inconsistent accessibility: base class 'TestConsole.MainConsole' is less accessible than class 'TestConsole.myClass' What is the default accessibility level when it is not explicitly defined? For example the following code is successfully built:
Program.cs
using System;
using System.Collections.Generic;
using System.Text;namespace TestConsole
{// public class MainConsole
// program is built successfully when using this line instead of the bottom oneclass MainConsole // default scope for this class?? { public const int myAge = 27; public int returnZero() { return 0; //return (number1 + number2); } static void Main(string\[\] args) { Console.WriteLine("Start program"); } }
myClass.cs
using System;
using System.Collections.Generic;
using System.Text;namespace TestConsole
{
class myClass : TestConsole.MainConsole
{
private int Age;
public myClass()
{
//initialize Age to zero (long-winded)
Age = base.returnZero();
}
}
}Jon
The default accessibility is always private. This applies to classes, methods, properties, and fields. In your code sample, defining
MainConsole
with out specifying the accessibility makes it private. However, the problem you are running into isn't the accessibility on the class since in this case, they are both private. InMainConsole
you do not have a default constructor, so the compiler is generating one for you. The compiler generated constructor is always private. When you then try to deriveMyClass
from it,MyClass
has a public constructor. What the compiler sees is that you have a class with a public constructor inheriting from one with a private constructor and says that there is no way for the derived class to create an instance of the base class. (If something is private, even the derived classes don't have visibility to it.) [modification 13:28 Tuesday 4th September, 2007] I was wrong on this response, not sure what I was thinking. See this response: http://www.codeproject.com/script/comments/forums.asp?forumid=1649&select=2209501&mpp=50&fr=354&df=100#xx2209501xx[^] [/modification]Scott.
—In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]
-
With .NET 2.0, it's noted that accessibility of classes need not be defined, since classes can be inherited within the same assembly. When the superclass is given accessibility as public, the program runs successfully. However setting the inherited class as public, error messages are displayed: public class myClass : TestConsole.MainConsole Error: Inconsistent accessibility: base class 'TestConsole.MainConsole' is less accessible than class 'TestConsole.myClass' What is the default accessibility level when it is not explicitly defined? For example the following code is successfully built:
Program.cs
using System;
using System.Collections.Generic;
using System.Text;namespace TestConsole
{// public class MainConsole
// program is built successfully when using this line instead of the bottom oneclass MainConsole // default scope for this class?? { public const int myAge = 27; public int returnZero() { return 0; //return (number1 + number2); } static void Main(string\[\] args) { Console.WriteLine("Start program"); } }
myClass.cs
using System;
using System.Collections.Generic;
using System.Text;namespace TestConsole
{
class myClass : TestConsole.MainConsole
{
private int Age;
public myClass()
{
//initialize Age to zero (long-winded)
Age = base.returnZero();
}
}
}Jon
Default accessibility is private for everything but namespace elements. That means classes, structs, delegates or enums that are not defined within another class are by default internal. In all other cases accessibility defaults to private. In your case, the
myClass
andMainConsole
classes are internal. -
The default accessibility is always private. This applies to classes, methods, properties, and fields. In your code sample, defining
MainConsole
with out specifying the accessibility makes it private. However, the problem you are running into isn't the accessibility on the class since in this case, they are both private. InMainConsole
you do not have a default constructor, so the compiler is generating one for you. The compiler generated constructor is always private. When you then try to deriveMyClass
from it,MyClass
has a public constructor. What the compiler sees is that you have a class with a public constructor inheriting from one with a private constructor and says that there is no way for the derived class to create an instance of the base class. (If something is private, even the derived classes don't have visibility to it.) [modification 13:28 Tuesday 4th September, 2007] I was wrong on this response, not sure what I was thinking. See this response: http://www.codeproject.com/script/comments/forums.asp?forumid=1649&select=2209501&mpp=50&fr=354&df=100#xx2209501xx[^] [/modification]Scott.
—In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]
-
Default accessibility is private for everything but namespace elements. That means classes, structs, delegates or enums that are not defined within another class are by default internal. In all other cases accessibility defaults to private. In your case, the
myClass
andMainConsole
classes are internal. -
The default accessibility is always private. This applies to classes, methods, properties, and fields. In your code sample, defining
MainConsole
with out specifying the accessibility makes it private. However, the problem you are running into isn't the accessibility on the class since in this case, they are both private. InMainConsole
you do not have a default constructor, so the compiler is generating one for you. The compiler generated constructor is always private. When you then try to deriveMyClass
from it,MyClass
has a public constructor. What the compiler sees is that you have a class with a public constructor inheriting from one with a private constructor and says that there is no way for the derived class to create an instance of the base class. (If something is private, even the derived classes don't have visibility to it.) [modification 13:28 Tuesday 4th September, 2007] I was wrong on this response, not sure what I was thinking. See this response: http://www.codeproject.com/script/comments/forums.asp?forumid=1649&select=2209501&mpp=50&fr=354&df=100#xx2209501xx[^] [/modification]Scott.
—In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]
Hmmm, default accessibility for classes and structs is internal, nested classes and structs are private. If you create no constructors for a class it will have a default public constructor. The error he gets is because MainConsole is internal and he creates a public class that inherits from it.
-
Hmmm, default accessibility for classes and structs is internal, nested classes and structs are private. If you create no constructors for a class it will have a default public constructor. The error he gets is because MainConsole is internal and he creates a public class that inherits from it.
You're right...I'm not sure what I was thinking when I wrote that response.
Scott.
—In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]