Refactoring dictionary

  • Thread starter Thread starter Luigi
  • Start date Start date
L

Luigi

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.
 
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, MyClass> Values;

Let me know if this design is acceptable in your situation.
 
Stanimir Stoyanov said:
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, MyClass> Values;

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
 
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
 
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
}
 
Back
Top