1

I've a list of object. I want get all values in a field I've dynamically. I explain more. I read all property of my dynamic object. When I found an image field, i want obtain all images in my list of object.

  For Each col In Ret.StructTbl.LstCol
      If col.EstImage Then
          col.Liste = GetType(List(Of Eleve)).GetProperty(col.SQLName).GetValue(Ret.LstDatas.LstObj, Nothing) 
      End If
  Next

I use it (vb.net), but I've

La référence d'objet n'est pas définie à une instance d'un objet

I think it's because I've some null value or I take a wrong way? It's the follow of filter and sorter dynamic null value

My eleve Stucture

Public Class Eleve

<TableFullOption.TFOAttribute("Key")> Property Id As Integer
<TableFullOption.TFOAttribute("Image")> Property Img As String
Property Name As String
Property Major As Boolean
<TableFullOption.TFOAttribute("FK_Sexe")> Property Sex As Integer
Property English As Nullable(Of Integer)
Property Japanese As Nullable(Of Double)
Property Calculus As Decimal
Property Geometry As Integer
End Class

and col.SQLName= "Img"

Can you help me... I want to make a list of all image I've (mypicture.jpg, img1.png ...)

Community
  • 1
  • 1
YannickIngenierie
  • 486
  • 1
  • 9
  • 24

2 Answers2

1

This GetProperty() statement returns Nothing because the property name is not found on type List(T).

GetType(List(Of Eleve)).GetProperty(col.SQLName) = Nothing ' true

The exception is raised in the next call to GetValue(), because you are essentially calling a method on a null reference.

Nothing.GetValue(...)

Did you intend to find a property on type Eleve instead?

GetType(Eleve).GetProperty(col.SQLName)
Steven Liekens
  • 10,761
  • 4
  • 50
  • 77
  • You write... But when I tried that before ask help, I've error cause I search on list of Eleve : GetValue(Ret.LstDatas.LstObj, Nothing) I've message : The object does not match the target type – YannickIngenierie Jan 06 '15 at 11:16
0

I find ... First I create a smart function

 Public Shared Function GetColumn(Of MaClass As Class)(ByVal items As List(Of MaClass), ByVal columnName As String) As List(Of String)
    Return items.Select(Function(x) If(x.GetType().GetProperty(columnName).GetValue(x, Nothing), String.Empty).ToString).ToList
End Function

And I call like

 For Each col In Ret.StructTbl.LstCol
     If col.EstImage Then
         col.Liste = GetColumn(Of Eleve)(Ret.LstDatas.LstObj, col.SQLName)
     End If
 Next
YannickIngenierie
  • 486
  • 1
  • 9
  • 24