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

  • Home
  • SEARCH
  • 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 203811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:24:43+00:00 2026-05-11T17:24:43+00:00

I’ve got a f(x) that returns a collection of user controls. .Net lets me

  • 0

I’ve got a f(x) that returns a collection of user controls. .Net lets me just treat the f(x) name as the collection. Is there something wrong with doing so?

ex)

Private Function GetCcB() As Collection(Of Reports_ucColumn)
    Dim cc As New Collection(Of Reports_ucColumn)
    cc.Add(Me.ucColumn0)
    cc.Add(Me.ucColumn1)
    cc.Add(Me.ucColumn2)
    Return cc
End Function

Then later on, all use the properties on the objects in the collection, like this:

    For i As Integer = 0 To csB.Count - 1
        If csB(i).Contains("Invalid") Or csB(i).Contains("Duplicate") Then
            GetCcB.Item(i).isValid = False
            GetCcB.Item(i).title = csB(i)
            If csB(i).Contains("Invalid") Then : GetCcB.Item(i).errCode = "005W"
            Else : GetCcB.Item(i).errCode = "005W" 'todo - chg this once we get a duplicate col err code
            End If
        Else
            GetCcB.Item(i).isValid = True
            GetCcB.Item(i).title = csB(i)
            GetCcB.Item(i).errCode = String.Empty
        End If
  • 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-11T17:24:43+00:00Added an answer on May 11, 2026 at 5:24 pm

    By calling the method each time that you use the collection, what your code is effectively doing is:

    For i As Integer = 0 To csB.Count - 1
       Dim cc As Collection(Of Reports_ucColumn)
       If csB(i).Contains("Invalid") Or csB(i).Contains("Duplicate") Then
          cc = New Collection(Of Reports_ucColumn)
          cc.Add(Me.ucColumn0)
          cc.Add(Me.ucColumn1)
          cc.Add(Me.ucColumn2)
          cc.Item(i).isValid = False
          cc = New Collection(Of Reports_ucColumn)
          cc.Add(Me.ucColumn0)
          cc.Add(Me.ucColumn1)
          cc.Add(Me.ucColumn2)
          cc.Item(i).title = csB(i)
          If csB(i).Contains("Invalid") Then
             cc = New Collection(Of Reports_ucColumn)
             cc.Add(Me.ucColumn0)
             cc.Add(Me.ucColumn1)
             cc.Add(Me.ucColumn2)
             cc.Item(i).errCode = "005W"
          Else
             cc = New Collection(Of Reports_ucColumn)
             cc.Add(Me.ucColumn0)
             cc.Add(Me.ucColumn1)
             cc.Add(Me.ucColumn2)
             cc.Item(i).errCode = "005W" 'todo - chg this once we get a duplicate col err code
          End If
       Else
          cc = New Collection(Of Reports_ucColumn)
          cc.Add(Me.ucColumn0)
          cc.Add(Me.ucColumn1)
          cc.Add(Me.ucColumn2)
          cc.Item(i).isValid = True
          cc = New Collection(Of Reports_ucColumn)
          cc.Add(Me.ucColumn0)
          cc.Add(Me.ucColumn1)
          cc.Add(Me.ucColumn2)
          cc.Item(i).title = csB(i)
          cc = New Collection(Of Reports_ucColumn)
          cc.Add(Me.ucColumn0)
          cc.Add(Me.ucColumn1)
          cc.Add(Me.ucColumn2)
          cc.Item(i).errCode = String.Empty
       End If
    Next
    

    You would never write code like that, would you?

    You can turn the function into a property with lazy initialisation. Then it’s ok to use it over and over, as the collection is only created once:

    Private _cc As Collection(Of Reports_ucColumn)
    
    Private Property CcB As Collection(Of Reports_ucColumn)
       Get
          If _cc Is Nothing Then
             _cc = New Collection(Of Reports_ucColumn)
             _cc.Add(Me.ucColumn0)
             _cc.Add(Me.ucColumn1)
             _cc.Add(Me.ucColumn2)
          End If
          Return _cc
       End Get
    End Property
    

    (Ideally the property should be public in a class and the _cc variable should be private, encapsulated in the class so that only the property could access it.)

    Semantically this works fine also. A function generally does a bit of work, so you shouldn’t call it over and over if you want to use the result over and over. A property on the other hand usually doesn’t do much work, and if the result needs some processing to be created, the property usually does something like lazy initialisation or pre-initialisation to make sure that the work isn’t done more than neccessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.