473,408 Members | 2,832 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,408 software developers and data experts.

Returning Arrays from Methods

I'm not quite sure of how to do this and I haven't been able to find
much about it. I'm hoping someone here can help me out.

In a nutshell,: I would like to use a foreach loop to iterate through
an Array which is returned by a method,

foreach ( string groupName in MySplitterFunction(stringToSplit) )

Now, how do I write the method so it returns an Array? Can any one
provide a simple example?

Thanks in advance,
--
Regards,
John Wood a.k.a Mortimer Schnurd
jo********@dropthistaggmail.com

(to reply: remove "dropthistag")
Nov 16 '05 #1
6 100484
John,

Here is an example of a function:

public string[] getStrings(){
string[] strings = new string[]{"hey", "world!"};

return strings;
}

You just have to make sure that you are declaring the return type as an
array.

Thanks,
Ian Suttle
http://www.IanSuttle.com

Nov 16 '05 #2
Mortimer Schnurd <fu**********@hotsmail.com> wrote:
I'm not quite sure of how to do this and I haven't been able to find
much about it. I'm hoping someone here can help me out.

In a nutshell,: I would like to use a foreach loop to iterate through
an Array which is returned by a method,

foreach ( string groupName in MySplitterFunction(stringToSplit) )

Now, how do I write the method so it returns an Array? Can any one
provide a simple example?


Sure:

using System;

class Test
{
static void Main()
{
foreach (string name in GetNames())
{
Console.WriteLine (name);
}
}

static string[] GetNames()
{
string[] ret = {"Matthew", "Mark", "Luke", "John"};
return ret;
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
private string[] isGuilty()
{
string[] result = new string[]{"Of", "course", "Peterson", "is",
"guilty"};
return result;
}

ShaneB
"Mortimer Schnurd" <fu**********@hotsmail.com> wrote in message
news:5n********************************@4ax.com...
I'm not quite sure of how to do this and I haven't been able to find
much about it. I'm hoping someone here can help me out.

In a nutshell,: I would like to use a foreach loop to iterate through
an Array which is returned by a method,

foreach ( string groupName in MySplitterFunction(stringToSplit) )

Now, how do I write the method so it returns an Array? Can any one
provide a simple example?

Thanks in advance,
--
Regards,
John Wood a.k.a Mortimer Schnurd
jo********@dropthistaggmail.com

(to reply: remove "dropthistag")

Nov 16 '05 #4
Thanks for the all the replies. They definitely hit the mark and
solved my problem. I have to make special mention of ShaneB's solution
though. Right on, ShaneB!
--
Regards,
John Wood a.k.a Mortimer Schnurd
jo********@dropthistaggmail.com

(to reply: remove "dropthistag")
Nov 16 '05 #5

"ShaneB" <st********@yahoo.com> wrote in message
news:ui**************@TK2MSFTNGP15.phx.gbl...
private string[] isGuilty()
{
string[] result = new string[]{"Of", "course", "Peterson", "is",
"guilty"};
return result;
}


Please keep your political opinions to yourself.
Nov 16 '05 #6
What does that have to do with politics? Anyway, it was merely a joke...I
just happened to be watching CourtTV when I replied.

Lighten up.

ShaneB

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:%2****************@TK2MSFTNGP14.phx.gbl...

"ShaneB" <st********@yahoo.com> wrote in message
news:ui**************@TK2MSFTNGP15.phx.gbl...
private string[] isGuilty()
{
string[] result = new string[]{"Of", "course", "Peterson", "is",
"guilty"};
return result;
}


Please keep your political opinions to yourself.

Nov 16 '05 #7

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

Similar topics

2
by: jeff | last post by:
I have a php file that contains a couple of arrays used for state/country pull-down lists. I have two global arrays and an accessor method for each. I have some simple logging methods, so I know a...
5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
3
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to...
60
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: >...
5
by: shyam | last post by:
Hi All I have to write a function which basically takes in a string and returns an unknown number( at compile time) of strings i hav the following syntax in mind char *tokenize(char *) ...
4
by: John | last post by:
Hi I need to return an array of string in my own split function (access 97). I have defined the function as below but I get err on 'As String()'. What can I do to make the function return an...
26
by: cdg | last post by:
Could anyone correct any mistakes in this example program. I am just trying to return an array back to "main" to be printed out. And I am not sure how a "pointer to an array" is returned to the...
2
by: dspc | last post by:
On Apr 28 2001, 6:06 am, "Michael Harris" <mik...@mvps.org> wrote: > "...So there is no way to transparentlyreturnanarrayinJScript?..." > To be "JScript-friendly", you have toreturn*everything"...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.