Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5944543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:33:07+00:00 2026-05-22T16:33:07+00:00

Is it possible to replace these two functions Function f1 (s As String) As

  • 0

Is it possible to replace these two functions

Function f1 (s As String) As class1()
  Dim a() As String = Split(s)
  Dim b(UBound(a)) As class1
  For i As Integer = 0 To UBound(a)
    b(i) = New class1(a(i))
  Next
  f1 = b
End Function

and

Function f2 (s As String) As class2()
  Dim a() As String = Split(s)
  Dim b(UBound(a)) As class2
  For i As Integer = 0 To UBound(a)
    b(i) = New class2(a(i))
  Next
  f2 = b
End Function

with a generic function something like

Function fGeneric (s As String,T As Type) As T()
  Dim a() As String = Split(s)
  Dim b(UBound(a)) As T
  For i As Integer = 0 To UBound(a)
    b(i) = New T(a(i))
  Next
  fGeneric = b
End Function

?

SOLUTION

Class class1
  Private _Name As String
  Public ReadOnly Property Name As String
    Get
      Name = _Name
    End Get
  End Property
  Sub New(s As String)
    _Name = s
  End Sub
End Class

Function fGeneric(Of T) (s As String) As IEnumerable(Of T)
  Dim a() As String = Split(s)
  Dim b(UBound(a)) As T
  For i As Integer = 0 To UBound(a)
    b(i) = GetType(T).InvokeMember( _
      Nothing, _
      BindingFlags.DeclaredOnly Or _
      BindingFlags.Public Or _
      BindingFlags.NonPublic Or _
      BindingFlags.Instance Or _
      BindingFlags.CreateInstance, _
      Nothing, _
      Nothing, _
      New Object(){a(i)} _
    )
  Next
  fGeneric = b
End Function

Response.Write(fGeneric(Of class1)("a b c")(1).Name) outputs b

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:33:07+00:00Added an answer on May 22, 2026 at 4:33 pm
    Function fGeneric(Of T) (ByVal s As String, ByVal createT As Func(Of String, T)) As IEnumerable(Of T)
      Return Split(s).Select(Function(a) createT(a))
    End Function
    

    call it like this

    fGeneric(Of class1)("some, string, value", Function(a) Return New class1(a))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Replace words in a string, but ignore HTML Is it possible to
Is it possible to replace the standard broken image via CSS or using another
Is it possible to replace the Mac OS X login window, /System/Library/CoreServices/loginwindow.app, with a
Is it possible to replace all PNGs across the board with GIFs using jquery?
Is it possible to replace a method of a Moose object at runtime ?
I know that it's possible to replace the browse button, which is generated in
Possible Duplicate: Make iPhone app paid version replace free version on install from app
Is it possible for a TFSBuild to replace part of your web.config settings with
Is it possible to find and replace in selected text in Snow Leopard Xcode
Is it possible to do a regex replace on all IMG tags that are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.