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

Re: revised : polymorphic object "against the grain" of C# ?

I think an overloading of the Indexer and the Add methods is quirky, but
can be justifiable, especially if all coders involved understand the
possible downsides. The plus side is that there may be less code and
increased convenience for the person coding to the class. The downside
is that it is going to be harder for humans to understand, unless they
are familiar with it, and there may be a performance penalty depending
on the kind of indexer used.

here's an example:

public class Unit
{
//this class will hold the bool and double dictionaries of values
private Dictionary<int, doublemydoubles = new Dictionary<int,
double>();
private Dictionary<int, boolmybools = new Dictionary<int, bool>();
#region Indexers
public bool this[int index]
{
get { return mybools[index]; }
set { mybools[index] = value; }
}
public double this[uint index]
{
get { return (mybools[(int)index]) ? 1d : 0d; }
set { mybools[(int)index] = (value == 1d) ? true : false; }
}
public double this[double index]
{
get { return mydoubles[(int)index]; }
set { mydoubles[(int)index] = value; }
}
#endregion
#region Add methods
public void Add(int index)
{
mybools.Add(index, false);
}
public void Add(uint index)
{
mybools.Add((int)index, false);
}
public void Add(double index)
{
mydoubles.Add((int)index, 0);
}
public void Add(int index, bool value)
{
mybools.Add(index, value);
}
public void Add(uint index, double value)
{
mybools.Add((int)index, ((value == 0)?false:true));
}
public void Add(double index, double value)
{
mydoubles.Add((int)index, value);
}
#endregion
public Unit Clone()
{
Unit temp = new PLCUnit();
foreach (KeyValuePair<int, boolkvp in mybools)
temp.Add(kvp.Key, kvp.Value);
foreach (KeyValuePair<int, doublekvp in mydoubles)
temp.Add((double)kvp.Key, kvp.Value);
return temp;
}
}

//The overloading is especially helpful when setting bools from doubles
and vice versa
//especially if this happens a lot
myUnit[1234u] = dblvalue;
//is much easier on the coder than
myUnit[1234] = ((dblvalue == 0)? false : true);


*** Sent via Developersdex http://www.developersdex.com ***
Jul 16 '08 #1
0 916

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

Similar topics

0
by: Dan | last post by:
Hi Friends: I create a Report (in ACCESS 2K) with Graph embeded on that, The Report and Graph are based on some fields of a query that have values with OPERATOR CHARACTERS set such as: "<= 1.007...
5
by: BH | last post by:
Hi what would be the C# equivalent of a VB.NET file that looks like this: Module Utilities Public Sub UtilitySubOne ( ByVal arg As String) // blah blah End Sub
12
by: Olaf Baeyens | last post by:
I am porting some of my buffer class code for C++ to C#. This C++ class allocates a block of memory using m_pBuffer=new BYTE; But since the class is also used for pointers for funtions that uses...
11
by: Antony | last post by:
I know this sounds stupid but I am going to carry on anyway. I want to create an interface that implements all methods of a form, plus another one or two. But I need to know if there is an...
71
by: Greg | last post by:
Is it your general opinion that C# is generally designed/intended/ready to replace C++? I know the answer is not black and white but please repond YES or NO (add any comments if you would like) ...
1
by: sweep | last post by:
Hi there, I am redeveloping my site to use CSS and have come up against a problem. The page I'm working on is at http://staging.proscenia.co.uk/revised%20simpler%20CSS%20floats%20version.html...
2
by: Kevin Frey | last post by:
We have a web application that, for the purposes of reporting, launches a new window to display the contents of the report in (PDF format). This is implemented using a custom IHttpHandler...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
4
by: | last post by:
Using VS.NET I am wondering what methods developers use to deploy ASP.NET website content to a remote server, either using FTP or network file copy. Ideally there would be a one-button or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.