|
It has to do with the accessibility rules in C# and the meanings of private, protected, and public methods.
From the C# Language Reference[^]
- The intuitive meaning of public is "access not limited".
- The intuitive meaning of protected is "access limited to the containing class or types derived from the containing class".
- The intuitive meaning of private is "access limited to the containing type".
When a type or member M is accessed, if M is protected, the access is permitted if it occurs within the class in which M is declared, or if it occurs within a class derived from the class in which M is declared and takes place through the derived class type (Section 3.5.3).
Otherwise, if M is private, the access is permitted if it occurs within the type in which M is declared.
As described in Section 3.4, all members of a base class, except for instance constructors, destructors and static constructors, are inherited by derived types. This includes even private members of a base class. However, the accessibility domain of a private member includes only the program text of the type in which the member is declared.
So, by declaring the constructor as "private", you are limiting it's accessibility to the class in which it is declared (the "containing type"). This effectively tells the compiler that since there is no default constructor accessible from outside the class, any derived classes cannot create an instance of their base class and, therefor, cannot themselves contain constructors.
By declaring the constructor as "protected", you are limiting it's accessibility to the class in which it is declared (the "containing type") and also to any types derived from that class. This effectively tells the compiler that the child classes are able to create an instance of their base class, and, therefor, can also contain constructors.
-----------------------------
In just two days, tomorrow will be yesterday.
|
|
|
|
|
The private constructor is intended:
- for classes that doesn't need to be created (provides only static methods)
- for implementing the singleton pattern
Also check out the constructor usage guidelines [^]
Anyway why would you derive a class with private constructor and instantiate that?
|
|
|
|
|
Zoltan Balazs wrote: Anyway why would you derive a class with private constructor and instantiate that?
I wouldn't, at least I can't see any situation where I would need to do that right now. I just want to try to understand every nook and cranny of the C# language and the CLR. Sometimes I get a very simple question stuck in my head and I am not satisfied until I figure it out. A lot of times these questions have to do with object-oriented concepts.
█▒▒▒▒▒██▒█▒██
█▒█████▒▒▒▒▒█
█▒██████▒█▒██
█▒█████▒▒▒▒▒█
█▒▒▒▒▒██▒█▒██
|
|
|
|
|
Well, any constructor you create in the child class will call the base class constructor - and fail because you've declared it as private. It's just like some external class attempting to instantiate and therefore call the private constructor.
If you're not clear why a derived class's constructor must call a base class one, take a look at this example
class Base
{
public int x;
public Base() { x = 20; }
}
class Derived : Base
{
public Derived() {}
}
Now if you run the following snippet of code
Derived d = new Derived();
Console.WriteLine(d.x);
What value do you expect? 20, because Derived after all, is a Base. If Derived's constructor did not call the Base one, then the above snippet will give x's default value instead.
|
|
|
|
|
Are you saying that the C# compiler implicitly calls the base's constructor? I thought you had to explicitly do that.
█▒▒▒▒▒██▒█▒██
█▒█████▒▒▒▒▒█
█▒██████▒█▒██
█▒█████▒▒▒▒▒█
█▒▒▒▒▒██▒█▒██
|
|
|
|
|
Captain See Sharp wrote: Are you saying that the C# compiler implicitly calls the base's constructor?
Yes, it attempts to call the base class's default constructor, if you don't make an explicit call. That's why you'll see code like this fail to compile
class Base
{
public Base(int x){}
}
class Derived : Base
{
public Derived() {}
}
|
|
|
|
|
S. Senthil Kumar wrote: That's why you'll see code like this fail to compile
I see, I thought C# always created a default parameterless constructor, I tested it and you are right, it does not compile.
█▒▒▒▒▒██▒█▒██
█▒█████▒▒▒▒▒█
█▒██████▒█▒██
█▒█████▒▒▒▒▒█
█▒▒▒▒▒██▒█▒██
|
|
|
|
|
It will always create a default parameterless public constructor unless you define your own constructor(s). That's why when you want to create a purely static class, you explicitly define a private constructor (typically parameterless)...that way you avoid having a default public constructor and therefore your class has no accesible constructors at all. If you dont explicitly add this private constructor your class would automatically have the default public one and consumers would be able to instantiate it.
And yes, any derived class constructor will attempt to call the default constructor of it's base class if no other constructor of said base class is explicitly called in your code.
|
|
|
|
|
Another question. The child class only implicitly calls the base's parameterless constructor right? If it does this, will it call the parent's parameterless constructor for all of the child's constructors if you do not explicitly call a specific base constructor?
█▒▒▒▒▒██▒█▒██
█▒█████▒▒▒▒▒█
█▒██████▒█▒██
█▒█████▒▒▒▒▒█
█▒▒▒▒▒██▒█▒██
|
|
|
|
|
Captain See Sharp wrote: will it call the parent's parameterless constructor for all of the child's constructors if you do not explicitly call a specific base constructor?
Yes, it will.
|
|
|
|
|
what is mean of state management...
where we r using.....
which purpose.....
please give me detailed...
|
|
|
|
|
State Management is used in ASP.NET 2.0. It provides a variety of mechanisms for building stateful Web applications. This module introduces view state, application cache, session state, profiles, and cookies.
|
|
|
|
|
I have a user control inherited from a Panel. I'm trying to make it re sizable and movable(mostly done)
I'd like to add a property to be able to lock the width or height or both.
I'm not sure how to do it. The code below kind of gives the feeling of what I'm trying to do.
Once again, my thanks goes out to this great board and its members.
<br />
<br />
public partial class PanelSizable : System.Windows.Forms.Panel<br />
{<br />
<br />
enum LockedType<br />
{<br />
Both,<br />
Width,<br />
Height,<br />
None<br />
};<br />
<br />
<br />
<br />
private LockedType lockStatus = LockedType.None;<br />
<br />
public LockedType LockSize<br />
{<br />
get { return lockStatus; }<br />
set { lockStatus = value; }<br />
<br />
}<br />
<br />
....<br />
|
|
|
|
|
Ok, when you set the lock type the set method will execute, that should be known already.;P In the set method record the size of your control. Now make sure you have registered all resize events for the control (there are at least 3), when one of those events gets fired due to a resize of the control, set the size back to the locked values. The control will then "snap back". In case more than one resize event gets executed during a resize make sure that you pay attention which events get fired and attempt to reduce duplicate "snap backs" to improve performance.
█▒▒▒▒▒██▒█▒██
█▒█████▒▒▒▒▒█
█▒██████▒█▒██
█▒█████▒▒▒▒▒█
█▒▒▒▒▒██▒█▒██
|
|
|
|
|
Hello every body
I'm working in my garduation project that is Iris Recgonition
My Supervisor recomened me the steps that i have to works on it
1-convert image to grayScale
2-apply smoothing filter
3-apply an edge detection algorithm( canny or sobel )
4-find pupil and iris using hough transform
thes is the first stage of my project
now i have already do the first & second & third steps
i need ur help in implementing a hough trnasform to find pupil and iris
i tried to study it for a long time but i really failed to understand the consept
please help me ...
|
|
|
|
|
|
Hello!
I would like to know how to generate your own colors in C# .NET 2.0
What I would like to do is generate my own color combinations, like give RGB coordinates which then are fed to drawing methods so that you don't have to set a color to for instance Color.LightBlue but (R=50; G=75; B=200). Is this possible?
Could someone give me the name (and namespace) of a method that turns RGB (or another color system) into a real color for use on the screen?
Thank you for helping me.
Ranger.
Beginner
|
|
|
|
|
Color myColor = Color.FromArgb(50, 75, 200)
|
|
|
|
|
Judah Himango wrote: Color myColor = Color.FromArgb(50, 75, 200)
Thanks! I wonder why I didn't find this while browsing through help files.
Ranger..
Beginner
|
|
|
|
|
I want 2 resize a bitmap........... Have seen Mr. Christian article on resizing but its sooo slow any other way to resize it..... THANX in advance......
|
|
|
|
|
Search MSDN for creating Thumbnails of images. I can't remember the exact library it is in otherwise I would tell you more. The function is either in System.Drawing or System.Windows.Forms.ControlPaint.
Regards,
Thomas Stockwell
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Visit my homepage Oracle Studios[ ^]
|
|
|
|
|
hello... i found some help regarding neural nets here at code project but all were dll files... that is i couln't have an insight of the code... anyways my question is that if backpropagation nets is used for ocr then when test with new patterns, the net decides on basis of previous training data ie weights and biases etc or does it simply move the data on the net, produce the activation function etc results and then determining on the highest activation function result to determine the result... ie if i have a net of 900 input , 8 hiden and 10(for numbers) output nodes, then if i use a new pattern to identify, this pattern will always give some activation function result now should this be compared with the save ones or just simply take the index where the activation is highest...
thanx
haseeb
|
|
|
|
|
Hi, I'm new to C# and .Net.
How to find a files in a directory in C#? Es. all *.doc?
Thanks.
|
|
|
|
|
|
Thanks
|
|
|
|