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

Refactoring dictionary

Hi all,
I have an object (named Voice) that has a dictionary like this:

public Dictionary<string, decimal?Values;

Voice is like an Excel cell, the "string" is the name of the column and
decimal nullable is its value.
Now we have discovered that tha value is not only numeric, but can be also a
description (a string).
How can I refactor my code to incapsulate this new feature?

Thanks in advance.
--
Luigi
http://blogs.dotnethell.it/ciupaz/
Nov 3 '08 #1
4 1734
I would create a new class to encapsulate the decimal-or-string feature, for
example MyClass. When you create its instance you call pass any either a
string or a decimal to its constructor. It would then have four properties
that you can use to handle both cases:

1) object Value: the raw, indeterminate value of the cell
2) bool IsString: determines if the cell value is a string, perhaps by
'return this.Value is string';
3) string StringValue { get { return this.Value as string; } }
3) decimal? DecimalValue { get { return this.Value as decimal?; } }

Then your dictionary would be of type

public Dictionary<string, MyClassValues;

Let me know if this design is acceptable in your situation.
--
Stanimir Stoyanov
http://stoyanoff.info

"Luigi" <ci****************@inwind.itwrote in message
news:A0**********************************@microsof t.com...
Hi all,
I have an object (named Voice) that has a dictionary like this:

public Dictionary<string, decimal?Values;

Voice is like an Excel cell, the "string" is the name of the column and
decimal nullable is its value.
Now we have discovered that tha value is not only numeric, but can be also
a
description (a string).
How can I refactor my code to incapsulate this new feature?

Thanks in advance.
--
Luigi
http://blogs.dotnethell.it/ciupaz/
Nov 3 '08 #2
"Stanimir Stoyanov" wrote:
I would create a new class to encapsulate the decimal-or-string feature, for
example MyClass. When you create its instance you call pass any either a
string or a decimal to its constructor. It would then have four properties
that you can use to handle both cases:

1) object Value: the raw, indeterminate value of the cell
2) bool IsString: determines if the cell value is a string, perhaps by
'return this.Value is string';
3) string StringValue { get { return this.Value as string; } }
3) decimal? DecimalValue { get { return this.Value as decimal?; } }

Then your dictionary would be of type

public Dictionary<string, MyClassValues;

Let me know if this design is acceptable in your situation.
Mmhh very interesting Stanimir.
Now I'm trying to implement it.

Thanks for now.

Luigi
Nov 3 '08 #3
Hi had written in this way:

public class Valore
{
private object Value;
public string StringValue { get { return this.Value as string; } }
public decimal? DecimalValue { get { return this.Value as decimal?;
} }

public bool IsString()
{
return this.Value is string;
}
}

but how can I set or instantiate my class "Valore"?

L
Nov 3 '08 #4
Good idea to have 'value' as a private field (so I changed the casing per
the coding guidelines). Below are all properties + the two constructors.

public class Valore
{
private object value;

public Valore(decimal? value) { this.value = value; }
public Valore(string value) { this.value = value; }

public string StringValue { get { return this.value as
string; } }
public decimal? DecimalValue { get { return this.value as
decimal?; } }

public bool IsString() { return this.value is string; }
}

Usage is:

Valore v1 = new Valore((decimal?)99.5);
Valore v2 = new Valore("test");

if (v1.IsString())
{
// Do something with v1.StringValue
}
else
{
// Do something with v1.DecimalValue
}

if (v2.IsString())
{
// Do something with v2.StringValue
}
else
{
// Do something with v2.DecimalValue
}

--
Stanimir Stoyanov
http://stoyanoff.info

"Luigi" <ci****************@inwind.itwrote in message
news:B7**********************************@microsof t.com...
Hi had written in this way:

public class Valore
{
private object Value;
public string StringValue { get { return this.Value as string; } }
public decimal? DecimalValue { get { return this.Value as decimal?;
} }

public bool IsString()
{
return this.Value is string;
}
}

but how can I set or instantiate my class "Valore"?

L
Nov 3 '08 #5

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

Similar topics

15
by: Frans Englich | last post by:
As continuation to a previous thread, "PyChecker messages", I have a question regarding code refactoring which the following snippet leads to: > > runner.py:200: Function (detectMimeType) has too...
9
by: Peter Dembinski | last post by:
I am trying to write Master Thesis on refactoring Python code. Where should I look for information? -- http://www.dembiński.prv.pl
4
by: | last post by:
Hi, Refactoring a winform causes problems if moving this form to a subdirectory in the same project. What is the workaround for this and will this be fixed in future? Thanks /BOB
0
by: Andre Baresel | last post by:
Hello together, just a year ago I was searching arround for a tool supporting refactoring for c++. I've seen implementations for java and was impressed how an IDE can help with such a feature....
2
by: Sachin Garg | last post by:
Hi, I was trying to find (like many others here) a tool for refactoring C++ code as I have lately been noticing that I spend most of my coding time doing refactoring and some refactoring which...
6
by: Dean Ware | last post by:
Hi, I am part way through developing a C++ application. I am developing it on my own and using VC++ 6.0. I am now at the stage where I wish to start tidying up my code. I have lots of...
8
by: Frank Rizzo | last post by:
I keep hearing this term thrown around. What does it mean in the context of code? Can someone provide a definition and example using concrete code? Thanks.
15
by: Simon Cooke | last post by:
Does anyone know of any tools for refactoring header files? We're using a third party codebase at work, and pretty much every file includes a 50Mb precompiled header file. I'm looking for a tool...
2
by: HendrikLeder | last post by:
Hello everybody :-) Next year I`ll write my diploma in computer science for business (It`s a degree in Germany) and I`ve some questions about the topic. The diploma will handle about refactoring...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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...

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.