Results 1 to 2 of 2

Thread: Custom type conversion

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    15

    Custom type conversion

    Hi all,

    How do we convert a source type into a destination type before using reflection to assign value from source field to destination field in below code snippet:

    Code:
    Try
                    tempPropertyInfo = tempDestinationObject.GetType.GetProperty(sourceObjectName)
                    If Not tempPropertyInfo Is Nothing AndAlso sourceDataTable.Columns.Contains(sourceObjectName) AndAlso tempPropertyInfo.CanWrite Then
                        Select Case tempPropertyInfo.PropertyType.Namespace
                            Case "System"
                                tempPropertyValue = CObj(sourceDataTable.Rows(rowCount).Item(sourceObjectName))
                                If Not tempPropertyValue Is Nothing AndAlso tempPropertyInfo.PropertyType.FullName = "System.DateTime" Then
                                    tempPropertyValue = CDate(tempPropertyValue)
                                End If
                            Case Else
                                tempPropertyValue = Activator.CreateInstance(tempPropertyInfo.PropertyType)
                                tempPropertyValue.Value = CObj(sourceDataTable.Rows(rowCount).Item(sourceObjectName))
                        End Select
                        If Not tempPropertyValue Is Nothing Then
                            tempPropertyInfo.SetValue(tempDestinationObject, tempPropertyValue, Nothing)
                        End If
                    End If
                Catch ex As Exception
                End Try
    When I try to assign a boolean value to a custom data type, I get an unhandled excpetion here

    Code:
    tempPropertyValue.Value = CObj(sourceDataTable.Rows(rowCount).Item(sourceObjectName))

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Custom type conversion

    What's the exception message? I notice that you are taking what you suggest might be a boolean, converting it to object, and assigning it to the Value member. You shouldn't need the CObj call, as Item will return an Object anyways, but is Value of type Object? If Value is of type boolean, then CBool would be the correct conversion. However, if Item(sourceObjectName) is not a Boolean, or is Null, that would cause problems, as well.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width