Solved

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

Posted on 2006-11-10
3
180 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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!
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now