I'm a newb to VB.NET and I'm having trouble with understanding how the Return actually works in a Function. The terminology I use will probably be incorrect but hopefully you'll understand what I'm driving toward.
I have a public module with a public function which opens an oledb connection to a csv file and gets the data. The function 'Return' csvDataSet. From my main form I can dim a new dataset (ds) and do the following:
Dim ds As New DataSet
DataGridView1.DataSource = FillDataSet(ds).Tables(0).DefaultView
DataGridView1 populates and all is well.
However, I've read that if I use a Class instead of a Module and use a Public Shared Function to return csvDataSet, it should be Static and I wouldn't need a new instance (in this case ds). I can't seem to make that work, so I know I'm missing something stupid in my understanding. If csvDataSet is Returned from a Public Shared Function inside a Public Class, should I always be able to set something to that csvDataSet i.e.
DataGridView1.DataSource = csvDataSet.Tables(0).DefaultView 'or something similar?
I know it's not a big deal to do it the way I'm doing it and Dim ds as New DataSet and get it that way. I just want to understand why I can't just use csvDataSet if it already contains all the pertinent data. Hopefully that was a tad clearer than mud!
Adding Code as requested. Module:
Imports System.Data.OleDb
Imports System.IO
Module modGetData
'Public strLastFirst As String
Public Function FillDataSet(ByVal csvDataSet As DataSet) As DataSet
Dim fi As New FileInfo("c:\prospects.csv")
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Text;Data Source=" & fi.DirectoryName
Dim conn As New OleDbConnection(connectionString)
Dim cmdSelect As New OleDbCommand("SELECT * FROM " & fi.Name, conn)
Dim adapter1 As New OleDbDataAdapter
conn.Open()
adapter1.SelectCommand = cmdSelect
adapter1.Fill(csvDataSet, "DATA")
Return csvDataSet
conn.Close()
End Function
End Module
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim ds As New DataSet
DataGridView1.DataSource = FillDataSet(ds).Tables(0).DefaultView