Passing a value to a class keeps returning 0









up vote
-1
down vote

favorite












this might be a simple/dumb question but I'm trying to figure out why my code keeps returning 0. I don't think my syntax for passing the value is correct but I cant figure out the proper way of doing it.



 class ICESMARK

static int ICECount = 0;
public double average = 0;

public double ICES = new double[8];
public ICESMARK(double Mark)

Mark = ICES[ICECount];
if (ICECount == (ICES.Length - 1))


for (int x = 0; x < ICES.Length; x++)

average += ICES[x];


average /= ICES.Length;


ICECount++;



class Program

static void Main(string args)

ICESMARK ICE = new ICESMARK[8];
//LABSMARK LAB = new LABSMARK[6];
double userInput;

for (int counter = 0; counter < ICE.Length ; counter++)

Console.Write("Please enter your mark for ICE0: ", counter + 1 );
bool ifInt = double.TryParse(Console.ReadLine(), out userInput);
ICE[counter] = new ICESMARK(userInput);


Console.WriteLine(ICE[1].average);
Console.ReadLine();




ICE[1].average - Displays 0



Also if anyone has a more efficient way of doing this, feel free to let me know. Except for the average, is gotta be a calculation, cant use the built in method.










share|improve this question























  • where are you using marks parameter which you passing to the constructor? Did not get your code sorry.. What you want to achieve?
    – Prasad Telkikar
    Nov 9 at 6:16







  • 5




    There are a lot of problems here. You should be defining a function that takes values, has a return type, and returns a value. Instead you're directly modifying members of the class you've defined as public. The point of a class is to encapsulate data, and only expose what is needed.
    – Brian White
    Nov 9 at 6:18










  • like you get the list of ints from the user, then pass them into a getAvg function. I'm not sure why your constructor calculates an average. Just change that to a function, and pass in List<int> or int
    – Brian White
    Nov 9 at 6:21










  • Okay sorry if is messy, I'm very new, specially to classes/methods. ICE = quiz, I want the user to input the mark he got on each quiz, and calculate his average.
    – Brian Naranjo
    Nov 9 at 6:21






  • 2




    also Mark = ICES[ICECount]; Is this some kind of 2020 technology or why are setting Mark to 0 even before the ICES are set... your Mark parameter becomes useless. Your code is waay to messy, sorry. That's hard to help
    – sLw
    Nov 9 at 6:50














up vote
-1
down vote

favorite












this might be a simple/dumb question but I'm trying to figure out why my code keeps returning 0. I don't think my syntax for passing the value is correct but I cant figure out the proper way of doing it.



 class ICESMARK

static int ICECount = 0;
public double average = 0;

public double ICES = new double[8];
public ICESMARK(double Mark)

Mark = ICES[ICECount];
if (ICECount == (ICES.Length - 1))


for (int x = 0; x < ICES.Length; x++)

average += ICES[x];


average /= ICES.Length;


ICECount++;



class Program

static void Main(string args)

ICESMARK ICE = new ICESMARK[8];
//LABSMARK LAB = new LABSMARK[6];
double userInput;

for (int counter = 0; counter < ICE.Length ; counter++)

Console.Write("Please enter your mark for ICE0: ", counter + 1 );
bool ifInt = double.TryParse(Console.ReadLine(), out userInput);
ICE[counter] = new ICESMARK(userInput);


Console.WriteLine(ICE[1].average);
Console.ReadLine();




ICE[1].average - Displays 0



Also if anyone has a more efficient way of doing this, feel free to let me know. Except for the average, is gotta be a calculation, cant use the built in method.










share|improve this question























  • where are you using marks parameter which you passing to the constructor? Did not get your code sorry.. What you want to achieve?
    – Prasad Telkikar
    Nov 9 at 6:16







  • 5




    There are a lot of problems here. You should be defining a function that takes values, has a return type, and returns a value. Instead you're directly modifying members of the class you've defined as public. The point of a class is to encapsulate data, and only expose what is needed.
    – Brian White
    Nov 9 at 6:18










  • like you get the list of ints from the user, then pass them into a getAvg function. I'm not sure why your constructor calculates an average. Just change that to a function, and pass in List<int> or int
    – Brian White
    Nov 9 at 6:21










  • Okay sorry if is messy, I'm very new, specially to classes/methods. ICE = quiz, I want the user to input the mark he got on each quiz, and calculate his average.
    – Brian Naranjo
    Nov 9 at 6:21






  • 2




    also Mark = ICES[ICECount]; Is this some kind of 2020 technology or why are setting Mark to 0 even before the ICES are set... your Mark parameter becomes useless. Your code is waay to messy, sorry. That's hard to help
    – sLw
    Nov 9 at 6:50












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











