我一直在尝试创建一个返回类型对象的函数。下面的这个函数目前在我的数据类中,可以从任何继承数据类的对象调用。我的问题是当我尝试返回我的对象时。当我在函数内部时它会很好地填充。当我回到调用对象时,它全部为空,就像它是一个新对象。
这是函数....
Public Overloads Function GetClassFromDB(ByVal ID As Integer) As Object
Try
Dim BaseObject As New Object
Dim objDerived As Type = MyBase.GetType()
Dim TableName As String = String.Empty
Dim SQL As New LottoPayload.SQLiDataClass
Dim SQLString As String = String.Empty
Dim SQLCommand As SQLiteCommand = Nothing
Dim SQLConnection As SQLiteConnection = Nothing
Dim SQLiteDRObj As SQLiteDataReader = Nothing
Dim SQLResultsTable As New DataTable
'Create an instance of the base object
BaseObject = Activator.CreateInstance(objDerived)
'Get the tablename from the object
TableName = objDerived.GetProperty("TableName").GetValue(BaseObject, Nothing).ToString
SQLString = "SELECT * FROM " & TableName & " WHERE ID = '" & ID.ToString & "' LIMIT 1"
SQLConnection = SQL.GetSqlConnection()
SQLConnection.Open()
SQLCommand = New SQLiteCommand(SQLConnection)
SQLCommand.CommandText = SQLString
SQLiteDRObj = SQLCommand.ExecuteReader()
SQLResultsTable.Load(SQLiteDRObj)
If SQLResultsTable.Rows.Count > 0 Then
For Each Row As DataRow In SQLResultsTable.Rows
For Each Column As DataColumn In Row.Table.Columns
Dim ColumnName As String = Column.ColumnName.ToString
Console.WriteLine(Row.Item(ColumnName))
Dim ColumnValue As Object = Row.Item(ColumnName)
Console.WriteLine(Column.DataType.Name & " ")
Select Case Column.DataType.Name
Case "Int64"
Dim ConvertedValue As Integer
Integer.TryParse(ColumnValue.ToString, ConvertedValue)
objDerived.GetProperty(ColumnName).SetValue(BaseObject, ConvertedValue, Nothing)
Case "Double"
Dim ConvertedValue As Double
Double.TryParse(ColumnValue.ToString, ConvertedValue)
objDerived.GetProperty(ColumnName).SetValue(BaseObject, ConvertedValue, Nothing)
Case "String"
Dim ConvertedValue As String
ConvertedValue = ColumnValue.ToString
objDerived.GetProperty(ColumnName).SetValue(BaseObject, ConvertedValue, Nothing)
Case Else
Dim ConvertedValue As String
ConvertedValue = "NotSet"
objDerived.GetProperty(ColumnName).SetValue(BaseObject, ConvertedValue, Nothing)
End Select
Next
Next
End If
Return objDerived
SQLiteDRObj.Close()
SQLConnection.Close()
SQLConnection.Dispose()
'Return False
Catch ex As Exception
Return False
End Try
End Function
这就是它的调用方式。
Dim objResults As New myapp.objResult
Dim ID As Integer = 3
objResults.GetClassFromDB(ID)
问题#1 - 当我从GetClassFromDB
返回对象时,objResults是空的,尽管它有数据直到Return objDerived
。为什么会这样?
问题#2 - 有没有办法可以消除Select Case
区域并使用反射合并代码?
提前感谢您的帮助。
答案 0 :(得分:0)
鉴于BaseObject
实际上是一个objDerived
对象,表示创建的对象的类型而不是对象,您不应该返回objDerived
而不是Type
本身?如果你没有首先使用像objDerived
这样令人震惊的名字,那就更清楚了。
答案 1 :(得分:0)
问题是你的方法返回一个对象,但你没有在任何地方将它赋值给一个变量。您需要将方法的返回值分配给变量
Dim someVariable As Object = objResults.GetClassFromDB(ID)
另外,你的方法很奇怪,如果代码成功运行它会返回一个对象,但是如果有一个异常,你会默认吞下一个布尔值(这也是一个不好的做法)。
答案 2 :(得分:0)
您正在返回错误的对象。 objDerived是一个Type,而不是您正在设置的对象。您需要返回BaseObject。另外,要意识到关闭和处理SqlCommand和Connection的代码不会被命中,因为它在返回运算符之后。这里最好将连接和命令包装在Using子句中,这将确保它们在超出范围时将被处理掉。看看以下是否有效:
Public Overloads Function GetClassFromDB(ByVal ID As Integer) As Object
Try
Dim BaseObject As New Object
Dim objDerived As Type = MyBase.GetType()
Dim TableName As String = String.Empty
Dim SQL As New LottoPayload.SQLiDataClass
Dim SQLString As String = String.Empty
Using SQLConnection As SQLiteConnection = Nothing
Using SQLCommand As SQLiteCommand = Nothing
Dim SQLiteDRObj As SQLiteDataReader = Nothing
Dim SQLResultsTable As New DataTable
'Create an instance of the base object
BaseObject = Activator.CreateInstance(objDerived)
'Get the tablename from the object
TableName = objDerived.GetProperty("TableName").GetValue(BaseObject, Nothing).ToString
SQLString = "SELECT * FROM " & TableName & " WHERE ID = '" & ID.ToString & "' LIMIT 1"
SQLConnection = SQL.GetSqlConnection()
SQLConnection.Open()
SQLCommand = New SQLiteCommand(SQLConnection)
SQLCommand.CommandText = SQLString
SQLiteDRObj = SQLCommand.ExecuteReader()
SQLResultsTable.Load(SQLiteDRObj)
If SQLResultsTable.Rows.Count > 0 Then
For Each Row As DataRow In SQLResultsTable.Rows
For Each Column As DataColumn In Row.Table.Columns
Dim ColumnName As String = Column.ColumnName.ToString
Console.WriteLine(Row.Item(ColumnName))
Dim ColumnValue As Object = Row.Item(ColumnName)
Console.WriteLine(Column.DataType.Name & " ")
Select Case Column.DataType.Name
Case "Int64"
Dim ConvertedValue As Integer
Integer.TryParse(ColumnValue.ToString, ConvertedValue)
objDerived.GetProperty(ColumnName).SetValue(BaseObject, ConvertedValue, Nothing)
Case "Double"
Dim ConvertedValue As Double
Double.TryParse(ColumnValue.ToString, ConvertedValue)
objDerived.GetProperty(ColumnName).SetValue(BaseObject, ConvertedValue, Nothing)
Case "String"
Dim ConvertedValue As String
ConvertedValue = ColumnValue.ToString
objDerived.GetProperty(ColumnName).SetValue(BaseObject, ConvertedValue, Nothing)
Case Else
Dim ConvertedValue As String
ConvertedValue = "NotSet"
objDerived.GetProperty(ColumnName).SetValue(BaseObject, ConvertedValue, Nothing)
End Select
Next
Next
End If
End Using ' Sql Command
End Using ' Sql Connection
Return BaseObject
'Return False
Catch ex As Exception
Return False
End Try
End Function
结束班
从第二个屏幕截图中,您似乎没有为GetClassFromDB(ID)方法的返回设置任何内容。它可能应该是这样的:
Dim result = objResults.GetClassFromDB(ID)
' Do something with result