473,396 Members | 1,766 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,396 software developers and data experts.

Virtual Properties Problem

I have thought that you can disable a set or a get when you override a
virtual property. I guess that I was wrong. The code below compiles and
doesn't throw the exceptions that I have expected. It calls the base
accessor.

using System;

namespace Test
{
public abstract class MetaDataFile
{
protected string name;

public virtual string Name
{
get
{
return name;
}
set
{
name = value;
}
}
}

public class MetaDataFileReader : MetaDataFile
{
public MetaDataFileReader(string name)
{
this.name = name;
}

public override string Name
{
get
{
return name;
}
}
}

public class MetaDataFileWriter : MetaDataFile
{
public override string Name
{
set
{
name = value;
}
}
}

public class Tester
{
public static void Main()
{
MetaDataFile f;
MetaDataFileReader r = new MetaDataFileReader("foobar.torrent");
MetaDataFileWriter w = new MetaDataFileWriter();

r.Name = "test.torrent"; // Should throw a set accessor exception
w.Name = "fooby.torrent";
Console.WriteLine(w.Name); // Should throw a get accessor exception
f = r;
f.Name = "test2.torrent";
}
}
}
Nov 16 '05 #1
1 2322
Spookey,

Virtual only tells you that you can override the default behaviour of
the member,
and if you decide not to then the default (the behaviou found in the base
class) will
be used. When compiled, a property does infact get translated to a pair of
methods
called get_PropertyName and set_PropertyName. This means that when declaring
a virtual property you are infact declaring two virtual methods. In the C#
Language
Specification (10.6.3) you can read the follwing

- A get accessor corresponds to a parameterless method with a return
value of
the property type and the same modifiers as the containing property.

- A set accessor corresponds to a method with a single value parameter
of the
property type, a void return type, and the same modifiers as the
containing property.

HTH,

//Andreas

"SpookyET" <no****@hotmail.com> skrev i meddelandet
news:opr5up78sl1s9n15@saturn...
I have thought that you can disable a set or a get when you override a
virtual property. I guess that I was wrong. The code below compiles and
doesn't throw the exceptions that I have expected. It calls the base
accessor.

using System;

namespace Test
{
public abstract class MetaDataFile
{
protected string name;

public virtual string Name
{
get
{
return name;
}
set
{
name = value;
}
}
}

public class MetaDataFileReader : MetaDataFile
{
public MetaDataFileReader(string name)
{
this.name = name;
}

public override string Name
{
get
{
return name;
}
}
}

public class MetaDataFileWriter : MetaDataFile
{
public override string Name
{
set
{
name = value;
}
}
}

public class Tester
{
public static void Main()
{
MetaDataFile f;
MetaDataFileReader r = new MetaDataFileReader("foobar.torrent");
MetaDataFileWriter w = new MetaDataFileWriter();

r.Name = "test.torrent"; // Should throw a set accessor exception
w.Name = "fooby.torrent";
Console.WriteLine(w.Name); // Should throw a get accessor exception
f = r;
f.Name = "test2.torrent";
}
}
}

Nov 16 '05 #2

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

Similar topics

0
by: Ken | last post by:
Hi, We have a component that we've used successfully under W2000 to create virtual directories on an IIS 5.0 web server. We're in the process of upgrading to W2003 and I'm now encountering an...
0
by: Ken | last post by:
Hi, We have a component that we've used successfully under W2000 to create virtual directories on an IIS 5.0 web server. We're in the process of upgrading to W2003 and I'm now encountering an...
0
by: Ken | last post by:
Hi, We have a component that we've used successfully under W2000 to create virtual directories on an IIS 5.0 web server. We're in the process of upgrading to W2003 and I'm now encountering an...
34
by: antonyliu2002 | last post by:
I've set up the virtual smtp server on my IIS 5.1 like so: 1. Assign IP address to "All Unassigned", and listen to port 25. 2. Access Connection granted to "127.0.0.1". 3. Relay only allow...
1
by: sridhard2406 | last post by:
Hi All, I am new to c++, please find my program below and my doubt as mentioned below. #include <iostream> using namespace std; class base { public:
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
tracyyun
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...
0
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,...

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.