6 Replies - 1046 Views - Last Post: 14 March 2011 - 06:54 PM Rate Topic: -----

#1 nanosystems  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-October 10

How to Initialize an Array of Type Object

Posted 13 March 2011 - 01:58 AM

Hello All,

My name is Kingsley. I'm a bit new in C# coding.Could anyone give me some hint on how to initialize an array of say, for e.g., Employee class in a for loop or foreach loop. I have tried as much as possible to achieve my goal, but was having a runtime error of NullPointer Reference.

Let's assume the array will take 5 student objects as shown below in my code - I was even using a Strongly Typed List collection, but all to no avail:


 class University
    {
       private string uname;
       private string uaddress;

        public string Uname
        { get; set; }
        public string Uaddress
        { get; set; }

        public Student[] students = new Student[5];
        
        List<Student> STList = new List<Student>();
     
          

        Student s1 = new Student("Manam", "Tedd", 100);
            STList.Add(s1);
            Student s2 = new Student("Good", "Man", 101);
            STList.Add(s2);
            Student s3 = new Student("Wonder", "Cliff", 102);
            STList.Add(s3);
            Student s4 = new Student("Giffords", "Slanders", 103);
            STList.Add(s4);
            Student s5 = new Student("Xerox", "Sam", 104);
            STList.Add(s5);

           
          
           
         students = STList.ToArray();
}




Thank you.

MOD EDIT: Fixed code tags. The RIGHT way to use code tags:

:code:

This post has been edited by JackOfAllTrades: 13 March 2011 - 05:39 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to Initialize an Array of Type Object

#2 maffelu  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 190
  • Joined: 21-August 08

Re: How to Initialize an Array of Type Object

Posted 13 March 2011 - 03:06 AM

Well, to begin with, you are doing all your logic directly in your class. You should start with reading up on Classes, Properties and Methods and how to use them in conjunction.

To give you a hinter, the following code, based on yours, is valid:


    class University
    {
        public string Uname{ get; set; }
        public string Uaddress{ get; set; }

        public Student[] students = new Student[5];

        public void LoadStudents()
        {
            List<Student> STList = new List<Student>();



            Student s1 = new Student("Manam", "Tedd", 100);
            STList.Add(s1);
            Student s2 = new Student("Good", "Man", 101);
            STList.Add(s2);
            Student s3 = new Student("Wonder", "Cliff", 102);
            STList.Add(s3);
            Student s4 = new Student("Giffords", "Slanders", 103);
            STList.Add(s4);
            Student s5 = new Student("Xerox", "Sam", 104);
            STList.Add(s5);




            students = STList.ToArray();
        }
    }


Was This Post Helpful? 0
  • +
  • -

#3 nanosystems  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-October 10

Re: How to Initialize an Array of Type Object

Posted 14 March 2011 - 11:52 AM

Hi All,

I have posted this question earlier, but solution is yet in sight, though one of the members gave some hints, yet it didn't work for my goal.

My Objective:

To implement a search logic on a Student type array with fname, lname, and id in a University class through a client program.

Could anyone please help out as I'm getting null values error in the list/array. I tried to use a strogly typed list datatype and converted to array, or using only
the list collection, yet it didn't work. Here is the code:

class Student  

02     {  

03         string fname;  

04         string lname;  

05         int id;  

06    

07    

08    

09         public string Lname  

10    

11         { get; set; }  

12    

13         public string Fname  

14         { get; set; }  

15    

16         public int Id  

17         { get; set; }  

18    

19       public Student(string firstname, string lastname, int idnum)  

20         {  

21           fname = firstname;  

22           lname = lastname;  

23           id = idnum;  

24         }  

25    

26           

27     }   

28    

29  class University  

30     {  

31       // private string uname;  

32        //private string uaddress;  

33    

34         public string Uname  

35         { get; set; }  

36         public string Uaddress  

37         { get; set; }  

38    

39        // public Student[] students = new Student[5];  

40           

41       public List<Student> LoadStudents()    

42    

43     {    

44    

45        List<Student> STList = new List<Student>();    

46        

47    

48      STList.Add (new Student("Manam", "Tedd", 100));   

49      STList.Add(new Student("Good", "Man", 101));  

50      STList.Add(new Student("Wonder", "Cliff", 102));    

51      STList.Add(new Student("Giffords", "Slanders", 103));    

52      STList.Add(new Student("Xerox", "Sam", 104));    

53      //students = STList.ToArray();  

54      return STList;   

55             

56           

57   }    

58    

59          

60        

61     }  

