Get an objects value as a string

  • Thread starter Thread starter Ray Cassick \(Home\)
  • Start date Start date
R

Ray Cassick \(Home\)

I am looking for a generic way to see if an object supports the ToString
method, then invoke that method to return the results of that generic
objects ToString method.

I have a generic exception class and one of the arguments I pass in is a
generic object type.

In this exception class I would like to be able to provide a ToString method
where this value is shown, but the exception class cannot know anything
about the object that is getting passed in. I want to keep it as generic as
possible that way.

I have been poking around with InvokeMember and just can't seem to get it
right.

Can anyone point me in the right direction?

--
Raymond R Cassick
CEO / CSA
Enterprocity Inc.
www.enterprocity.com
3380 Sheridan Drive, #143
Amherst, NY 14227
V: 716-316-7537
Blog: http://spaces.msn.com/members/rcassick/
 
Well I should have known, after trying for 2 hours and posting this message
I worked another few minutes and got it...

Friend Function GetStringValue(ByVal inputData As Object) As String

Dim retval As String
Dim types(-1) As System.Type
Dim inputDataType As Type = inputData.GetType

retval = inputDataType.InvokeMember("ToString",
BindingFlags.InvokeMethod, Nothing, inputData, types)

Return retval

End Function
 
Well I should have known, after trying for 2 hours and posting this message
I worked another few minutes and got it...

Friend Function GetStringValue(ByVal inputData As Object) As String

Dim retval As String
Dim types(-1) As System.Type
Dim inputDataType As Type = inputData.GetType

retval = inputDataType.InvokeMember("ToString",
BindingFlags.InvokeMethod, Nothing, inputData, types)

Return retval

End Function

I'm not sure why you need to go to these lengths? Every value in VB.NET
has a ToString method, since everything inherits from System.Object. If
you simply call inputData.ToString () you will get the proper result -
even if it's overridden:

Option Strict On
Option Explicit On

Imports System

Module modMain
' Our Entry Point
Public Sub Main ()
Dim t As Object = New Testing ()
Console.WriteLine (t.ToString ())
End Sub

Private Class Testing
Public Overrides Overloads Function ToString () As String
Return "Hello, From Testing"
End Function
End Class
End Module
 
This is what I had figured as well but it did not seem to look right in the
IDE.

Dim testData As Object

Then when I went to try to do a Debug.WriteLine the .ToString method did not
appear after the dot. All I got was GetType.

....but when I typed in the following...

Debug.WriteLine(testData.ToString)

....it seemed to take and even compile.. seems odd that the member would be
valid but not show up in IntelliSense like that.
 
Oh boy does that really tick me off!

Why the heck would they HIDE ANYTHING from a developer!

And... how can I do that?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top