locked
How to create functions to be reused RRS feed

  • Question

  • I am writing a financial app and completed the Moving Average using C# with the outstanding help from this forum.  I am working on the MACD.  The MACD function is getting too long and I am repeating code.  I want to created a function SMA and call it to the MACD function. I created this class but I get an error:

    namespace myBackEnd
    {
        public class SMA
        {
            public decimal SMA (Queue<Models.DateClose> queue, int period) <--- error here
            {
                decimal average, sum=0;
                for (int i = 0; i < period; i++)
                {
                    Models.DateClose dateClose;
                    dateClose = queue.Dequeue();
                    sum += dateClose.Close;
                }
                return average = sum/period;
            }
        }

    I get an error 'member name cannot be the same as enclosing name".  How can I fix this?

    Monday, November 12, 2018 7:59 PM

Answers

  • You could place this code in it's own namespace e.g.

    namespace myBackEnd.SMA
    {
        public class Operations
        {
            public decimal SMA(Queue<Models.DateClose> queue, int period)
            {
                decimal average, sum = 0;
                for (int i = 0; i<period; i++)
                {
                    Models.DateClose dateClose;
                    dateClose = queue.Dequeue();
                    sum += dateClose.Close;
                }
            return average = sum/period;
        }
    }
    As shown you can keep the method name "as is" or give it a different name.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by DoctorWhoKnew Tuesday, November 13, 2018 11:16 AM
    Monday, November 12, 2018 9:28 PM
  • The method's name cannot be the same name as the class name that encloses the method 

    Class name is SMA and a method name within the class is SMA.


    • Edited by DA924x Monday, November 12, 2018 9:05 PM
    • Proposed as answer by DerChris88 Monday, November 12, 2018 9:24 PM
    • Marked as answer by DoctorWhoKnew Tuesday, November 13, 2018 11:16 AM
    Monday, November 12, 2018 9:04 PM

All replies

  • The method's name cannot be the same name as the class name that encloses the method 

    Class name is SMA and a method name within the class is SMA.


    • Edited by DA924x Monday, November 12, 2018 9:05 PM
    • Proposed as answer by DerChris88 Monday, November 12, 2018 9:24 PM
    • Marked as answer by DoctorWhoKnew Tuesday, November 13, 2018 11:16 AM
    Monday, November 12, 2018 9:04 PM
  • You could place this code in it's own namespace e.g.

    namespace myBackEnd.SMA
    {
        public class Operations
        {
            public decimal SMA(Queue<Models.DateClose> queue, int period)
            {
                decimal average, sum = 0;
                for (int i = 0; i<period; i++)
                {
                    Models.DateClose dateClose;
                    dateClose = queue.Dequeue();
                    sum += dateClose.Close;
                }
            return average = sum/period;
        }
    }
    As shown you can keep the method name "as is" or give it a different name.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by DoctorWhoKnew Tuesday, November 13, 2018 11:16 AM
    Monday, November 12, 2018 9:28 PM