62 }  

63 class Program  

64     {  

65         static void Main(string[] args)  

66         {  

67             University u1 = new University();  

68             //List<Student> sts = new List<Student>();  

69             var sts=u1.LoadStudents();  

70    

71                            

72             foreach (Student student in sts) {  

73                

74                 if (student.Lname =="Man")   

75                 {  

76                     Console.WriteLine(student);  

77                        

78                     break;  

79                 }  

80             }  





Thank you.

Kingsley
81 Console.ReadLine();

82 }

83 }

84 }
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,161
  • Joined: 12-June 08

Re: How to Initialize an Array of Type Object

Posted 14 March 2011 - 11:57 AM

Hey chief - let's keep the opening of multiple topics down to a nice round zero. Your original post wasn't even off the first page yet! Thanks bro! I am certain someone will help you when they get a minute!

This post has been edited by modi123_1: 14 March 2011 - 11:58 AM

Was This Post Helpful? 0
  • +
  • -

#5 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 271
  • View blog
  • Posts: 550
  • Joined: 07-July 10

Re: How to Initialize an Array of Type Object

Posted 14 March 2011 - 12:01 PM

What line is causing the error?

Have you tried using a breakpoint? Put a breakpoint at line 67. Step into every bit of code. After every step, check the values of your variables. Do they make sense? I think you'll spot a problem if you do that.
Was This Post Helpful? 0
  • +
  • -

#6 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: How to Initialize an Array of Type Object

Posted 14 March 2011 - 12:39 PM

Hi,

In your foreach loop, you are accessing the .Lname property of the Student object. However, the .LName property is an automatically implemented property of which you NEVER initialise (so guess what it's default value is ;)).

You assign the 'lastname' string passed into the constructor of the Student object to the field named 'lname', NOT to the .Lname property :) (lname = lastname;)

You need to change your .Lname property to this:

public string Lname { get {return this.lname;}  set{this.lname = value;} }  



so that 'lname' is it's backing field.

OR... get rid of the 'lname' field and assign 'lastname' to the automatically implemented .Lname property instead.

(You need to change the other properties in the Student class in the same way)

That shouldn't really cause a problem in terms of that exception since your only using the '==' operator on the strings... But that's the only thing I can see that's anywhere close to causing such an error in the code you've given.

Are you sure you aren't accessing that property elsewhere, as that code shouldn't throw that exception... If not, what line is the exception occuring on? :)

This post has been edited by CodingSup3rnatur@l-360: 14 March 2011 - 02:56 PM

Was This Post Helpful? 0
  • +
  • -

#7 nanosystems  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-October 10

Re: How to Initialize an Array of Type Object

Posted 14 March 2011 - 06:54 PM

View PostCodingSup3rnatur@l-360, on 14 March 2011 - 12:39 PM, said:

Hi,

In your foreach loop, you are accessing the .Lname property of the Student object. However, the .LName property is an automatically implemented property of which you NEVER initialise (so guess what it's default value is ;)).

You assign the 'lastname' string passed into the constructor of the Student object to the field named 'lname', NOT to the .Lname property :) (lname = lastname;)

You need to change your .Lname property to this:

public string Lname { get {return this.lname;}  set{this.lname = value;} }  



so that 'lname' is it's backing field.

OR... get rid of the 'lname' field and assign 'lastname' to the automatically implemented .Lname property instead.

(You need to change the other properties in the Student class in the same way)

That shouldn't really cause a problem in terms of that exception since your only using the '==' operator on the strings... But that's the only thing I can see that's anywhere close to causing such an error in the code you've given.

Are you sure you aren't accessing that property elsewhere, as that code shouldn't throw that exception... If not, what line is the exception occuring on? :)




Thank you D.I.C. for your smart observation. I edited the "get" and "set" prperties of the Student class as advised and everything worked well.

I wish to also thank all others in the Drean-in-Code Community who have contributed to my question in one way other.

Cheers!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1