this might be a simple/dumb question but I'm trying to figure out why my code keeps returning 0. I don't think my syntax for passing the value is correct but I cant figure out the proper way of doing it.



 class ICESMARK

static int ICECount = 0;
public double average = 0;

public double ICES = new double[8];
public ICESMARK(double Mark)

Mark = ICES[ICECount];
if (ICECount == (ICES.Length - 1))


for (int x = 0; x < ICES.Length; x++)

average += ICES[x];


average /= ICES.Length;


ICECount++;



class Program

static void Main(string args)

ICESMARK ICE = new ICESMARK[8];
//LABSMARK LAB = new LABSMARK[6];
double userInput;

for (int counter = 0; counter < ICE.Length ; counter++)

Console.Write("Please enter your mark for ICE0: ", counter + 1 );
bool ifInt = double.TryParse(Console.ReadLine(), out userInput);
ICE[counter] = new ICESMARK(userInput);


Console.WriteLine(ICE[1].average);
Console.ReadLine();




ICE[1].average - Displays 0



Also if anyone has a more efficient way of doing this, feel free to let me know. Except for the average, is gotta be a calculation, cant use the built in method.










share|improve this question















this might be a simple/dumb question but I'm trying to figure out why my code keeps returning 0. I don't think my syntax for passing the value is correct but I cant figure out the proper way of doing it.



 class ICESMARK

static int ICECount = 0;
public double average = 0;

public double ICES = new double[8];
public ICESMARK(double Mark)

Mark = ICES[ICECount];
if (ICECount == (ICES.Length - 1))


for (int x = 0; x < ICES.Length; x++)

average += ICES[x];


average /= ICES.Length;


ICECount++;



class Program

static void Main(string args)

ICESMARK ICE = new ICESMARK[8];
//LABSMARK LAB = new LABSMARK[6];
double userInput;

for (int counter = 0; counter < ICE.Length ; counter++)

Console.Write("Please enter your mark for ICE0: ", counter + 1 );
bool ifInt = double.TryParse(Console.ReadLine(), out userInput);
ICE[counter] = new ICESMARK(userInput);


Console.WriteLine(ICE[1].average);
Console.ReadLine();




ICE[1].average - Displays 0



Also if anyone has a more efficient way of doing this, feel free to let me know. Except for the average, is gotta be a calculation, cant use the built in method.







c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 6:42









Mrinal Kamboj

8,21821946




8,21821946










asked Nov 9 at 6:12









Brian Naranjo

6318




6318











  • where are you using marks parameter which you passing to the constructor? Did not get your code sorry.. What you want to achieve?
    – Prasad Telkikar
    Nov 9 at 6:16







  • 5




    There are a lot of problems here. You should be defining a function that takes values, has a return type, and returns a value. Instead you're directly modifying members of the class you've defined as public. The point of a class is to encapsulate data, and only expose what is needed.
    – Brian White
    Nov 9 at 6:18










  • like you get the list of ints from the user, then pass them into a getAvg function. I'm not sure why your constructor calculates an average. Just change that to a function, and pass in List<int> or int
    – Brian White
    Nov 9 at 6:21










  • Okay sorry if is messy, I'm very new, specially to classes/methods. ICE = quiz, I want the user to input the mark he got on each quiz, and calculate his average.
    – Brian Naranjo
    Nov 9 at 6:21






  • 2




    also Mark = ICES[ICECount]; Is this some kind of 2020 technology or why are setting Mark to 0 even before the ICES are set... your Mark parameter becomes useless. Your code is waay to messy, sorry. That's hard to help
    – sLw
    Nov 9 at 6:50
















  • where are you using marks parameter which you passing to the constructor? Did not get your code sorry.. What you want to achieve?
    – Prasad Telkikar
    Nov 9 at 6:16







  • 5




    There are a lot of problems here. You should be defining a function that takes values, has a return type, and returns a value. Instead you're directly modifying members of the class you've defined as public. The point of a class is to encapsulate data, and only expose what is needed.
    – Brian White
    Nov 9 at 6:18










  • like you get the list of ints from the user, then pass them into a getAvg function. I'm not sure why your constructor calculates an average. Just change that to a function, and pass in List<int> or int
    – Brian White
    Nov 9 at 6:21










  • Okay sorry if is messy, I'm very new, specially to classes/methods. ICE = quiz, I want the user to input the mark he got on each quiz, and calculate his average.
    – Brian Naranjo
    Nov 9 at 6:21






  • 2




    also Mark = ICES[ICECount]; Is this some kind of 2020 technology or why are setting Mark to 0 even before the ICES are set... your Mark parameter becomes useless. Your code is waay to messy, sorry. That's hard to help
    – sLw
    Nov 9 at 6:50















