Shane
Shane

Reputation: 31

How to set array values to a constructor c#

I got the program running guys thanks alot i just deleted everything and started again from scratch! Now I am having problems with :

// The constructor

public Grade ( string cname , int Studentident , int [] homework , int [] classwork ,      int [] midexam , int [] finalexam)
{
    coursename = cname ;
    Studentid = Studentident ;  
    hwgrade = homework ;
    cwgrade = classwork ;
    midegrade = midexam ;
    finalegrade = finalexam ;
} 

// trying to set the values

Grade grade = new Grade('m',1,1,1,1,1);

it is giving me an error cannot convert from int to int[]

Upvotes: 1

Views: 8080

Answers (2)

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

You can replace with this code - delete [] if you wish use int, and not array of int

Grade ( string cname , int Studentident , int homework , int classwork ,      int midexam , int  finalexam)
{
  ...
}

Or create array with new int[] {1} and pass in parameters

Upvotes: 0

Oded
Oded

Reputation: 499402

You must pass in arrays, not single values:

Grade grade = new Grade('m',
                        new int[] {1}, 
                        new int[] {1}, 
                        new int[] {1}, 
                        new int[] {1}, 
                        new int[] {1});

int is not the same as int[], after all.

The above assumes that your fields (such as hwgrade) are declared as int[] and not int.

If this is not the case, and the fields are actually integers and not integer arrays, you need to change the method signature to take integers instead of arrays:

public Grade(string cname, 
             int Studentident, 
             int homework, 
             int classwork,
             int midexam,
             int finalexam)

Upvotes: 7

Related Questions