I have an array which collects a name and a mark,I want to use a method to get the total of the array, I assume this can be done I have googled it but it doesn't really give me an answer.

heres Main

static void Main(string[] args)
        {
            Student[] sdt = new Student[5];
            string carryOn = "n";
            int choice = 0;
            bool inMenu = false;
            int recnum = 0;
            
     
            while (!inMenu)
            {
                Console.WriteLine("Choose which account it you wish to use\n\n");
                Console.WriteLine("1)Teacher");
                Console.WriteLine("2)Student");
                Console.WriteLine("3)Print Teacher details");
                Console.WriteLine("4)Print Student Details");
                Console.WriteLine("5)Quit ");
                Console.Write("Enter your Selection: ");
                try
                {
                    choice = int.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                }
                Console.WriteLine();
                switch (choice)
                {
                    case 1:
                        {
                        }
                        break;
                    case 2:
                        {
                            int i = 0;
                             recnum = 0;
                            bool done = false;
                            while (!done && recnum <= 4)
                            {
                                Console.Write("Do you wish to enter a record?: ");
                                carryOn = Console.ReadLine();
                                if (carryOn == "y")
                                {
                                    sdt[i] = new Student();
                                    Console.Write("Enter a username: ");
                                    sdt[i].Name = Console.ReadLine();
                                    Console.Write("Enter a mark: ");
                                    sdt[i].Mark = int.Parse(Console.ReadLine());
                                    Console.WriteLine();
                                    st.GetMark();
                                    recnum++;
                                    i++;
                                }
                                else
                                {
                                    done = true;
                                }

                            }   
                        }
                        break;
                    case 3:
                        {                           
                        }
                        break;
                    case 4:
                        {
                            for (int i = 0; i < recnum; i++)
                            {
                                if (sdt[i] == null)
                                {
                                    Console.WriteLine("NO DATA");
                                }
                                else
                                {
                                    Console.WriteLine(sdt[i].PrintStudent());
                                    Console.WriteLine(st.GetMark());
                                }
                            }
                        }
                        break;

                    case 5:
                        {
                            inMenu = true;
                        }
                        break;
                    default:
                        {

                        }
                        break;
                }
            }
}

I'm not sure whether the method should be put in Main or in the Student Class
here is Student Class

class Student : Base
    {
        private int mark;

        public Student()
        {
            mark = 0;
        }
        public Student(string name, int mark)
        {
            this.mark = mark;
            base.name = name;
        }


        public int Mark
        {
            get{return mark;}
            set { mark = value; }
        }
        public string PrintStudent()
        {
            string t = "Student Name: " + name + "\nStudent Mark: " + mark +
                "\n";              
            return t;
         }

not really sure where to start, have tried a few things but none that will actually compile, cheers

Use a for loop or a while loop and with it, add up the marks of the elements of the array.

Several problems here.
What do you mean by st.GetMark()? Is it a method of the Student class? I don't see it. You have defined a property which does the same thing. Use it like this:
To set it : Student.Mark=5;
To get it : Console.WriteLine(Student.Mark); //will print 5

Is your Student class derived from a class called Base? If not, remove it.

Here is how I see your Student class. Remove most of the comments if you think it's OK.

class Student
        //you wrote: class Student : Base Base is to my knowldge no class
        //unless you defined your own, but I don't see a reason why
    {
        private int mark;
        private string name; //added this field

        public Student()
        {
            mark = 0;
            name = string.Empty; //added
        }

        public Student(string name, int mark)
        {
            this.mark = mark;
            //removed base.name = name; base is a keyword in C# and refers
            //to the parent class of this class, which in this case is Object
            //as the Object class has no field called "name" you get an error here
            this.name = name; //added
        }


        public int Mark
        {
            get { return mark; }
            set { mark = value; }
        }

        public string Name  //added property for name
        {
            get { return name; }
            set { name = value; }
        }

        public string PrintStudent()
        {
            string t = "Student Name: " + name + "\nStudent Mark: " + mark +
                "\n";
            return t;
        }
    }
st.GetMark()

was the method name i was using to try get the marks, however I don't know whether the method should be in Static Main or Student class and it didn't work no matter where I had it.

There is a base class called Base which will be used for the other switch choices along with another derived class and Base has the name property as it is the only shard by all classes data member, I took that code out when I posted just because it wasn't relevant.

this is where I am heading at the moment

for (int j = 0; j < recnum; i++)
{
         sdt[i] = new Student();
         total += sdt[i].Mark;
}

expect i get a null reference so I put the sdt = new student() there, then when i run it i get index out of range, i didnt put that code in a method but just inside the case statement.

You don't need the getmark method, if you want he mark of a student use [B]sdt[i].Mark [/B]as you already did. What is in your Base class?

for (int j = 0; j < recnum; i++)
{
         sdt[i] = new Student();
         total += sdt[i].Mark;
}

Here total will always be zero!
You call new which executes the contructor which sets the mark-field to zero. Why call new? In case 2 i and recnum seem to do the same thing.

commented: good help dude +1

The Base Class holds the name property as its the only common data member across any classes.

I used i and recnum so that I could actually separate the 2 for my thinking process.

Everything works ok, the arrays hold all the data across all classes,will print the data across all the arrays.

Its just trying to get the total of the Mark Array that im am struggling with, I could do it within the Switch Case and add it as it loops each time, but thought a method would be more challenging and that it is.:-/

Put this in a function:
int total=0;
for (int i = 0; i < sdt.Length; i++)
{
total += sdt.Mark;
}

and let it return the total value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.