views:

236

answers:

4

I am now designing an SNMP library. The problem is caused by a special function like this,

*** GetTable(string id)

This function may return Variable[,] which is a two dimensional array sometimes, but also Variable[,,] and arrays with more dimensions. So I believe it is not reasonable to return fixed array such as Variable[,], Variable[,,] and so on.

But what should this method return then? How to design it? What about a custom VariableCollection?

Any suggestion is welcome.

+1  A: 

Well, you could return Array and let the caller check the .Rank?

Of course, returning an array may not be the best option in the first place (depending on the scenario): Arrays considered somewhat harmful

Marc Gravell
+3  A: 

As a general rule, returning a custom type allows for greater flexibility in the representation of your data. I'd also favour providing your own methods for accessing the data in this collection, rather than exposing multidimensional arrays.

I generally avoid using multidimensional rectangular arrays myself. In many cases, multidimensional jagged arrays are more convenient as you can lift a slice out in isolation.

Drew Noakes
+1  A: 

Returning a multidimensional array is a risky choiche. I would suggest to implement a custom collection along with methods to get informations about it's internal state and methods to get the data from it. It's more flexible and less susceptible to errors when the client calls your method.

Stefano Driussi
A: 

If creating a public Api it is recommended to return a strongly typed collection < T >. So I'd create a custom object which can take the required data which you want to return and then use the .net collection < T > to return it.

Hope this helps.

WestDiscGolf