0

If something is being passed into a method as object[] myparams, each element needs to be accessed as myparams[0], myparams[1], etc.

Is there some way to provide more meaning to each index rather than just a number? Something perhaps similar to named parameters, which I know don't work for arrays.

For example:

employeeid = myparams[1]; employeeFirstName = myparams[4]; employeeAddress = myparams[6];

You would have to know that indexes 1, 4, 6 actually refer to those values.

As an additional note, object[] is bound by an interface so there isn't a way to switch out the type.

4thSpace
  • 43,672
  • 97
  • 296
  • 475
  • You can always do `myVarArgMethod("first-name", "Joe", "last-name", "Shmoe", "dob", new DateTime(2001, 1, 1))` – Sergey Kalinichenko Jun 25 '13 at 19:29
  • Are these downvotes automatic via bots? They happen almost immediately. Doesn't seem human. stackoverflow needs to fix that. – 4thSpace Jun 25 '13 at 20:24
  • I seriously doubt that there are downvoting bots around. It is more likely that some of the viewers of this question didn't understand what you are asking about, and decided to express their disapproval through a vote. I've seen this happen; SO is not going to pay attention as long as it's not a fraud. – Sergey Kalinichenko Jun 25 '13 at 20:58
  • Ok. Guess I don't see the value in the voting process than. The answer provided did two things - let me know it is likely the only way and provided a solution. – 4thSpace Jun 25 '13 at 22:25
  • 1
    It's a good answer, yes. I don't think the voters took their time to decide. I think it's on OK question, so I'll upvote it :) – Sergey Kalinichenko Jun 25 '13 at 23:21
  • @4thSpace Nope. It's automatic people that downvote stuff. – Undo Jun 26 '13 at 04:01
  • At moderators, hope I've clarified the question better to remove the hold. Not sure what that means for all the downvotes. Thanks @dasblinkenlight. – 4thSpace Jun 26 '13 at 20:25

1 Answers1

2

You should use key-value collection (i.e Dictionary, HashSet) rather than pure array of objects to achieve desired effect.

sgnsajgon
  • 664
  • 2
  • 13
  • 56