Refactoring dictionary

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?THVpZ2k=?=

    Refactoring dictionary

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

    public Dictionary<stri ng, 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

  • Stanimir Stoyanov

    #2
    Re: Refactoring dictionary

    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<stri ng, MyClassValues;

    Let me know if this design is acceptable in your situation.
    --
    Stanimir Stoyanov
    메이저사이트 순위 먹튀검증이 완료된 토토사이트 커뮤니티 - 토토핫 입니다. 저희 토토핫에서는 베팅할때 필수로 점검해야 되는 안전 가이드를 제공하며 안전한 메이저 놀이터 목록과 메이저사이트 먹튀검증 꽁머니사이트를 추천드립니다.


    "Luigi" <ciupazNoSpamGr azie@inwind.itw rote in message
    news:A0653707-239A-4B32-8CC4-5A5FD586DFA1@mi crosoft.com...
    Hi all,
    I have an object (named Voice) that has a dictionary like this:
    >
    public Dictionary<stri ng, 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/

    Comment

    • =?Utf-8?B?THVpZ2k=?=

      #3
      Re: Refactoring dictionary

      "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<stri ng, 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

      Comment

      • =?Utf-8?B?THVpZ2k=?=

        #4
        Re: Refactoring dictionary

        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

        Comment

        • Stanimir Stoyanov

          #5
          Re: Refactoring dictionary

          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
          메이저사이트 순위 먹튀검증이 완료된 토토사이트 커뮤니티 - 토토핫 입니다. 저희 토토핫에서는 베팅할때 필수로 점검해야 되는 안전 가이드를 제공하며 안전한 메이저 놀이터 목록과 메이저사이트 먹튀검증 꽁머니사이트를 추천드립니다.


          "Luigi" <ciupazNoSpamGr azie@inwind.itw rote in message
          news:B7A6611B-2A91-4C50-84E3-039F5345BE65@mi crosoft.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

          Comment

          Working...