Getters and Setters not saving variable to output

Viewed 66

Ive been trying for hours to fix this code I just can't seem to understand the problem, I'm storing a variable with get and set inside of a public containing a case. What I'm trying to do is use this menu below to store the variable.

          {

              
              Volume vol = new Volume();
              
              
              Console.WriteLine("\n                                                   ---------------\n" +
                  "                                             |>>>>> Control Panel <<<<<|\n" +
                  "                                                   ---------------" +
                 "\n\n                                    |===========================================|\n" +
                 "\n                                                    Press key...\n" +
                  "\n                                             [Q] to Change M Main 1 Volume\n" +
                  "                                             [W] to Change M Main 2 Volume\n" +
                  "                                             [E] to Change Host Volume\n" +
                  "                                             [R] to Change Guest Volume\n" +
                  "                                             [T] to Change Speaker Volume\n" +
                  "                                             [Y] to Change Lights\n" +
                  "                                             [G] to exit\n" +
                 "\n                                    |===========================================|");

              ConsoleKeyInfo inputuser = Console.ReadKey(true);
              switch (inputuser.Key)
              {
                  case ConsoleKey.Q:
                      {
                          try
                          {


                              Console.WriteLine("| | |Input Volume percentege from 0-100 %| | |");
                              vol.MainMic1 = int.Parse(Console.ReadLine());

                              if (vol.MainMic1 > 100 || vol.MainMic1 < 0)
                              {
                                  Console.WriteLine("Invalid Number");
                                  Console.ReadKey();
                              }
                              else if (vol.MainMic1 == 0 || vol.MainMic1 <= 100)

                                  
                                  Console.WriteLine(vol.MainMic1);
                              Console.ReadKey(); ```

This sets it in this current method because I tried it but not in the method I use to call it, when I call it in console it returns its original value or 0

 public void ShowVolume()
            {

                Volume voll = new Volume();

                Console.Clear();
                Console.WriteLine("|======     ======     ======     ======     ======     ======     ======|\n" +
                    "\n                  >>>>> Lights and Volume settings <<<<<\n" +
                    "\nMain 1(Peterson) Microphone currently at:       {0}%     volume.\n" +
                    "Co-Speaker(Weiss) Microphone currently at:      {1}%     volume.\n" +
                    "Host Microphone currently at:                   {2}%     volume.\n" +
                    "Guest Microphone currently at:                  {3}%      volume.\n" +
                    "Quad-Speakers currently at:                     {4}%     volume.\n" +
                    "Lights are at:                                  {5}%     power.\n" +
                    "\n|======     ======     ======     ======     ======     ======     ======|", voll.MainMic1, voll.MainMic2, voll.MainMic3, voll.GuestMic, voll.QuadSpeaker, voll.Lighting);


                Console.ReadKey();

I can't figure out the problem, I get everything to run, but not to print what is asked, is there some sort of case /method blockage here I don't understand?


here is class with get/set

  public class Volume
    {

        private int o_mainmic1 = 50;
        private int o_mainmic2 = 50;
        private int o_mainmic3 = 50;
        private int o_guestmic = 30;
        private int o_quadspeaker = 30;
        private int o_lighting = 30;

        public int MainMic1
        {
            get
            {
                return o_mainmic1;
            }

            set
            {
                o_mainmic1 = value;
            }
        }

Here are some Gyazo pictures if that is of help to you, I appreaciate any assistance I get here, thanks in advance.

https://gyazo.com/d241e1f8db821ba38415ecf1c57fb3f9

https://gyazo.com/35f31140568794aa41ee74c8ead2296a

https://gyazo.com/1359b4a291baa385b8dcee586a3b97ea

https://gyazo.com/1ce9e237fdd56a7f1a40dabd24c50b5a ```

1 Answers

Step 1: Make Volume static:

  public static class Volume
    {

        private int o_mainmic1 = 50;
        private int o_mainmic2 = 50;
        private int o_mainmic3 = 50;
        private int o_guestmic = 30;
        private int o_quadspeaker = 30;
        private int o_lighting = 30;

        public int MainMic1
        {
            get
            {
                return o_mainmic1;
            }

            set
            {
                o_mainmic1 = value;
            }
        }

Step 2: Rename every mention of vol or voll to Volume and don't initialize the Volume class. [first code sample]

          {
              Volume vol = new Volume(); // Delete this, this is not needed anymore since Volume is a static class.

/* I skipped over the Console.WriteLine stuff, though you need it.
 */
              ConsoleKeyInfo inputuser = Console.ReadKey(true);
              switch (inputuser.Key)
              {
                  case ConsoleKey.Q:
                      {
                          try
                          {


                              Console.WriteLine("| | |Input Volume percentege from 0-100 %| | |");

                              Volume.MainMic1 = int.Parse(Console.ReadLine());

                              if (Volume.MainMic1 > 100 || Volume.MainMic1 < 0)
                              {
                                  Console.WriteLine("Invalid Number");
                                  Console.ReadKey();
                              }
                              else if (Volume.MainMic1 == 0 || Volume.MainMic1 <= 100)
                              {
                                  Console.WriteLine(vol.MainMic1);
                                  Console.ReadKey();
                              }

[second code sample]

 public void ShowVolume()
            {

                Volume voll = new Volume(); //Again, delete this.

                Console.Clear();
                Console.WriteLine("|======     ======     ======     ======     ======     ======     ======|\n" +
                    "\n                  >>>>> Lights and Volume settings <<<<<\n" +
                    "\nMain 1(Peterson) Microphone currently at:       {0}%     volume.\n" +
                    "Co-Speaker(Weiss) Microphone currently at:      {1}%     volume.\n" +
                    "Host Microphone currently at:                   {2}%     volume.\n" +
                    "Guest Microphone currently at:                  {3}%      volume.\n" +
                    "Quad-Speakers currently at:                     {4}%     volume.\n" +
                    "Lights are at:                                  {5}%     power.\n" +
                    "\n|======     ======     ======     ======     ======     ======     ======|", Volume.MainMic1, Volume.MainMic2, Volume.MainMic3, Volume.GuestMic, Volume.QuadSpeaker, Volume.Lighting);


                Console.ReadKey();
Related