The C# Language Specification says:
A virtual property declaration specifies that the accessors of the property
are virtual. The virtual modifier applies to both accessors of a read-write
property - it is not possible for only one accessor of a read-write property
to be virtual.
An abstract property declaration specifies that the accessors of the
property are virtual, but does not provide an actual implementation of the
accessors. Instead, non-abstract derived classes are required to provide
their own implementation for the accessors by overriding the property.
Because an accessor for an abstract property declaration provides no actual
implementation, its accessor-body simply consists of a semicolon.
Does this mean it is possible for only one accessor of a read-write property
to be abstract (both get and set are virtual)?
I tried:
interface A
{
int X { get; set; }
}
abstract class B : A
{
virtual public int X
{
get
{
return 0;
}
}
}
class C : B
{
override public int X
{
// use base class getter
set
{
throw new Exception("Ille gally tried to set X.");
}
}
}
But no matter omit the setter in abstract class B, leave it empty, try to
use the abstract keyword, I always get an error. I know I can use an
abstract member function for this, but how can I leave the setter undefined
(since B is abstract after all)? 2 9764
"Ben Voigt" <bv****@nospam. nospam> wrote in message
news:eW******** ******@TK2MSFTN GP11.phx.gbl...
| The C# Language Specification says:
|
| A virtual property declaration specifies that the accessors of the
property
| are virtual. The virtual modifier applies to both accessors of a
read-write
| property - it is not possible for only one accessor of a read-write
property
| to be virtual.
|
| An abstract property declaration specifies that the accessors of the
| property are virtual, but does not provide an actual implementation of the
| accessors. Instead, non-abstract derived classes are required to provide
| their own implementation for the accessors by overriding the property.
| Because an accessor for an abstract property declaration provides no
actual
| implementation, its accessor-body simply consists of a semicolon.
|
|
|
|
|
| Does this mean it is possible for only one accessor of a read-write
property
| to be abstract (both get and set are virtual)?
|
| I tried:
|
| interface A
| {
| int X { get; set; }
| }
| abstract class B : A
| {
| virtual public int X
| {
| get
| {
| return 0;
| }
| }
| }
| class C : B
| {
| override public int X
| {
| // use base class getter
| set
| {
| throw new Exception("Ille gally tried to set X.");
| }
| }
| }
|
|
|
| But no matter omit the setter in abstract class B, leave it empty, try to
| use the abstract keyword, I always get an error. I know I can use an
| abstract member function for this, but how can I leave the setter
undefined
| (since B is abstract after all)?
|
|
If you mean:
set{}
by " leave it empty"?
this should work.
Willy.
It appears that the compiler will not allow you to implement "half" of
an interface property. You must either declare it "abstract," in which
case you can define neither the getter nor the setter:
public abstract int X { get; set; }
or you must declare it "virtual," in which case it can have no abstract
component and you must define both the getter and the setter:
public virtual int X
{
get { ... }
set { ... }
}
There appears to be no way to implement the getter but require that
derived classes implement the setter. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Paul MG |
last post by:
Hi
Template partial specialization always seems like a fairly
straightforward concept - until I try to do it :).
I am trying to implement the input sequence type (from Stroustrup
section 18.3.1, 'Iseq'). I want the version for containers that he
gives, but also to provide a specialization for construction from a
pair<It,It> (eg because that is returned by equal_range()).
|
by: Erik Wikström |
last post by:
In school (no I will not ask you to do my schoolwork for me) we talked
about policy-based design and got an assignment where we got the a code-
fragment from a stack-implementation. The idea with the code was that
if, when its destructor was called, it still contained any elements it
would run 'delete' on them if they were pointers and do nothing if they
were not.
Our assignment was to reimplement the stack using policies so that the...
|
by: Gomaw Beoyr |
last post by:
Two question about the "partial classes" (in the next wersion of
..NET).
Question 1
==========
Will partial classes (in the next version of C#) have to be
declared "partial" in ALL places.
I.e. do we have to need to write:
|
by: Madhuri Mittal |
last post by:
Hi,
I know that we can define a class over multiple source files using the
keyword 'Partial' in C# within a project. Can we define the class over
multiple projects using the 'Patial' keyword- I know VB.Net does not allow
it, however was wondering if it's possible using C#?
Madhuri Mittal
|
by: pawel.pabich |
last post by:
Hajo,
I would like to have 2 my own partial classes.
For example:
Default.aspx.cs
Default2.aspx.cs
and they both will relate to
Default.aspx page.
| |
by: Fat Elvis |
last post by:
I'd like to extend some of my Asp.net pages by using Partial Classes.
Example ASP.Net Page:
public partial class Admin_Customer : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Data_List();
}
}
|
by: Hiten |
last post by:
Is it possible to have more code behind file but that file will have
same class in way of partial class,
Default
Structure I
--------------
Homepage.aspx
+ Homepage.aspx.cs (this .cs file will have "Homepage" class
which is partial)
|
by: daokfella |
last post by:
I want to extend the auto-generated tableadapter classes created by
the dataset designer.
I'd like to add a connectionString property so I can set this outside
my class library. I've got Get/Set code for that in my partial class.
Is it possible to "REPLACE" method calls in a partial class? In other
words, I'd like to replace the InitConnection() Sub in the generated
class with my own in my partial class.
|
by: JDeats |
last post by:
So I have a class that spans over two partial classes in code, here's
an example (do not read much into this, the code is of no practical
use, this is just a simple example of where my confusion occurs).
// Inside SharedClassExample1.cs
public partial class SharedClassExample
{
public List<stringBooksOnShelf { get; set; }
public List<stringBooksOnDesk { get; set; }
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |