Using an Array of Type object

Need help with code to instantiate elements of an array of type object

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1424 Views - Last Post: 15 March 2010 - 04:19 PM Rate Topic: -----

#1 chookscratchings  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 14-March 10

Using an Array of Type object

Posted 14 March 2010 - 01:42 AM

Hi,

Could someone please offer some useful tips as I'm new to C# and stuck with this.

I have created a base class Person and the derived classes Admin, FT, PT, LocStudent and IntStudent.

Now, I need to create an array of type Person and instantiate all derived classes above as 5 separate elements of an array.

I also need to hard code calls to each constructor.

Person [] = new Person [5];

Person[0] = new FT_Teacher("Jerry", "MacGuire",1);



The derived class FT_Teacher contained the following:
public class FT_Teacher : Teacher
    {

        int teachLoad = 0;
        double salary = 0;
        
        public FT_Teacher(String familyName, String givenName, int employeeID)
            :base(familyName, givenName, employeeID)
        {
        }    

        public override double ComputeAmount(double amount)
        {
            return (salary/52)*(teachLoad / 40);
        }

        // Override ComputeLeave method to apply to FT Teacher.
        public override int ComputeLeave(int leave)
        {
            return (teachLoad / 40) * 28;
        }

        // public override void PrintDetails;
        public override void  PrintDetails()
        {
            throw new NotImplementedException(); //Just a dummy response at this stage.
        }  
    }



It's spitting back the error "Must contain a return type" at me.

What am I doing wrong? help, Im pulling my hair out -- and I am naturally bald!!

This post has been edited by eclipsed4utoo: 14 March 2010 - 07:01 AM
Reason for edit:: please use the code tags....[code] your code here [/code]


Is This A Good Question/Topic? 0
  • +

Replies To: Using an Array of Type object

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Using an Array of Type object

Posted 14 March 2010 - 07:02 AM

where are you receiving the error message?
Was This Post Helpful? 0
  • +
  • -

#3 chookscratchings  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 14-March 10

Re: Using an Array of Type object

Posted 14 March 2010 - 10:24 AM

hey yeah,

red squiggle lines appear under Person[0] = new FT_Teacher("Jerry","Macguire",1);

- that's where it's returning the floating compile warning (sorry, as opposed to error) - method must return a type. Isn't Person my declared type?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,606
  • Joined: 23-August 08

Re: Using an Array of Type object

Posted 14 March 2010 - 10:36 AM

Person [] persons = new Person [5];
persons [0] = new FT_Teacher("Jerry", "MacGuire",1);


Was This Post Helpful? 0
  • +
  • -

#5 chookscratchings  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 14-March 10

Re: Using an Array of Type object

Posted 14 March 2010 - 11:40 AM

Hi hahahah :rolleyes2: yeah forgot to show that in my code I had done as you had shown above -- ie. I have also declared the variable name for the array..

It just doesn't seem to behave itself without reminding me "must return a type". Am stumped..

This post has been edited by chookscratchings: 14 March 2010 - 11:40 AM

Was This Post Helpful? 0
  • +
  • -

#6 Bent al-Yemen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 30-December 09

Re: Using an Array of Type object

Posted 14 March 2010 - 06:01 PM

i don't know if i can put my post here,if you please Mrchookscratchings, but it's very related to this topic.
Here, in this code:
Person [] persons = new Person [5];
persons [0] = new FT_Teacher("Jerry", "MacGuire",1);



if i want to search for the element" Jerry", how can i do that in this array.
Another thing, if i don't know number of persons in this example, how can i change it to ArrayList.

i hope to get satisfying answer :clover: ,Thaaaaaaanx. :) :clover: :clover:

This post has been edited by Bent al-Yemen: 15 March 2010 - 01:24 AM

Was This Post Helpful? 0
  • +
  • -

#7 Bent al-Yemen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 30-December 09

Re: Using an Array of Type object

Posted 15 March 2010 - 01:23 AM

:sleepy: :dozingoff:

:clover: :clover: :clover:
Was This Post Helpful? 0
  • +
  • -

#8 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: Using an Array of Type object

Posted 15 March 2010 - 01:39 AM

First off bumping to receive an answer is normally frowned upon, but this time I will give the answer that I can. To make the code you gave into an ArrayList use the following:
Collection<Person> persons = new Collection<Person>;
persons.Add(new FT_Teacher("Jerry", "MacGuire",1));


Was This Post Helpful? 0
  • +
  • -

#9 Bent al-Yemen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 30-December 09

Re: Using an Array of Type object

Posted 15 March 2010 - 02:01 AM

Quote

First off bumping to receive an answer is normally frowned upon


