Click here to Skip to main content
16,004,977 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
So far this is what I have,

public static int PersonData()
{
Person p1 = new Person();
Person p2 = new Person();
}

What I have tried:

This is the problem,

add a static method, PersonData, which does not take any parameters and returns a Person array. Create two person objects, p1 and p2 in your Person array. one is about yourself and other is about any person.

I don't know if PersonData is an int or string. I'm confused. Can you help?
Posted
Updated 4-Apr-16 12:11pm

If you want to return an array of Person, then return type of the method should be an array.

C#
public static Person[] PersonData()
{
   Person p1 = new Person();
   Person p2 = new Person();
   Person[] personArray = new Person[2];
   personArray[0] = p1;
   personArray[1] = p2;
   return personArray;
}


But I would suggest you use List instead of Array. Read about List to learn the its advantages over an array

C#
public static List<person> PersonData()
{
   Person p1 = new Person();
   Person p2 = new Person();
   List<person> personList = new List<person>();
   personList.Add(p1);
   personList.Add(p2);
   return personList;
}
 
Share this answer
 
v2
It is not int and not string. It's clearly written that this is a function. The question does not make any sense at all.

And the implementation of the method makes no sense at all.
You create two instances of Person. Then you loose them, because they are local variables. The frame stack is removed after the call, with all your variables. The actually created objects become inaccessible and will be eventually removed by GC.

This is something you have to understand: Garbage collection (computer science) — Wikipedia, the free encyclopedia[^].

"What to do" would not be a correct question. It depends on what you want to achieve. I would suggest that, in the give context, the whole idea to create a method (static or not) creating two instances of the same class makes no practical sense at all. Let's see. First of all this is how you can return these to objects in the simplest possible way:
C#
public static Person[] PersonData() {
   return new Person[] { new Person(), new Person(), };
}
Another option would be: Tuple(T1, T2) Class (System)[^]
But why? Why?!
Let's consider the calling code using this method. What can it do?
C#
Person[] people = PersonData();
people[0].DoSomething();
people[1].DoSomething();
// you many need to check up if the array has members at 0 and 1.
// by why?!
// in the same content, won't the code below be simpler? Look:
(new Person()).DoSomething(); //?

Here is the deal: it all makes no sense, because the method PersonData does not create any value, does not introduce any knowledge. This is the main thing. Avoid such methods.

(And of course it makes no sense to create a List in such function. A list is good if you have several different calls with the same list, but then the list instance should be transparently passed to method as a parameter. Solution 1 makes not much more sense than your question.)

See also:
Poltergeist (computer programming) — Wikipedia, the free encyclopedia[^],
https://en.wikipedia.org/wiki/Cargo_cult_programming[^],
https://en.wikipedia.org/wiki/Spaghetti_code#Lasagna_code[^].

—SA
 
Share this answer
 
v3
Comments
Yetco 4-Apr-16 17:58pm    
I should've posted the full question. Here it is.

1. Create a class library, YourLastNameLibrary.

2. Add the following classes to your class library.
a) Name, Address, and Person on Pages 231—233.
b) Account, Checking, and Savings

3. Create a console application, YourLastName_Assign6. Add a reference to your class library and add “using yourLibrary;” at the using section.

a) add a static method, PersonData, which does not take any parameters and returns a Person array. Create two person objects, p1 and p2 in your Person array. one is about yourself and other is about any person.

b) add a static method, AccountData, which does not take any parameters and returns an Account array. Create a Checking account and Savings account for each of your person objects returned by PersonData.

c) In Main, call the AccountData method, and output the account type and its holder with the current balance for each account returned by AccountData.
Sergey Alexandrovich Kryukov 4-Apr-16 19:41pm    
Okay, it simply means that the person who gave you this assignment wasn't very thoughtful about its design. :-)
But still, I've shown you how to do it, didn't I?
Even taking into account more complete formulation of the assignment, I fully answered your original question.
If you have other concerns, your follow-up questions will be welcome.
—SA
Yetco 4-Apr-16 20:04pm    
I wrote this, but I keep getting an error for Person(); and Person(); I don't know why.

Person p1 = new Person();
Person p2 = new Person();
Person[] PersonArray = new Person[2];
PersonArray[0] = p1;
PersonArray[1] = p2;

return PersonArray;
}
Sergey Alexandrovich Kryukov 4-Apr-16 20:08pm    
What error, where? Well, you assume that you have a constructor Person(), without parameters. It might not be the case. Some variables could be redefined... Everything looks correct, only all 6 lines should better be one, simpler one, as I've shown in the answer. You really need to provide comprehensive error information and/or comprehensive context...
—SA
Yetco 4-Apr-16 20:57pm    
I changed the class name to Person and the Person() error went away. What I'm confused about now is this question.

add a static method, AccountData, which does not take any parameters and returns an Account array. Create a Checking account and Savings account for each of your person objects returned by PersonData.

Am I supposed to write the first Account array like the person array? When creating the checking and savings account?
I should've posted the full question. Here it is.

1. Create a class library, YourLastNameLibrary.

2. Add the following classes to your class library.
a) Name, Address, and Person on Pages 231—233.
b) Account, Checking, and Savings

3. Create a console application, YourLastName_Assign6. Add a reference to your class library and add “using yourLibrary;” at the using section.

a) add a static method, PersonData, which does not take any parameters and returns a Person array. Create two person objects, p1 and p2 in your Person array. one is about yourself and other is about any person.

b) add a static method, AccountData, which does not take any parameters and returns an Account array. Create a Checking account and Savings account for each of your person objects returned by PersonData.

c) In Main, call the AccountData method, and output the account type and its holder with the current balance for each account returned by AccountData.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Apr-16 20:10pm    
You have to post such content by using "Improve question". Non-answers posted as solutions are considered as abuse.
—SA
Yetco 4-Apr-16 21:34pm    
I didn't know. Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900