PDA

Click to See Complete Forum and Search --> : help with returning an array...


effekt26
Feb 22nd, 2007, 03:37 AM
Ok, i have the following code, but im having trouble returning the array of BlogEntry objects....

BlogEntry[] _blogEntries = new BlogEntry[_totalEntries];

for (int i = 0; i <= _totalEntries; i++) {

BlogEntry _entry = new BlogEntry();
_entry.Id = (int)dsBlogPosts.Tables[0].Rows[i]["entry_id"];
_entry.Title = (string)dsBlogPosts.Tables[0].Rows[i]["entry_title"];
_entry.Post = (string)dsBlogPosts.Tables[0].Rows[i]["entry_post"];
_entry.PostDescription = (string)dsBlogPosts.Tables[0].Rows[i]["entry_post_desc"];
_entry.PostDate = (DateTime)dsBlogPosts.Tables[0].Rows[i]["entry_post_date"];
_entry.Ip = (string)dsBlogPosts.Tables[0].Rows[i]["entry_ip"];
_entry.CategoryName = (string)dsBlogPosts.Tables[0].Rows[i]["category_name"];
_entry.Author = (string)dsBlogPosts.Tables[0].Rows[i]["author_name"];
_entry.AuthorEmail = (string)dsBlogPosts.Tables[0].Rows[i]["author_email"];
_blogEntries[i] = _entry;

}
return _blogEntries;

i get an error on the 'return _blogEntries;' line. cannot convert type BlogEntry[] to BlogEntry is the error message...

if i do a 'return _blogEntries[];' i get a Expecting value...but a 'return _blogEntries[0];' works fine...will this return the first dimension of the array...or the first key...

whats the correct return syntax??

Thanks, Justin

jmcilhinney
Feb 22nd, 2007, 05:49 AM
You've declared your method as returning a BlogEntry rather than a BlogEntry[], i.e. an array of BlogEntry objects. That's what the error message is telling you. The method returns a BlogEntry and you're specifying a BlogEntry[] in the return statement. It cannot convert one to the other so it falls over.

effekt26
Feb 22nd, 2007, 03:39 PM
You've declared your method as returning a BlogEntry rather than a BlogEntry[], i.e. an array of BlogEntry objects. That's what the error message is telling you. The method returns a BlogEntry and you're specifying a BlogEntry[] in the return statement. It cannot convert one to the other so it falls over.

ahh, see now that makes sense. how ever i didnt realise an object, and an array of that object were different objects.

Thanks for the reply...i love these forums, ya learn something new everyday

Justin

jmcilhinney
Feb 22nd, 2007, 05:30 PM
OOP mimics the real world. Are an egg and an egg carton different things? Yes they are: one is a container that can hold multiple instances of the other. So are a BlogEntry and a BlogEntry[].