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 8306225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:17:15+00:00 2026-06-08T18:17:15+00:00

Suppose i have this class: class Foo Public Property a() As Integer Private _l

  • 0

Suppose i have this class:

class Foo
 Public Property a() As Integer
 Private _l As List(Of Integer) = New List(Of Integer)
 Public Property l() As List(Of Integer)
    Get
     Return _l
    End Get
    Set(value As List(Of Integer))
     _l = value
    End Set
 End Property    
end class

I am trying to use initializer list to initialize the properties:

Dim f as Foo = New Foo With {.a = 1, .l.Add(1)}

The above doesn’t work. I am getting a bit confused here. Now, in an initializer list from what i understand, i can initialize the collection like this:

Dim f as Foo = New Foo With {.a = 1, .l = New List(Of Integer){1}}

But what i need to do, since _l is already pointing to an object when i say New Foo, is only add the elements without saying ..., .l = New List(Of Integer){1}}
. Why isn’t it working?

  • 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-06-08T18:17:16+00:00Added an answer on June 8, 2026 at 6:17 pm

    It’s possible using collection initializer syntax, i.e. Dim f As New Foo From {1, 2, 3} (see below). However, this has a few drawbacks:

    • You cannot use collection initializer syntax and object initializers (e.g. With {.a = 1}) at the same time. Thus, you’ll have to move initialization of property a to a constructor.
    • Your class must implement IEnumerable and have an Add method.

    That said, here is a working example:

    Class Foo
        Implements IEnumerable
    
        Public Property a() As Integer
        Private _l As List(Of Integer) = New List(Of Integer)
        Public Property l() As List(Of Integer)
            Get
                Return _l
            End Get
            Set(value As List(Of Integer))
                _l = value
            End Set
        End Property
    
        Public Sub New(ByVal a As Integer)
            Me.a = a
        End Sub
    
        Public Sub Add(ByVal item As Integer)
            _l.Add(item)
        End Sub
    
        Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
            Return _l.GetEnumerator()
        End Function
    End Class
    

    And it can be used as follows:

    Dim f As New Foo(1) From {1, 2, 3}   ' sets a to 1 and adds 1, 2 and 3 to the list
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following class: public class Foo { private List<Integer> list =
Suppose I have a function defined like this: class Foo() { public: void bar(MyClass*
Suppose you have a simple class like this: class foo{ private: int* mData; int
Suppose I have a class public class Foo { public Bar Bar { get;
Suppose I have class like this (simplified): class Foo_p; class Foo { private: Foo_p
Suppose I have some code like this: class Base { public: virtual int Foo(int)
Suppose I have this class: class foo { public: foo() { } foo(const std::string&
Suppose I have this class hierarchy: class A { public: virtual void foo(Base *b)
Suppose I have a class like this: class Foo { std::vector<int> bar; public: std::vector<int>&
Suppose I have this class: class MyClass(object): def uiFunc(self, MainWindow): self.attr1 = foo self.attr2

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.