Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 512065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:16:54+00:00 2026-05-13T07:16:54+00:00

Here is my method: private void ConvertValues() { txtResult.Text = angles[cmbUnits1.SelectedIndex]; double value1 =

  • 0

Here is my method:

private void ConvertValues()  
{  
    txtResult.Text = angles[cmbUnits1.SelectedIndex];  
    double value1 = Convert.ToDouble(txtValue1.Text);  
    double value2 = Convert.ToDouble(txtValue2.Text);  
    int unit1 = cmbUnits1.SelectedIndex;  
}

What I want the method to do is to get the selected of the ComboBoxes and test the values. But I want to know if there is an alternative to this:

if( angles[cmbUnits1.SelectedIndex].Equals("Degrees") && 
    angles[cmbUnits2.SelectedIndex].Equals("Radians")) {  
    ...  
}

By the way, I’m making a kind of unit converter so I will have sections other than angles. So I would like some enum, interface, abstract class, or class that I can implement. Maybe a class with the name Unit? So I can create new objects like Unit degrees = new Unit(Units.Angle) with Units as an enum. Or just Unit sqrMillimeters = new Unit("Area");?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:16:54+00:00Added an answer on May 13, 2026 at 7:16 am

    Sure, polymorphism. Anytime you see long switch statements or if/then/else constructs it’s always possible to handle them using polymorphism and factories. In your case I’d imagine an ITemperatureConverter interface with concrete implementations for Fahrenheit, Celcius, Kelvin, and Rankine would do nicely.

    UPDATE:

    If you find you have this logic repeated, why not have a better abstraction for an Angle than a double?

    If you’re writing in an object-oriented language, it’s a good idea to rise above primitives (e.g, double and, yes, string) to encapsulate behavior into objects.

    I’d think about an Angle class that would hang onto the value and return it as whatever measure you wanted.

    Here’s one way to do it in Java. I’ll leave the translation to C# and the rest for you.

    package angles;
    
    public class Angle
    {
        private double value;
        private AngleUnit units;
    
        public Angle()
        {
            this(0.0, AngleUnit.RADIANS);
        }
    
        public Angle(double value)
        {
            this(value, AngleUnit.RADIANS);
        }
    
        public Angle(double value, AngleUnit units)
        {
            this.value = value;
            this.units = units;
        }
    
        public double getValue()
        {
            return value;
        }
    
        public AngleUnit getUnits()
        {
            return units;
        }
    
        public Angle convert(AngleUnit newUnits)
        {
            Angle newAngle = null;
    
            if (this.units.equals(newUnits))
            {
                return this;
            }
    
            return newAngle;
        }
    }
    
    package angles;
    
    public interface AngleConverter
    {
        Angle convert(Angle angle, AngleUnit to);
    }
    
    package angles;
    
    public enum AngleUnit
    {
        DEGREES, RADIANS, GRADIANS;
    }
    
    package angles;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class DegreeConverter implements AngleConverter
    {
        private final Map<AngleUnit, Double> factors;
    
        public DegreeConverter(Map<AngleUnit, Double> factors)
        {
            this.factors = new HashMap<AngleUnit, Double>();
            this.factors.put(AngleUnit.DEGREES, 1.0);
            this.factors.put(AngleUnit.RADIANS, Math.PI/180.0);
            this.factors.put(AngleUnit.GRADIANS, 100.0/90.);
        }
    
        public Angle convert(Angle angle, AngleUnit to)
        {
            assert angle != null && to != null;
    
            return new Angle(angle.getValue()*this.factors.get(to), to);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 416k
  • Answers 416k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, validation is performed by the managed object context on… May 15, 2026 at 9:30 am
  • Editorial Team
    Editorial Team added an answer This cannot be done - To update a Jar file… May 15, 2026 at 9:30 am
  • Editorial Team
    Editorial Team added an answer The solution that worked for me was to set any… May 15, 2026 at 9:30 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.