Converting C# to VB.Net - Reflection Option Strict Error

Hi,

I wrote the following code in C#, and it works absolutely brilliantly in being able to read any class that is passed to it (I use this on error logging), extract the properties, and values of this object, so that it can be e-mailed

I have attempted to convert this to vb.net and get the error:

Option Strict On prohibits operands of type Object for operator '&'

From what I understand, I have to define the type of the object toConvert - but the point of the function is that the code determins this dynamically!

Is there any way around this?  Do not like vb.net!!



c# code
/// <summary>
        /// Convert the properties of an object that has raised an error to string so that it can be displayed as text
        /// </summary>
        /// <param name="toConvert">Object</param>
        /// <returns>String of containing object properties and their values</returns>
        private static string ConvertPropertiesToString(object toConvert)
        {
            string LogMsg = "";
            Type convertType = toConvert.GetType();

            foreach (PropertyInfo propInfo in convertType.GetProperties())
            {
                if (propInfo.CanRead)
                {
                    LogMsg += propInfo.Name + ":\t\t" + propInfo.GetValue(toConvert, null) + "\n";
                }
            }

            return LogMsg;
        }

Open in new window


vb.net code
''' <summary>
    ''' Convert the properties of an object that has raised an error to string so that it can be displayed as text
    ''' </summary>
    ''' <param name="toConvert">Object</param>
    ''' <returns>String of containing object properties and their values</returns>
    Private Shared Function ConvertPropertiesToString(toConvert As Object) As String
        Dim LogMsg As String = ""
        Dim convertType As Type = toConvert.[GetType]()

        For Each propInfo As PropertyInfo In convertType.GetProperties()
            If propInfo.CanRead Then
                LogMsg = LogMsg & propInfo.Name & ":"
                LogMsg = LogMsg & vbTab & vbTab
                LogMsg = LogMsg & propInfo.GetValue(toConvert, Nothing)
                LogMsg = LogMsg & vbLf
            End If
        Next

        Return LogMsg
    End Function

Open in new window


Line causing error: LogMsg = LogMsg & propInfo.GetValue(toConvert, Nothing)
LVL 1
Member_4438002Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Meir RivkinFull stack Software EngineerCommented:
change it to:
LogMsg = LogMsg & propInfo.GetValue(toConvert, Nothing).ToString

Open in new window

since propInfo.GetValue() returns object.
also u might want to check if propInfo.GetValue() returns Nothing but its up to you.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Member_4438002Author Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.