Dave Burke - Freelance .NET Developer specializing in Online Communities

A freelance .NET Developer

Arrays. Use'm or lose'm

Arrays used to be hard.  Oh hell.  They're still hard.  Array[] verses ArrayList?  Use one when?  What???  The double brackets go with the object type, not the variable? Curlies around the declaration, not parentheses?

But still, they pass multiple values real good!

We'll pass three strings from a method: a list of user IDs, the emails of those users, and the users' names.  I was going to create a quick data structure, which would have been preferrable for strongly typed use, but I wanted to use the array.  Pretty ecomonical, actually.


public string[] GetTeamStuff()
{
 team_ids = "1,2,3";
 string[] Team =
  {
   team_ids,
   Users.GetEmails(team_ids).
   Users.GetNames(team_ids),
  };
 return Team;
}

and used as

txtTeamEmails.Text = GetTeamStuff()[1];

Posted: Apr 28 2004, 04:27 PM by daveburke | with 3 comment(s)
Filed under:

Comments

Anonymous Me said:

Only you wouldn't REALLY want to use the code you posted there, right? Rather than re-assemble the array for every item you want to access, you'd grab the array once, assign the result to a local variable and then access the items from the local variable:

string[] stuff = GetTeamStuff();
txtTeamEmails.Text = stuff[ 1 ];
txtSomethingElse.Text = stuff[ 2 ];
// etc.
# April 28, 2004 6:43 PM

Dave Burke said:

Anonymous, You be right. Separate calls and re-assembling the array would be dumb. I'd better change my code. :-) Thanks!
# April 28, 2004 7:18 PM

secretGeek said:

(Note that indenting is lost in comments!)

'Assume Users has been instantiated
Public Function GetTeamStuff() As String()()
Dim team_ids() As String = {1, 2, 3}
Dim Team()() As String = _
{team_ids, _
users.GetEmails(team_ids), _
users.GetNames(team_ids)}
Return Team
End Function
# April 28, 2004 7:54 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)