Получить из словаря, который является экземпляром объекта - PullRequest
0 голосов
/ 02 июля 2019

Я использую функцию, которая возвращает объект, а объект является списком.Я не могу использовать индекс для получения свойств.

Мой код:

Получить объект:

Object myObject = x.ImportDataFromX(Int32.Parse(txtX.Text),dataGViewX.Rows[i].Cells[6].Value.ToString());

Использовать индекс:

dataGViewX.Rows[i].Cells[5].Value = myObject[5]; //ERROR

Функция:

Public Function ImportDataFromX(ByVal ClientID As Integer, ByVal BarcodeID As String) As Object
        Dim DrByBarcode As DataTable = GetTable("select MainInfo,FromNumber,ToNumber,FromDate,ToDate,BeurDate,Field1,Field2,Field3 from BoxsInfo inner join TblBoxsPreIndex on BoxsInfoID=[BoxsInfo].id where TblBoxsPreIndex.[ClientId]=" & ClientID & " and [FormId]=-5000 and TblBoxsPreIndex.BarcodeID='" & BarcodeID & "'")

        Select Case DrByBarcode.Rows.Count
            Case 0
                Return "can't find  record to barcode: " & BarcodeID
            Case 1
                Dim Dr As New Dictionary(Of String, String)
                Dr.Add("MainInfo", IIf(IsDBNull(DrByBarcode.Rows(0).Item("MainInfo")), "", DrByBarcode.Rows(0).Item("MainInfo")))
                Dr.Add("FromNumber", IIf(IsDBNull(DrByBarcode.Rows(0).Item("FromNumber")), "", DrByBarcode.Rows(0).Item("FromNumber")))
                Dr.Add("ToNumber", IIf(IsDBNull(DrByBarcode.Rows(0).Item("ToNumber")), "", DrByBarcode.Rows(0).Item("ToNumber")))
                Dr.Add("FromDate", IIf(IsDBNull(DrByBarcode.Rows(0).Item("FromDate")), "", DrByBarcode.Rows(0).Item("FromDate")))
                Dr.Add("ToDate", IIf(IsDBNull(DrByBarcode.Rows(0).Item("ToDate")), "", DrByBarcode.Rows(0).Item("ToDate")))
                Dr.Add("BeurDate", IIf(IsDBNull(DrByBarcode.Rows(0).Item("BeurDate")), "", DrByBarcode.Rows(0).Item("BeurDate")))
                Dr.Add("Field1", IIf(IsDBNull(DrByBarcode.Rows(0).Item("Field1")), "", DrByBarcode.Rows(0).Item("Field1")))
                Dr.Add("Field2", IIf(IsDBNull(DrByBarcode.Rows(0).Item("Field2")), "", DrByBarcode.Rows(0).Item("Field2")))
                Dr.Add("Field3", IIf(IsDBNull(DrByBarcode.Rows(0).Item("Field3")), "", DrByBarcode.Rows(0).Item("Field3")))


                Return Dr

            Case > 1
                Return " barcode" & BarcodeID & "duplicate"
        End Select
    End Function

Что мне делать?

1 Ответ

0 голосов
/ 02 июля 2019

Вы можете получить свои значения, приведя ваши object к Dictionary:

dataGViewX.Rows[i].Cells[5].Value = ((Dictionary<string, string>)myObject)["BeurDate"];

или

dataGViewX.Rows[i].Cells[5].Value = (myObject as Dictionary<string, string>)["BeurDate"];

Если вы хотите получить свое значение по индексу, вы можетеиспользуйте ElementAt:

dataGViewX.Rows[i].Cells[5].Value = ((Dictionary<string, string>)myObject).Values.ElementAt(5);

Но имейте в виду, что порядок Dictionary не гарантируется.Поэтому вместо того, чтобы использовать индекс вашего значения, используйте ключ Dictionary.Или вы можете использовать OrderedDictionary .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...