0

I'm trying to create an Inherit class that can interface with a datarow and auto-fill all of its data. To do that I've create a Dictionary like this:

reyRelation As New Dictionary(Of String, String)
rey.Add("Id", "id_user")
rey.Add("Name", "name")
rey.Add("Surname", "surname")

where the "keys" are the name of the property, and the "values" are the name of column inside the DataRow object.

So, I want to create a function called "load" for example, like this:

    Try
        Dim row As DataRow = Me.getRowById()
        For Each key As String In Me.reyRelation.Keys
            Me.key = getRowById(reyRelation.Item(key))
        Next

    Catch ex As Exception
        MessageBox.Show(ex.Message, "MODEL Error in Utente: Errore caricamento dati",     MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

Where:

1 - The Me.getRowById() is a function inside the class declared MustOverride so every class can use the correct dataAdapter.

2 - The Me.key is obviously the focus of my question: I can't do something like this because I find an error, so that's the question: How I can access from the string key at the property to set its value?

I hope I was clear with the explanation! Thank you!

Community
  • 1
  • 1
DuffMck
  • 81
  • 7
  • 3
    possible duplicate of [Set object property using reflection](http://stackoverflow.com/questions/619767/set-object-property-using-reflection) – Mark Jan 09 '15 at 15:19
  • Have C# and vb the same sintax? Sorry, I don't know c# language.. – DuffMck Jan 09 '15 at 15:22
  • 4
    Same syntax, no, but the libraries (e.g. Reflection) are the same, and unfortunately, much of the .NET information available is in C#, so it's work knowing at least how to translate from C# to VB.NET. In many cases, you just need to remove semi-colons. e.g. From [this answer](http://stackoverflow.com/a/8942565/2278086) you could do `Me.GetType().GetProperty(key).SetValue(Me, getRowById(reyRelation.Item(key)), Nothing)`. Performance could be an issue, so you may need to cache the `PropertyInfo` instances or look for a library that does that for you (one option also mentioned in that answer). – Mark Jan 09 '15 at 15:46
  • Thank you very much! Your explanations are really clear and helpful! – DuffMck Jan 09 '15 at 16:01

1 Answers1

0

I have found an answer following suggestions by @Mark (Thank you and sorry for my stupid question about C# and vb..), so that's the code that solve my problem, I hope it would be helpful for someone else!

Public Sub load()
    Try
        Dim row As DataRow = Me.getRowById()
        For Each key As String In Me.reyRelation.Keys
            If key <> "Id" Then

                Dim type As Type = Me.GetType
                Dim propInfo As System.Reflection.PropertyInfo = type.GetProperty(key)
                Dim value As Object = row.Item(reyRelation.Item(key))
                If value Is DBNull.Value Then
                    propInfo.SetValue(Me, value.ToString, Nothing)
                Else
                    propInfo.SetValue(Me, value, Nothing)
                End If

            End If
        Next
    Catch ex As Exception
        MessageBox.Show(ex.Message, "MODEL Error in Utente: Errore caricamento dati", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub
T.S.
  • 18,195
  • 11
  • 58
  • 78
DuffMck
  • 81
  • 7