Thanx a lot for your answer, but i didn't get this point. can you tell me what do you mean.For me i didn't mean anything at all, i just wanted to rise the topic.
Thanx :)
Was This Post Helpful? 0
  • +
  • -

#10 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: Using an Array of Type object

Posted 15 March 2010 - 02:25 AM

Raising the topic is called "bumping" It is normally not a pleasant tactic to use. If you have more information or wish to clarify something in your post by all means go for it. But posting again simply to get your post back in the spotlight is not really that cool.
Was This Post Helpful? 0
  • +
  • -

#11 Bent al-Yemen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 30-December 09

Re: Using an Array of Type object

Posted 15 March 2010 - 03:10 AM

View PostAdkins, on 15 March 2010 - 11:25 AM, said:

Raising the topic is called "bumping" It is normally not a pleasant tactic to use. If you have more information or wish to clarify something in your post by all means go for it. But posting again simply to get your post back in the spotlight is not really that cool.


Ok, sorry :blush:

i was worried that you'll not notice that there's a new post in the topic, because it's not my topic as you can see.

I have a question here:
Collection<Person> persons = new Collection<Person>


Is it " List<>" , i have many c# book, but this the firs time is see "collection">
Was This Post Helpful? 0
  • +
  • -

#12 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: Using an Array of Type object

Posted 15 March 2010 - 03:18 AM

A List is a type of Collection. They work basically the same, and to the best of my knowledge there is little to no difference. If you are more comfortable with a List because you have seen it in your books and there for have a point of reference for it, then simply replace the word Collection with the word List in my previous code.
Was This Post Helpful? 0
  • +
  • -

#13 Bent al-Yemen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 30-December 09

Re: Using an Array of Type object

Posted 15 March 2010 - 03:40 AM

ok, thank you so much Mr Adkins for your reply, i still have the question in my first reply " How can i search for an element inside an array or in a List if it is in this form:
Collection<Person> persons = new Collection<Person>;
persons.Add(new FT_Teacher("Jerry", "MacGuire",1));



i have an object that contains more than element, if i want to search for the "Jerry " for example, how??

I'm not repeating my post, I'm clarifying it. :angel:

This post has been edited by Bent al-Yemen: 15 March 2010 - 03:40 AM

Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,805
  • Joined: 16-October 07

Re: Using an Array of Type object

Posted 15 March 2010 - 05:38 AM

Here's some example code for a search. I had to extrapolate your classes. You might want to look at that, too.

public class Person {
	protected string familyName, givenName;

	public Person(string familyName, string givenName) {
		this.familyName = familyName;
		this.givenName = givenName;
	}

	public string FamilyName { get { return familyName; } }

	public string GivenName { get { return givenName; } }

	public override string ToString() {
		return "Person: " + this.givenName + " " + this.familyName;
	}
}


public abstract class Employee : Person {
	protected int employeeID;

	protected Employee(string familyName, string givenName, int employeeID)
		: base(familyName, givenName) {
		this.employeeID = employeeID;
	}

	public int EmployeeID { get { return employeeID; } }

	public abstract void PrintDetails();

	public abstract double Salary { get; }
}


public abstract class Teacher : Employee {
	// I didn't understand why you'd pass values to these, 
	// so I took the parameters out
	protected Teacher(String familyName, String givenName, int employeeID) : base(familyName, givenName, employeeID) { }

	public abstract double ComputeAmount();
	public abstract int ComputeLeave();

}

public class FT_Teacher : Teacher {
	// having these 0 makes little sense
	int teachLoad = 0;
	// double salary = 0;

	public FT_Teacher(String familyName, String givenName, int employeeID) : base(familyName, givenName, employeeID) { }

	public override double ComputeAmount() { return (Salary / 52) * (teachLoad / 40); }

	public override int ComputeLeave() { return (teachLoad / 40) * 28; }

	// again, going by your model.  poor guy
	public override double Salary { get { return 0; } }

	public override void PrintDetails() { throw new NotImplementedException(); }
}

//....

List<Person> list = new List<Person>();
list.Add(new FT_Teacher("MacGuire", "Jerry", 1));
list.Add(new Person("Lin", "Tam"));
list.Add(new Person("O'Bedlam", "Tom"));

// find with loop
foreach (Person person in list) {
	if (person.GivenName.Equals("Tom")) {
		Console.WriteLine(person);
		break;
	}
}

// find with built in search
Console.WriteLine(
	list.Find(delegate(Person person) { return person.GivenName.Equals("Jerry"); })
);



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#15 Bent al-Yemen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 30-December 09

Re: Using an Array of Type object

Posted 15 March 2010 - 03:01 PM

Thank you soooo much Mr baavgai , i appreciate your help. I'll see if it'll help finish my code.

:bigsmile:
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2