Problem in working with object array in c#. I have written the code below, please explain me.?
-
here when the compiler reaches the first method (s[i].get) inside for loop main function, it shows runtime error saying that object instance not defined properly, please explain me.. class super { public string name; public int roll; public int[] m = new int[5]; } class sub1 : super { public void get() { Console.WriteLine("\n\n\tName : "); name = Console.ReadLine(); Console.WriteLine("\n\n\tRoll : "); roll = int.Parse(Console.ReadLine()); Console.WriteLine("\n\n\tEnter marks : "); for (int i = 0; i < 3; i++) { m[i]=int.Parse(Console.ReadLine()); } } } class sub2 : sub1 { public void show() { Console.WriteLine("\n\n\tName : "+name); Console.WriteLine("\n\n\tRoll : "+roll); Console.WriteLine("\n\n"); for (int i = 0; i < 3; i++) { Console.WriteLine("\tm[{0}] : {1}\n\t", i, m[i]); } } } namespace MultilevelInheritance { class Program { static void Main(string[] args) { sub2[] s = new sub2[10]; Console.WriteLine("\n\n\tenter the no. of records : "); int n = int.Parse(Console.ReadLine()); Console.WriteLine("\n\n\tReading data : "); for (int i = 0; i < n; i++) { s[i].get(); } Console.WriteLine("\n\n\tDisplaying data : "); for (int i = 0; i < n; i++) { s[i].show(); } Console.Read(); } } }
-
Answer:
When you do sub2[] s = new sub2[10]; you are declaring an array that can hold 10 sub2 objects, but it does NOT create the individual objects. You would first need to loop through the array and create each object.
rahul at Yahoo! Answers Visit the source
Related Q & A:
- What are the different ecological principles? Please explain each briefly?Best solution by Yahoo! Answers
- Do you anticipate any absences from work on a regular basis? If so, please explain the circumstances. ANSWER?Best solution by Yahoo! Answers
- What is Computer Science? Can someone please explain to me?Best solution by seas.gwu.edu
- Can I get written up for a medical issue?Best solution by Yahoo! Answers
- Can somebody please explain to me what spatial perspective is?Best solution by answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.