Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object list as parameter

Status
Not open for further replies.

Peppi

Programmer
Apr 9, 2001
205
CA
Hi,

I'm not sure why the following does not work:

Code:
Public Shared Function ListToString(ByVal objList As List(Of Object)) As String
     Dim sb As New StringBuilder

     For Each obj As Object In objList
         sb.AppendFormat("{0},", obj.ToString)
     Next

     Return sb.ToString.TrimEnd(Convert.ToChar(","))
End Function
...
Dim result as String = ListToString(ReviewApprovers)

ReviewApprovers is a list of ContentReviewApprovers. It's complaining about invalid arguments being passed.
How can I get this to work?

Thx.
 
List(of Object) is the same as ArrayList. all objects have the ToString member, so as long as it's not null there won't be an isse. you could pass IEnumerable as the argument type and pass any list.
Code:
Public Shared Function ListToString(ByVal objList As IEnumerable) As String
     Dim sb As New StringBuilder

     For Each obj As Object In objList
         If obj == null Then Continue
         sb.AppendFormat("{0},", obj.ToString)
     Next

     Return sb.ToString.TrimEnd(Convert.ToChar(","))
End Function

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi,

The problem is not inside of the function. The problem is in calling the function like so:

Code:
Dim result as String = ListToString(ReviewApprovers)

It is complaining about the argument that I am passing, which is a list of a custom type called ContentReviewApprovers. I don't understand why I can't pass this list to a method that expects a list of objects.

Thx.
 
List<ContentReviewApprovers> != List<Object>

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Jesus, that's incredibly helpful. Thanks! Off to find a better forum.
 
This forum is not your help desk! You ask alot of questions here, but you do not contribute. It is a 2 way street here. You have been given much help in the past and posted no answers to others. To expect immediate answers and contribute nothing is very selfish on your part.
 
Jesus, that's incredibly helpful. Thanks! Off to find a better forum.
106 questions asked yet no help given to other people on the site? I hope you enjoy leeching from your new found "better forum"...


Mark,

[URL unfurl="true"]http://lessthandot.com[/url] - Experts, Information, Ideas & Knowledge
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Website Design
[URL unfurl="true"]http://aspnetlibrary.com[/url] - An online resource for professional ASP.NET developers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top