where are you using marks parameter which you passing to the constructor? Did not get your code sorry.. What you want to achieve?
– Prasad Telkikar
Nov 9 at 6:16





where are you using marks parameter which you passing to the constructor? Did not get your code sorry.. What you want to achieve?
– Prasad Telkikar
Nov 9 at 6:16





5




5




There are a lot of problems here. You should be defining a function that takes values, has a return type, and returns a value. Instead you're directly modifying members of the class you've defined as public. The point of a class is to encapsulate data, and only expose what is needed.
– Brian White
Nov 9 at 6:18




There are a lot of problems here. You should be defining a function that takes values, has a return type, and returns a value. Instead you're directly modifying members of the class you've defined as public. The point of a class is to encapsulate data, and only expose what is needed.
– Brian White
Nov 9 at 6:18












like you get the list of ints from the user, then pass them into a getAvg function. I'm not sure why your constructor calculates an average. Just change that to a function, and pass in List<int> or int
– Brian White
Nov 9 at 6:21




like you get the list of ints from the user, then pass them into a getAvg function. I'm not sure why your constructor calculates an average. Just change that to a function, and pass in List<int> or int
– Brian White
Nov 9 at 6:21












Okay sorry if is messy, I'm very new, specially to classes/methods. ICE = quiz, I want the user to input the mark he got on each quiz, and calculate his average.
– Brian Naranjo
Nov 9 at 6:21




Okay sorry if is messy, I'm very new, specially to classes/methods. ICE = quiz, I want the user to input the mark he got on each quiz, and calculate his average.
– Brian Naranjo
Nov 9 at 6:21




2




2




also Mark = ICES[ICECount]; Is this some kind of 2020 technology or why are setting Mark to 0 even before the ICES are set... your Mark parameter becomes useless. Your code is waay to messy, sorry. That's hard to help
– sLw
Nov 9 at 6:50




also Mark = ICES[ICECount]; Is this some kind of 2020 technology or why are setting Mark to 0 even before the ICES are set... your Mark parameter becomes useless. Your code is waay to messy, sorry. That's hard to help
– sLw
Nov 9 at 6:50












2 Answers
2






active

oldest

votes

















up vote
2
down vote













Simplest code to get your work done:



void Main()

double ICE = new double[8];

double userInput = 0.0;

for (int counter = 0; counter < ICE.Length; counter++)

Console.WriteLine($"Please enter your mark for ICE counter: ");

bool isNumerical = false;

while(!isNumerical)
isNumerical = double.TryParse(Console.ReadLine(), out userInput);

ICE[counter] = userInput;


Console.WriteLine("Average : " + ICE.Average());
Console.ReadLine();




How it works:



  1. Removed all the frills, you don't need an extra class just for this purpose

  2. Made it mandatory for code to enter valid double value to fill all the slots

  3. Finally used Linq Average to calculate Average value





