?
Solved

Getting the integer value behind an enumeration

Posted on 2007-08-01
5
Medium Priority
?
233 Views
Last Modified: 2010-04-15
I need some help working with enumerations.

I have this class I wrote:


    public class CommonFinancedItems
    {
        private AmortizationPeriodType _amortType;

        public CommonFinancedItems()
        {
        }

        public enum AmortizationPeriodType : int
        {
            Weekly = 52,
            BiWeekly = 24,
            Monthly = 12
        }
        public AmortizationPeriodType AmortPeriod
        {
            get
            {
                return _amortType;
            }
            set
            {
                _amortType = value;
            }
        }



In another class I have some Properties setup which attempt to use the enumeration (exposed through the class above) in order to return the right number of MONTHS accrued during a loan pay-off, BASED UPON what the Amortization Period was  (Monthly, BiWeekly or Weekly)

Here are the Properties:

        public int HelocMonthsAccrued
        {
            get
            {
                int months = (_helocMonthsAccrued / ((int)this._commonFinancedItem.AmortPeriod) / 12);
                return months;
            }            
        }
       
        public int MortgageMonthsAccrued
        {
            get
            {
                int AmortPeriodDivisor = (int)this._commonFinancedItem.AmortPeriod;
                int divisor = 12;
                int months = this._mortMonthsAccrued / (AmortPeriodDivisor / divisor);
                return months;
            }            
        }



The Property "MortgageMonthsAccrued" used to look more like the property "HelocMonthsAccrued"....but I started to break-it-apart a little bit more so I could understand...pinpoint....the problem a little better.

It seems that when this line runs in the getter for the property "MortgageMonthsAccrued" the result is 0:


   int AmortPeriodDivisor = (int)this._commonFinancedItem.AmortPeriod;

after this line runs.......AmortPeriodDivisor equals  0, instead of 12, 24 or 52   (the values behind the enumerations "Monthly", "BiWeekly" and "Weekly" respectively)

I'm even casting the enumeration to an INT first  (because of some other complaints I got early on about trying to use enumerations this way).


What am I not getting?   :)
0
Comment
Question by:Tom Knowlton
  • 2
  • 2
5 Comments
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 19611429
this works fine for me

        public enum Test : int
        {
            one = 5,
            two = 6,
            three = 8
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show(((int)Test.one).ToString());
        }
0
 
LVL 20

Accepted Solution

by:
REA_ANDREW earned 1000 total points
ID: 19611440
the output is a messagebox which displays 5

Andrew
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 19611948
Huh...I took another look at this......and sometimes it seems to work and other times it does not:

int AmortPeriodDivisor = (int)this._commonFinancedItem.AmortPeriod;

Sometimes AmortPeriodDivisor equals 12 (or some other valid enumeration value)

othertimes...it does indeed equal 0.


So I've got some other problem...perhaps with scope or initialization?
0
 
LVL 10

Assisted Solution

by:Nash2334
Nash2334 earned 1000 total points
ID: 19612642
How is _amortType/AmortPeriod set?  You will get 0 back if it is null since you are casting your enumerated type from an int.  That is what I suspect is occurring here.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 19612721
Nash:

I think you just gave me a big clue.

Let me do some checking, and I'll get back to you on this.

Thanks,

Tom
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question