473,386 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Return an Array

Hello, Newsgroupians:

I have a simple question. I have a function that I want to return an array.
I want the array to be other objects that were instantiated in the function
itself. What's the correct way to return the array? Here's what I would
like to happen...

public object[] MyFunc()
{
object obj1 = SomeOtherFunc1();
object obj2 = SomeOtherFunc2();
object obj3 = SomeOtherFunc3();

return object[3] {obj1, obj2, obj3}; // Of course this is an error
}

But, of course, this doesn't work. I see that creating a new instance of
the array and then assigning the array to object may be inefficient...

object obj1 = SomeOtherFunc1();
object obj2 = SomeOtherFunc2();
object obj3 = SomeOtherFunc3();

object retVal[] = {obj1, obj2, obj3};
return retVal;

Although it may seem inefficient, is this the proper way to return an array
of objects? Is there a better way? Thank you all.
Trecius
Nov 19 '07 #1
3 1679
Trecius,

You are forgetting to use "new", like so:

public object[] MyFunc()
{
object obj1 = SomeOtherFunc1();
object obj2 = SomeOtherFunc2();
object obj3 = SomeOtherFunc3();

// Emphasise on NEW mine.
return NEW object[3] {obj1, obj2, obj3};
}
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Trecius" <Tr*****@discussions.microsoft.comwrote in message
news:07**********************************@microsof t.com...
Hello, Newsgroupians:

I have a simple question. I have a function that I want to return an
array.
I want the array to be other objects that were instantiated in the
function
itself. What's the correct way to return the array? Here's what I would
like to happen...

public object[] MyFunc()
{
object obj1 = SomeOtherFunc1();
object obj2 = SomeOtherFunc2();
object obj3 = SomeOtherFunc3();

return object[3] {obj1, obj2, obj3}; // Of course this is an error
}

But, of course, this doesn't work. I see that creating a new instance of
the array and then assigning the array to object may be inefficient...

object obj1 = SomeOtherFunc1();
object obj2 = SomeOtherFunc2();
object obj3 = SomeOtherFunc3();

object retVal[] = {obj1, obj2, obj3};
return retVal;

Although it may seem inefficient, is this the proper way to return an
array
of objects? Is there a better way? Thank you all.
Trecius

Nov 19 '07 #2
Thank you, Mr. Paldino. It worked perfectly!

Trecius

"Nicholas Paldino [.NET/C# MVP]" wrote:
Trecius,

You are forgetting to use "new", like so:

public object[] MyFunc()
{
object obj1 = SomeOtherFunc1();
object obj2 = SomeOtherFunc2();
object obj3 = SomeOtherFunc3();

// Emphasise on NEW mine.
return NEW object[3] {obj1, obj2, obj3};
}
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Trecius" <Tr*****@discussions.microsoft.comwrote in message
news:07**********************************@microsof t.com...
Hello, Newsgroupians:

I have a simple question. I have a function that I want to return an
array.
I want the array to be other objects that were instantiated in the
function
itself. What's the correct way to return the array? Here's what I would
like to happen...

public object[] MyFunc()
{
object obj1 = SomeOtherFunc1();
object obj2 = SomeOtherFunc2();
object obj3 = SomeOtherFunc3();

return object[3] {obj1, obj2, obj3}; // Of course this is an error
}

But, of course, this doesn't work. I see that creating a new instance of
the array and then assigning the array to object may be inefficient...

object obj1 = SomeOtherFunc1();
object obj2 = SomeOtherFunc2();
object obj3 = SomeOtherFunc3();

object retVal[] = {obj1, obj2, obj3};
return retVal;

Although it may seem inefficient, is this the proper way to return an
array
of objects? Is there a better way? Thank you all.
Trecius


Nov 19 '07 #3
Use:

return new object[]{obj0, obj1, obj2};

or alternatively use "out" parameters if you always have a fixed number of
'output' values - this technique does not create the additional array and
might be faster in inner loop situations.

Hilton
Nov 19 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andrew Poulos | last post by:
If I'm searching for an occurance of a value in a multi-dimensional array how can I get it's index returned as an array, if found? For example, if: foo = new Array(); foo = , 5, , 9, 10]; ...
0
by: Marc van Boven | last post by:
I'm stuck with the following problem: My nusoap-client calls a server-function giveCombination(). The function giveCombination should return something like array( => array( 'a_id' => 6,...
4
by: Isaac | last post by:
Hi mates I want to know a simple program of return array from function ? Do I need to use pointer to return the address of the first element in an array. Isaac
6
by: Neo | last post by:
Dear All, I want to know how a subroutine should return an array of values to the main program. From the main program, I call a sub-routine 'get_sql' which then fetches data from oracle db using...
8
by: M. Moennigmann | last post by:
Dear all: I would like to write a function that opens a file, reads and stores data into an 2d array, and passes that array back to the caller (=main). The size of the array is not known before...
23
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
5
by: Sam | last post by:
Hi All I have couple of question regarding property of a class and structures. **** ---- Here is my class and structure ---- ***** 1. Public Structure MyPoint 2. Dim p As Point 3. ...
17
by: benben | last post by:
Given a class template Vector<>, I would like to overload operator +. But I have a hard time deciding whether the return type should be Vector<U> or Vector<V>, as in: template <typename U,...
5
by: shashi59 | last post by:
Now i need information about interface c and python.Now i call c function from python but i dont know how to return a array from c to python
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.