473,396 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Save inside array and pointer

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;

}

}

}



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



Nov 15 '05 #1
6 1253
How can I save this person inside an array?


_Persons = new Person[] {_NewPerson, _NewPerson2};

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #2
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'

?
Nov 15 '05 #3
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!
Nov 15 '05 #4
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #5
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
"Arjen" <bo*****@hotmail.com> wrote in message
news:bt**********@news2.tilbu1.nb.home.nl...
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'

?

Nov 15 '05 #6
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" <ma********************@mvps.org> schreef in bericht
news:OV**************@TK2MSFTNGP12.phx.gbl...
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Dmitrii PapaGeorgio | last post by:
let's say i have a 2D array, how would i access it with a pointer? int x ; func(x); void func(int *x) { //i want to fill it with a number, like 1 }
7
by: Fabian Wauthier | last post by:
Hi list, I am trying to dynamically grow a 2 dimensional array (Atom ***Screen) of pointers to a struct Atom (i.e. the head of a linked list). I am not sure if this is the right way to do it: ...
3
by: Kenny | last post by:
Will is affect the result if I pass Array Pointer as a parameter to a function compare to Array as a parameter? For example, BYTE buffer a.) function(&buffer) b.) function(buffer) Is it any...
5
by: s88 | last post by:
Howdy: the follows is my program, I wanna change my structure array pointer in the function "testfunc", but I fail..., I also try to call the testfunc by reference, but the compiler says...
7
by: André Hänsel | last post by:
Hi! This: function foo() { $a = array('a' => 'b', 'c' => 'd'); return array_keys($a); } while ($a = each(foo()))
2
by: Chris | last post by:
How do I save an array of bytes to a file? I am binding a update parameter to a fileupload control. It passes the file to my object as a Filebytes datatype. I am assuming it is just an array of...
12
by: howa | last post by:
// example #include <iostream> using namespace std; size_t bsearch(int *a) { cout<<a<<endl; cout<<sizeof(a) / sizeof(a)<<endl;
3
by: tivicoo | last post by:
I have a 16 bit array called ADData, and i want to save this array to a text file using C#. i tried to use different codes that i located online but when i open the saved data on notepad the data...
26
by: =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Do you think we can reach any kind of consensus on whether the following code's behaviour is undefined by the Standard? int my_array; int const *const pend = *(&my_array + 1); Considering...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.