C#: Windows form which radio button is checked

Hi

I'm new to C# and .NET so forgive if I'm asking stupid questions
i'm going through some exercises from a recent course and hit some questions
1.
I have a windows form with 4 radio buttons

Given the code below is there a better way of establishing which radio button is checked and returning the first letter?
This seems long winded to me

if (AddRadio.Checked)
{
game.Operation = "A";
}
else if (SubtractRadio.Checked)
{
game.Operation = "S";
}
else if (MultiplyRadio.Checked)
{
game.Operation = "M";
}
else if (DivideRadio.Checked)
{
game.Operation = "D";
}

Open in new window



In another exercise this returns 0 but equates correctly  if replace  (5 / 9) with 0.5555555555555556

        private double FahrenheitToCelsius()
        {

  
            return (ValueToConvert - 32) *  (5 / 9); //0.5555555555555556;

        }

Open in new window

LVL 1
trevor1940Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
for your second question, you are dividing by integers so the compilers returns an integer. You just need to specify that you are dealing with double here:
private double FahrenheitToCelsius()
{
            return (ValueToConvert - 32) *  (5 / 9d); 
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Éric MoreauSenior .Net ConsultantCommented:
for the first part, there are ways but it involves keeping private members so I would say that if you only 4 values, better keep your current mechanism.
0
it_saigeDeveloperCommented:
Answer to your first question, if you are using the first letter of the checked radio button, you could simply use the CheckChanged event in order to parse the first letter from the control name; e.g. -

Form1.cs -
using System;
using System.Windows.Forms;

namespace EE_Q29082954
{
    public partial class Form1 : Form
    {
        RadioButton lastChecked = default(RadioButton);

        public Form1()
        {
            InitializeComponent();
        }

        void OnCheckedChanged(object sender, EventArgs e)
        {
            if (sender is RadioButton)
            {
                var radio = sender as RadioButton;
                if (radio.Checked)
                {
                    lastChecked = radio;
                }
            }
        }

        private void OnClick(object sender, EventArgs e)
        {
            if (sender is Button)
            {
                var btn = sender as Button;
                if (btn.Equals(button1))
                {
                    MessageBox.Show($"Radio Button - {lastChecked.Name}; is checked.{Environment.NewLine}The first letter of {lastChecked.Name} is {lastChecked.Name.Substring(0, 1)}.");
                }
            }
        }

        private void OnLoad(object sender, EventArgs e)
        {
            lastChecked = AddRadio;
        }
    }
}

Open in new window

Form1.Designer.cs -
using System;

namespace EE_Q29082954
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.AddRadio = new System.Windows.Forms.RadioButton();
            this.SubtractRadio = new System.Windows.Forms.RadioButton();
            this.MultiplyRadio = new System.Windows.Forms.RadioButton();
            this.DivideRadio = new System.Windows.Forms.RadioButton();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // AddRadio
            // 
            this.AddRadio.AutoSize = true;
            this.AddRadio.Location = new System.Drawing.Point(12, 12);
            this.AddRadio.Name = "AddRadio";
            this.AddRadio.Size = new System.Drawing.Size(44, 17);
            this.AddRadio.TabIndex = 0;
            this.AddRadio.TabStop = true;
            this.AddRadio.Text = "Add";
            this.AddRadio.UseVisualStyleBackColor = true;
            this.AddRadio.CheckedChanged += new System.EventHandler(this.OnCheckedChanged);
            // 
            // SubtractRadio
            // 
            this.SubtractRadio.AutoSize = true;
            this.SubtractRadio.Location = new System.Drawing.Point(12, 35);
            this.SubtractRadio.Name = "SubtractRadio";
            this.SubtractRadio.Size = new System.Drawing.Size(65, 17);
            this.SubtractRadio.TabIndex = 1;
            this.SubtractRadio.TabStop = true;
            this.SubtractRadio.Text = "Subtract";
            this.SubtractRadio.UseVisualStyleBackColor = true;
            this.SubtractRadio.CheckedChanged += new System.EventHandler(this.OnCheckedChanged);
            // 
            // MultiplyRadio
            // 
            this.MultiplyRadio.AutoSize = true;
            this.MultiplyRadio.Location = new System.Drawing.Point(12, 58);
            this.MultiplyRadio.Name = "MultiplyRadio";
            this.MultiplyRadio.Size = new System.Drawing.Size(60, 17);
            this.MultiplyRadio.TabIndex = 2;
            this.MultiplyRadio.TabStop = true;
            this.MultiplyRadio.Text = "Multiply";
            this.MultiplyRadio.UseVisualStyleBackColor = true;
            this.MultiplyRadio.CheckedChanged += new System.EventHandler(this.OnCheckedChanged);
            // 
            // DivideRadio
            // 
            this.DivideRadio.AutoSize = true;
            this.DivideRadio.Location = new System.Drawing.Point(12, 81);
            this.DivideRadio.Name = "DivideRadio";
            this.DivideRadio.Size = new System.Drawing.Size(55, 17);
            this.DivideRadio.TabIndex = 3;
            this.DivideRadio.TabStop = true;
            this.DivideRadio.Text = "Divide";
            this.DivideRadio.UseVisualStyleBackColor = true;
            this.DivideRadio.CheckedChanged += new System.EventHandler(this.OnCheckedChanged);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(197, 226);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 4;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.OnClick);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.DivideRadio);
            this.Controls.Add(this.MultiplyRadio);
            this.Controls.Add(this.SubtractRadio);
            this.Controls.Add(this.AddRadio);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.OnLoad);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.RadioButton AddRadio;
        private System.Windows.Forms.RadioButton SubtractRadio;
        private System.Windows.Forms.RadioButton MultiplyRadio;
        private System.Windows.Forms.RadioButton DivideRadio;
        private System.Windows.Forms.Button button1;
    }
}

Open in new window

Which produces the following output -Capture.PNG
As for the second part of your question, you are performing integer division, in integer division 5 / 9 = 0.
If you want to perform decimal, float or double division, you need to specify that either the divisor or quotient are decimals/floats/doubles; e.g. -
using System;

namespace EE_Q29082954_Console
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"Value of 5 / 9 = {5 / 9}");
            Console.WriteLine($"Value of 5 / 9 = {5 / 9f}");
            Console.WriteLine($"32F = {FarenheitToCelcius(32)}C");
            Console.ReadLine();
        }

        static double FarenheitToCelcius(double value)
        {
            return (value - 32) * (5 / 9f);
        }
    }
}

Open in new window

Produces the following output -Capture.PNG
-saige-
1
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

trevor1940Author Commented:
Thank You for help and advise
0
it_saigeDeveloperCommented:
Or as Eric had stated...  LOL...  ;)  Beat me to it Eric.

-saige-
0
trevor1940Author Commented:
Unsure how but when I accepted Eric solution it was the only one sorry
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.