Results 1 to 5 of 5

Thread: Return items in a list before returning the list

  1. #1

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Return items in a list before returning the list

    That was probably bad title, but I'll illustrate what I'm trying to do. I have a method in a class that I need to call several times. The method returns a List(Of String). I'm just grabbing some information from a database.

    VB.NET Code:
    1. Public Function GetTestList() As List(Of String)
    2.         Dim int As Integer = 0
    3.  
    4.         Dim cso_list As New List(Of String)
    5.  
    6.         Dim connection As New Connection()
    7.         Dim record_set As New Recordset
    8.         connection.Open("...")
    9.         record_set.Open("...", connection)
    10.         With record_set
    11.             Do While Not .EOF And Not int = 100
    12.                 int = int + 1
    13.                 cso_list.Add(CStr(record_set("...").Value))
    14.                 .MoveNext()
    15.             Loop
    16.         End With
    17.         connection.Close()
    18.  
    19.         Return cso_list
    20.     End Function

    You can see in that method that I'm getting the values from the database, adding it to the new list and returning that list.

    I then assign that list to a variable in my main class and loop through them.

    VB.NET Code:
    1. Dim records As List(Of String) = Me.test.GetTestList
    2.        
    3.         For Each str In records
    4.            'Do stuff
    5.         Next

    When looping through the items, I'm showing them in the UI. But that's the quick and easy part. The long part is actually gathering the list. So I want to display the value without returning the list.

    Is there a way to do this without updating the UI from the method?
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Return items in a list before returning the list

    You could make GetTestList an asynchronous method, which will basically return immediately and set off the data gathering task in a secondary thread. The method that actually does the work could raise events periodically, e.g. once for every item or perhaps every batch of ten items or something like that. The event could be raised either on the secondary thread or the UI thread but any UI updates have to be done on the UI thread regardless.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Return items in a list before returning the list

    You could even simplify the whole thing and just use a BackgroundWorker, passing the items back to the UI thread via the ReportProgress method and ProgressChanged event. That's basically exactly what I was talking about but all the multi-threading and marshalling is already in place for you.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Return items in a list before returning the list

    This may sound crazy, but it's pure curiosity: Returning the Recordset from the method to the UI may be an option or this would be a bad practice in .NET?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Return items in a list before returning the list

    Quote Originally Posted by jcis View Post
    This may sound crazy, but it's pure curiosity: Returning the Recordset from the method to the UI may be an option or this would be a bad practice in .NET?
    The first thing I would say is that it's wrong to use a Recordset at all in .NET. Ideally everyone would be using ADO.NET in .NET apps rather than ADO. The only reason that I would consider valid for using ADO is when a VB6 app is upgraded to VB.NET using a conversion wizard. Even then, if the app is intended to be modified in future, I would think that converting data access to ADO.NET to be the preferred option.

    That aside, the question still arises whether to use a DataSet/DataTable directly in the presentation code. The answer is ideally not. Beginners will generally start that way and Microsoft even make it simple to add DataSets to a form at design time. No professional app should work that way though. There should always be an abstraction so that the UI is not dependent on a specific data access technology. In this case, by the method returning a List(Of String), you could change from ADO to ADO.NET to EF to anything else and the consumer wouldn't have to know or care because the method will continue to return a List(Of String).
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width