0

In my code, I have a function, which returns a single row of data from a database. It returns the data using a Structure so I can return more than 1 data.

For example:

Public Structure structure_name
    Public column1 as integer
    Public column2 as string
    Public column3 as string
End Structure

Function function_name() As structure_name
    Dim single_row_structure as structure_name

    'get single row from database
    'assign single row values to attributes of Structure

    single_row_strucure.column1 = dbcol1
    single_row_strucure.column2 = dbcol2
    single_row_strucure.column3 = dbcol3

    return single_row_strucure
End Function

This works fine for a single row. My question is, how do I get a function to return more than 1 row of data?

oshirowanen
  • 15,297
  • 82
  • 198
  • 350

3 Answers3

1
Function function_name() As structure_name()

    Dim rows(2) as structure_name
    rows(0).column1 = dbcol1
    rows(0).column2 = dbcol2
    rows(0).column3 = dbcol3
    rows(1).column1 = dbcol1
    rows(1).column2 = dbcol2
    rows(1).column3 = dbcol3

    return rows
End Function

http://www.vb-helper.com/howto_net_declare_arrays.html

jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • I understand how to change this so the number of rows is based on the number of rows returned from the database, but how do I use the returned structure array outside the function_name()? if the number of rows was determined by the amount of rows returned from the database? – oshirowanen Mar 15 '13 at 12:24
0

Yes, you can return a collection of some sort. You might be a little limited in the syntax options for this as your tags suggest you are using the first version of .NET. (1.1 -- Is that true?)

Using the latest version of .NET and C#, I'd return List<structure_name>. But there are a lot of other options.

As far as getting the data from the database, databases are very good at returning multiple rows. But you need to provide more details about which database and how you are obtaining the data in order to make suggestions about how you'd transfer it to a C# collection.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
0

Sorry been a while since VB so the C#

Why not use a collection like a List

List<structure_name> structure_names = new List<structure_name>();

structure_names.Add(single_row_strucure);  // repeat
paparazzo
  • 44,497
  • 23
  • 105
  • 176