Results 1 to 4 of 4

Thread: help with returning an array...

  1. #1

    Thread Starter
    Addicted Member effekt26's Avatar
    Join Date
    Nov 2006
    Posts
    138

    help with returning an array...

    Ok, i have the following code, but im having trouble returning the array of BlogEntry objects....

    Code:
                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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: help with returning an array...

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member effekt26's Avatar
    Join Date
    Nov 2006
    Posts
    138

    Re: help with returning an array...

    Quote Originally Posted by jmcilhinney
    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
    Last edited by effekt26; Feb 22nd, 2007 at 05:00 PM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: help with returning an array...

    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[].
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width