473,320 Members | 1,839 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,320 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 2315
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.