Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

how do i create an array of objects by a function return?

Posted on 2006-11-10
3
182 Views
Last Modified: 2010-04-16
here goes experts:

i have a class lets call it myClass.

i want to create an array of myClass objects by a function return like this:

 myClass[]  l_oClass = myFunction();

myFunction looks like this:
 public myClass[] myFunction()
{
    myClass[] l_oClassArray = new myClass[10];

   // alternate code here

    return l_oClassArray;
}

where it says //alternate code here
i have tried to say
  l_oClassArray.Initialize();

or alternatively:
 for( int i=0; i++; i<10)
{ l_oClassArray[i] = new myClass(); }

neither of which stopped me from seeing "object not set to a reference" error when i tried to use the objects

any suggestions?
i have also tried passing the myClass array by ref into the function and then I get the same "not a reference" error when it comes back  even though its been initialized and used inside myFunction before control is returned.

-thanks
0
Comment
Question by:richardsimnett
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
ozymandias earned 500 total points
ID: 17916939
for( int i=0; i++; i<10)
{ l_oClassArray[i] = new myClass(); }


the above should work (except that you have a couple of the arguments the wrong way round)

for( int i=0; i<10; i++){
    l_oClassArray[i] = new myClass();
}

0
 

Author Comment

by:richardsimnett
ID: 17917078
>except that you have a couple of the arguments the wrong way round
its always fun without intellisense ;)

ok. so what you are saying is basically that what i am doing should work and that if i am seeing an error it must be generated elsewhere in the code.  (obviously there is alot more going on with my classes than what i posted)

let me wait and see if I get anymore interesting responses before awarding you the points.

thanks alot
0
 

Author Comment

by:richardsimnett
ID: 17917307
werd.
you get the points.

it was right all along.  there was a problem with the X|\/|L parsing that was populating the class.

i didnt believe it was right until you agreed, so your help was instrumental.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question