Solved

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

Posted on 2006-11-10
3
179 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
Comment Utility
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
Comment Utility
>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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

8 Experts available now in Live!

Get 1:1 Help Now