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

Overloading properties?

ORC
Is it possible to overload properties - and if so - how?

Mayby there is another and better way to do what I'm trying to do so if you
have any suggestion I would be happy:

I have a number of properties to set and get values, but I want my
properties to be able to return a type that is corresponding to the target
variabel (and also be able to receive a type corresponding to the source)?

Thanks,
Ole
Nov 16 '05 #1
4 11727
Is it possible to overload properties - and if so - how?
No

I have a number of properties to set and get values, but I want my
properties to be able to return a type that is corresponding to the target
variabel (and also be able to receive a type corresponding to the source)?


What if there is no target variable? Even if there is, there's no way
to know from inside a property getter.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Hi,

Short answer: NO

to overload a method you need a different set of arguments, a different
return value cannot be used to overload, why? because it would be terribly
difficult, if not impossible to solve the correct instance of the method
when assigning to a variable of not the exact type declared in the method.

With that in mind AND remembering that the properties are methods in real
life is why you cannot do it.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"ORC" <or*@sol.dk> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Is it possible to overload properties - and if so - how?

Mayby there is another and better way to do what I'm trying to do so if
you
have any suggestion I would be happy:

I have a number of properties to set and get values, but I want my
properties to be able to return a type that is corresponding to the target
variabel (and also be able to receive a type corresponding to the source)?

Thanks,
Ole

Nov 16 '05 #3
Well, using an old trick I learned in C++, it can be done, sorta, but it's
still probably not a good idea:

public class A
{
private int _n;
public A(int n)
{
_n = n;
}
public AHelper TimesTwo
{
get
{
return new AHelper(this);
}
}
public int N
{
get { return _n; }
}
}

public class AHelper
{
private A _a;
public AHelper(A a)
{
_a = a;
}
public static implicit operator int(AHelper ah)
{
return ah._a.N *2;
}
public static implicit operator string (AHelper ah)
{
return String.Format("The value is {0}", ah._a.N *2);
}
}
public class MyClass
{
public static void Main()
{
A aa = new A(5);
int i = aa.TimesTwo;
string s = aa.TimesTwo;

Console.WriteLine("int ='{0}', string='{1}'", i, s);
}

}

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"ORC" <or*@sol.dk> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Is it possible to overload properties - and if so - how?

Mayby there is another and better way to do what I'm trying to do so if you have any suggestion I would be happy:

I have a number of properties to set and get values, but I want my
properties to be able to return a type that is corresponding to the target
variabel (and also be able to receive a type corresponding to the source)?

Thanks,
Ole

Nov 16 '05 #4
You have to make your property use type "object" and sort it out
internally, I'm afraid.

It's not clear from your post, but maybe what you're more interested in
is generic types. Read up on "Whidbey", which is .NET Framework 2.0 and
all of the C# enhancements that come with it. Generics are one of the
heavy hitting changes.

Nov 16 '05 #5

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

Similar topics

17
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
4
by: John Smith | last post by:
can we overload a javascript function with different argument? example: function a(a){} function a(a,b){}
4
by: Ook | last post by:
I'm a but fuzzy about this. You would call it like this:Polynomial poly; // Polynomial is my class with various properties cin >> poly; In the function itself, I would, for example, do the...
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
18
by: uday | last post by:
Does C supports overloading. I am thinking no. But some people are saying yes. If yes how. Please answer with examples. Also in c++ size of empty class is one. why. and what are the default...
8
by: John Hardin | last post by:
All: The syntax for overloading the "+" and other simple binary math operators is pretty straightforward, but I can't seem to wrap my brain around the idea that overloading the "+" operator...
15
by: Brian Haynes | last post by:
I am having a problem with overloading. An example: Public Class Base ... End Class Public Class Derived Inherits Base ... End Class
4
by: Doug | last post by:
Hi I am 'returning' to the learning of C# after a change in jobs and I remember that I used to struggle with the defintion, purpose and function of 'overloading'. When I was beginning with C# I...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
Oralloy
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,...
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...

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.