Save inside array and pointer

  • Thread starter Thread starter Arjen
  • Start date Start date
A

Arjen

Hello,



I have made a class "main" and "person".

I have created new persons inside main.



How can I save this person inside an array?

And how does pointers work?



Thanks!













// -----------------------------------------------------



public class Main {



public Main() {



// Create a new person

Person _NewPerson = new Person();

_NewPerson.Name = "James";



// Create a new person

Person _NewPerson2 = new Person();

_NewPerson2.Name = "John";



// Code

}

}

// -----------------------------------------------------







// -----------------------------------------------------



// Array of persons

// public Person[] _Persons;



public class Person: Main {



// The name of the person

private string _Name;



public string Name {

get {

return _Name;

}

set {

_Name = value;

}

}

}



// -----------------------------------------------------
 
Hello,

I have changed it to:
Person _Persons = new Person[] {_NewPerson, _NewPerson2};

The message I now get is:
Cannot implicitly convert type 'Person[]' to 'Person'

?
 
I have changed it to:
Person _Persons = new Person[] {_NewPerson, _NewPerson2};

Why? You said originally that _Persons was declared as

public Person[] _Persons

Keep it that way.



Mattias
 
I have also post an other question inside this newsgroup.
Subject: "Big question: XML serialization and OO"

Maybe you can help with that one?

Thanks!
 
try
Person[] _Persons = new Person[2];//This creates an array with a size of two
that can hold a type of "Person"
_Persons[0] = new Person();//this create a new person at position 0 or the
array
_Person2[1] = new Person();//this create a new person at position 1 or the
array

the message you got states that you cannot convert a Person to an array of
Persons. This syntax should get you throught.

Hope this helps

Marco
 
Because I don't knows how it works...


// Array of persons

// public Person[] _Persons;



This was placed in the comment.



How can I add a person after I already have add a person?

Means this that I have to extend the _Persons???









Mattias Sjögren said:
I have changed it to:
Person _Persons = new Person[] {_NewPerson, _NewPerson2};

Why? You said originally that _Persons was declared as

public Person[] _Persons

Keep it that way.



Mattias
 
Back
Top