share|improve this answer





























    up vote
    0
    down vote



    accepted










    I want to clarify that yes there are easier ways to solve this, but the entire point of the project was to use a class and it specifically says I cant use built in methods such as "array.average".



    Sorry that my code was super messy, I was honestly all over the place and very confused. Having that said I finally arrived to this solution. Thanks to everyone who tried to help! really appreciate it, some tips here were very helpful in solving and cleaning up my code.



     class ICESMARK

    public static int ICECount = 0;
    public static double average = 0;

    public ICESMARK(double Mark)


    average += Mark;
    if (ICECount < 8) ICECount++;
    if (ICECount == 8) average /= ICECount;



    class Program

    static void Main(string args)

    ICESMARK ICE = new ICESMARK[8];

    double userInput;

    for (int counter = 0; counter < ICE.Length ; counter++)

    Console.Write("Please enter your mark for ICE0: ", counter + 1 );
    bool ifInt = double.TryParse(Console.ReadLine(), out userInput);
    ICE[counter] = new ICESMARK(userInput);
    Console.WriteLine(ICESMARK.ICECount);


    Console.WriteLine(ICESMARK.average);
    Console.WriteLine(ICESMARK.ICECount);
    Console.ReadLine();







    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53220691%2fpassing-a-value-to-a-class-keeps-returning-0%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote













      Simplest code to get your work done:



      void Main()

      double ICE = new double[8];

      double userInput = 0.0;

      for (int counter = 0; counter < ICE.Length; counter++)

      Console.WriteLine($"Please enter your mark for ICE counter: ");

      bool isNumerical = false;

      while(!isNumerical)
      isNumerical = double.TryParse(Console.ReadLine(), out userInput);

      ICE[counter] = userInput;


      Console.WriteLine("Average : " + ICE.Average());
      Console.ReadLine();




      How it works:



      1. Removed all the frills, you don't need an extra class just for this purpose

      2. Made it mandatory for code to enter valid double value to fill all the slots

      3. Finally used Linq Average to calculate Average value





      share|improve this answer


























        up vote
        2
        down vote













        Simplest code to get your work done:



        void Main()

        double ICE = new double[8];

        double userInput = 0.0;

        for (int counter = 0; counter < ICE.Length; counter++)

        Console.WriteLine($"Please enter your mark for ICE counter: ");

        bool isNumerical = false;

        while(!isNumerical)
        isNumerical = double.TryParse(Console.ReadLine(), out userInput);

        ICE[counter] = userInput;


        Console.WriteLine("Average : " + ICE.Average());
        Console.ReadLine();




        How it works:



        1. Removed all the frills, you don't need an extra class just for this purpose

        2. Made it mandatory for code to enter valid double value to fill all the slots

        3. Finally used Linq Average to calculate Average value





        share|improve this answer
























          up vote
          2
          down vote










          up vote
          2
          down vote









          Simplest code to get your work done:



          void Main()

          double ICE = new double[8];

          double userInput = 0.0;

          for (int counter = 0; counter < ICE.Length; counter++)

          Console.WriteLine($"Please enter your mark for ICE counter: ");

          bool isNumerical = false;

          while(!isNumerical)
          isNumerical = double.TryParse(Console.ReadLine(), out userInput);

          ICE[counter] = userInput;


          Console.WriteLine("Average : " + ICE.Average());
          Console.ReadLine();




          How it works:



          1. Removed all the frills, you don't need an extra class just for this purpose

          2. Made it mandatory for code to enter valid double value to fill all the slots

          3. Finally used Linq Average to calculate Average value





          share|improve this answer














          Simplest code to get your work done:



          void Main()

          double ICE = new double[8];

          double userInput = 0.0;

          for (int counter = 0; counter < ICE.Length; counter++)

          Console.WriteLine($"Please enter your mark for ICE counter: ");

          bool isNumerical = false;

          while(!isNumerical)
          isNumerical = double.TryParse(Console.ReadLine(), out userInput);

          ICE[counter] = userInput;


          Console.WriteLine("Average : " + ICE.Average());
          Console.ReadLine();




          How it works:



          1. Removed all the frills, you don't need an extra class just for this purpose

          2. Made it mandatory for code to enter valid double value to fill all the slots

          3. Finally used Linq Average to calculate Average value






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 9 at 7:57

























          answered Nov 9 at 7:13









          Mrinal Kamboj

          8,21821946




          8,21821946






















              up vote
              0
              down vote



              accepted










              I want to clarify that yes there are easier ways to solve this, but the entire point of the project was to use a class and it specifically says I cant use built in methods such as "array.average".



              Sorry that my code was super messy, I was honestly all over the place and very confused. Having that said I finally arrived to this solution. Thanks to everyone who tried to help! really appreciate it, some tips here were very helpful in solving and cleaning up my code.



               class ICESMARK

              public static int ICECount = 0;
              public static double average = 0;

              public ICESMARK(double Mark)


              average += Mark;
              if (ICECount < 8) ICECount++;
              if (ICECount == 8) average /= ICECount;



              class Program

              static void Main(string args)

              ICESMARK ICE = new ICESMARK[8];

              double userInput;

              for (int counter = 0; counter < ICE.Length ; counter++)

              Console.Write("Please enter your mark for ICE0: ", counter + 1 );
              bool ifInt = double.TryParse(Console.ReadLine(), out userInput);
              ICE[counter] = new ICESMARK(userInput);
              Console.WriteLine(ICESMARK.ICECount);


              Console.WriteLine(ICESMARK.average);
              Console.WriteLine(ICESMARK.ICECount);
              Console.ReadLine();







              share|improve this answer


























                up vote
                0
                down vote



                accepted










                I want to clarify that yes there are easier ways to solve this, but the entire point of the project was to use a class and it specifically says I cant use built in methods such as "array.average".



                Sorry that my code was super messy, I was honestly all over the place and very confused. Having that said I finally arrived to this solution. Thanks to everyone who tried to help! really appreciate it, some tips here were very helpful in solving and cleaning up my code.



                 class ICESMARK

                public static int ICECount = 0;
                public static double average = 0;

                public ICESMARK(double Mark)


                average += Mark;
                if (ICECount < 8) ICECount++;
                if (ICECount == 8) average /= ICECount;



                class Program

                static void Main(string args)

                ICESMARK ICE = new ICESMARK[8];

                double userInput;

                for (int counter = 0; counter < ICE.Length ; counter++)

                Console.Write("Please enter your mark for ICE0: ", counter + 1 );
                bool ifInt = double.TryParse(Console.ReadLine(), out userInput);
                ICE[counter] = new ICESMARK(userInput);
                Console.WriteLine(ICESMARK.ICECount);


                Console.WriteLine(ICESMARK.average);
                Console.WriteLine(ICESMARK.ICECount);
                Console.ReadLine();







                share|improve this answer
























                  up vote
                  0
                  down vote



                  accepted







                  up vote
                  0
                  down vote



                  accepted






                  I want to clarify that yes there are easier ways to solve this, but the entire point of the project was to use a class and it specifically says I cant use built in methods such as "array.average".



                  Sorry that my code was super messy, I was honestly all over the place and very confused. Having that said I finally arrived to this solution. Thanks to everyone who tried to help! really appreciate it, some tips here were very helpful in solving and cleaning up my code.



                   class ICESMARK

                  public static int ICECount = 0;
                  public static double average = 0;

                  public ICESMARK(double Mark)


                  average += Mark;
                  if (ICECount < 8) ICECount++;
                  if (ICECount == 8) average /= ICECount;



                  class Program

                  static void Main(string args)

                  ICESMARK ICE = new ICESMARK[8];

                  double userInput;

                  for (int counter = 0; counter < ICE.Length ; counter++)

                  Console.Write("Please enter your mark for ICE0: ", counter + 1 );
                  bool ifInt = double.TryParse(Console.ReadLine(), out userInput);
                  ICE[counter] = new ICESMARK(userInput);
                  Console.WriteLine(ICESMARK.ICECount);


                  Console.WriteLine(ICESMARK.average);
                  Console.WriteLine(ICESMARK.ICECount);
                  Console.ReadLine();







                  share|improve this answer














                  I want to clarify that yes there are easier ways to solve this, but the entire point of the project was to use a class and it specifically says I cant use built in methods such as "array.average".



                  Sorry that my code was super messy, I was honestly all over the place and very confused. Having that said I finally arrived to this solution. Thanks to everyone who tried to help! really appreciate it, some tips here were very helpful in solving and cleaning up my code.



                   class ICESMARK

                  public static int ICECount = 0;
                  public static double average = 0;

                  public ICESMARK(double Mark)


                  average += Mark;
                  if (ICECount < 8) ICECount++;
                  if (ICECount == 8) average /= ICECount;



                  class Program

                  static void Main(string args)

                  ICESMARK ICE = new ICESMARK[8];

                  double userInput;

                  for (int counter = 0; counter < ICE.Length ; counter++)

                  Console.Write("Please enter your mark for ICE0: ", counter + 1 );
                  bool ifInt = double.TryParse(Console.ReadLine(), out userInput);
                  ICE[counter] = new ICESMARK(userInput);
                  Console.WriteLine(ICESMARK.ICECount);


                  Console.WriteLine(ICESMARK.average);
                  Console.WriteLine(ICESMARK.ICECount);
                  Console.ReadLine();








                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 10 at 3:53

























                  answered Nov 9 at 8:37









                  Brian Naranjo

                  6318




                  6318



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53220691%2fpassing-a-value-to-a-class-keeps-returning-0%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo