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 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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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];
...
|
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,...
|
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
|
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...
|
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...
|
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;
|
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. ...
|
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,...
|
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
|
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
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |