locked
How to get object value from function? RRS feed

  • Question

  • User-256799415 posted

    Please help me to solve this problem.

    I need to work with object whose values are taken from database. I do the database scan in Function scan and it seems to run well. But i still have the problem, how to get values (result of function scan) out of the function?

    Here is my code. I'm a beginner in VB .Net. So, if there's any mistake in my basic knowledge, please let me know it.

    Public Class data
        Dim _field1 As Integer
        Dim _field2 As Integer
        Dim _field3 As Integer
        Dim _field4 As Integer
        Dim _field5 As Integer
    
        Public Property field1() As Integer
            Get
                Return _field1
            End Get
            Set(ByVal value As Integer)
                _field1 = value
            End Set
        End Property
    
        Public Property field2() As Integer
            Get
                Return _field2
            End Get
            Set(ByVal value As Integer)
                _field2 = value
            End Set
        End Property
    
        Public Property field3() As Integer
            Get
                Return _field3
            End Get
            Set(ByVal value As Integer)
                _field3 = value
            End Set
        End Property
    
        Public Property field4() As Integer
            Get
                Return _field4
            End Get
            Set(ByVal value As Integer)
                _field4 = value
            End Set
        End Property
    
        Public Property field5() As Integer
            Get
                Return _field5
            End Get
            Set(ByVal value As Integer)
                _field5 = value
            End Set
        End Property
    End Class

    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            scan()
    
            'HERE IS THE PROBLEM. HOW TO GET VALUE FROM arr?'
    
        End Sub
    
        Public Function scan() As Object
            Dim ds As New DataSet
            Dim connetionString As String
            Dim oledbCnn As OleDbConnection
            Dim oledbAdapter_1 As OleDbDataAdapter
            Dim sql_list As String
    
            connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=datasource.mdb;"
            sql_list = "SELECT * FROM sample"
    
            oledbCnn = New OleDbConnection(connetionString)
            oledbCnn.Open()
            oledbAdapter_1 = New OleDbDataAdapter(sql_list, oledbCnn)
            oledbAdapter_1.Fill(ds)
            Dim arr(ds.Tables(0).Rows.Count - 1) As data
    
            For i = 0 To (ds.Tables(0).Rows.Count - 1)
                Dim temp As data = New data
                temp.field1 = ds.Tables(0).Rows(i).Item(0)
                temp.field2 = ds.Tables(0).Rows(i).Item(1)
                temp.field3 = ds.Tables(0).Rows(i).Item(2)
                temp.field4 = ds.Tables(0).Rows(i).Item(3)
                temp.field5 = ds.Tables(0).Rows(i).Item(4)
                arr(i) = temp
            Next
            Return arr
        End Function
    End Class
    Tuesday, April 3, 2012 9:30 PM

Answers

  • User-256799415 posted

    I've solved this :D

    I just need to fix the function declaration

    Public Function scan() As data()

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 3, 2012 11:54 PM

All replies

  • User2105670541 posted

    write down:

    Dim ob as Object= scan() 

    Tuesday, April 3, 2012 11:49 PM
  • User-256799415 posted

    I've solved this :D

    I just need to fix the function declaration

    Public Function scan() As data()

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 3, 2012 11:54 PM