Removal Of Enumerable Items From A Collection?

Oct 20, 2009

removal of items in a collection or list using the " For Each " statement to iterate through.This code works:

For Each item As ListViewItem In ListView1.SelectedItems
ListView1.Items.Remove(item)
Next

This code does not:

For Each itemChecked As Object In CheckedListBox1.CheckedItems
CheckedListBox1.Items.Remove(itemChecked)
Next

I have been reading on this for hours today to find out why the first code block works, but the second does not.What I've learned is the "For Each" statement implements the IEnumerable interface. I have seen these terms used many times and scratched my head when it didn't make scense, then just moved along, something to strain my brain about later.Today was later...So since the IEnumerable interface is used, the .GetEnumerator method of the IEnumerable interface is called upon at the beginning of the "For Each" block, this evaluates the collection or list as in: For Each item As ListViewItem In myCollection This is done at the beginning of the iteration through the myCollection, so if the myCollection is altered as in:

For Each itemChecked As Object In myCollection
CheckedListBox1.Items.Remove(itemChecked)
Next

The altering of the list returns the error:List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.

[code].....

View 9 Replies


ADVERTISEMENT

Accessing And Adding Items To A Collection That Is In A Collection?

Apr 6, 2012

currently in my application I have a Global Collection that is a Collection of Collections.Currently I am trying to add items/ elements to one of the child collections of the Global Collection but I am unable to use the collections methods of the child.When I assign a variable to the child collection and return the collection it is just an object with the collection inside.

Ex:
Dim GlobalCollection as New Collection
Dim ChildCollection1 as New Collection
Dim tempCurrentCollection[code]......

View 3 Replies

Using Regex Or Enumerable To Create A String?

Mar 13, 2011

Is there a way using regex or enumerable to create a string that looks like abbcccddddeeeee that continues through 26 z's ? In case that isn't clear the string has:

1 a
2 b's
3 c's
4 d's
5 e's
etc
26 z's

View 4 Replies

Write A Generic Extension Method For Every Enumerable(Of T)?

May 30, 2012

After looking at this thread.>>

How to randomize the alphabet I have decided to make it into an extension method for a String and a List.

Note: Please see the following post if you are still using VB.Net 2002, 2003 or 2005

this code is for use with 2008 and later versions of VB.Net

String.Mix and also List.Mix I was trying to write a generic extension method for every Enumerable(Of T)

I settled on just doing it for a STRING

[Code]...

View 16 Replies

.net - Can't Enumerate Through Items In Collection?

Apr 13, 2011

Aren't arrays of object enumerable?Look for the comments in the code

Public Class AddressCollection
Inherits System.Collections.ObjectModel.Collection(Of AddressType)
Public Sub New()
End Sub

[Code]...

View 3 Replies

How Come In .NET A Listcontrol Has No Items Collection But In ASP.NET It Does

Apr 11, 2012

I have a simple subroutine that loads a list from a database. I would like to be able to use the same code to load a ListBox and a ComboBox by defining the list type as the common abstract base class ListControl, and see no reason why I can't - except that VB.NET doesn't expose/implement/whatever the Items collection in ListControl. I note with frustration that this is not the case in ASP.NET. At moment my code is ugly because I have to check what type of list control I have passed in, in order to cast it to a type that has an Items collection. (My code may be ugly for numerous other reasons too, but it is beautiful to me). Is there a way to rewrite the code to avoid having to go through the testing and casting nonsense? (I've stripped it down somewhat so that all that remains is where the problem lies).

Sub loadList(ByVal db As SqlDatabase, ByVal strCommandText As String, lstHost As ListControl, Optional bClearList As Boolean = True, Optional bIsListBox As Boolean = True)
If bClearList Then

[code]....

View 1 Replies

IDE :: Sort Items In A Collection(of T)?

Mar 1, 2010

i am trying to sort the items in a collection(of T), by the start date. but i do not know how to do it. i do know that we have to implement list properties.

View 2 Replies

Validate Items Of A .NET Collection Against Each-other?

Dec 2, 2011

I have a class that represents some business object, such as this:

Public Class MyObject
Inherits SomeBaseObject ' Implements INotifyPropertyChanged and IDataErrorInfo
Private mMyVariable as Integer
Public Property MyVariable as Integer

[code]....

SomeBaseObject implements both INotifyPropertyChanged and IDataErrorInfo, such that a Validate method will populate the appropriate IDataErrorInfo fields when setting the property values.I also have a collection that supports change events, which I eventually bind to a WPF DataGrid.

Public Class MyCollection
Inherits ObservableCollection(Of MyObject)
End Class

Part 1: Let's say that I have a business rule which says that no two items in my collection may have the same value for MyVariable. How do I set up my events (or implement additional interfaces somewhere) to perform this cross-item validation whenever the property value changes on any item in the collection?

Part 2: Suppose I have a list of integers such that whenever I add a new MyObject item to an instance of MyCollection or change the MyVariable property on any item already in the collection, the value of the MyVariable property must exist in this list of integers?

View 1 Replies

Define A Collection In .NET So Can Reference Items Either By Value Or Name

Apr 18, 2011

I often have a class defined and I also define another class that is simply a collection of the other class. To do this I found it easiest just to define the other class as Inherits List(Of Type). The reason I define a collection class is to add extra functionality to the collection. Take the following code for example.

Class Car
Property Name As String
Property Year As Short

[Code]....

how can I define my own collection so that I can reference it by index or name like that. Right now I can only reference it by index.

View 5 Replies

VS 2008 Adding Items To A Collection?

Jun 25, 2011

I am trying to add the file information of the contents of a folder to a collection. Although I am getting the collection to hold the correct number of items that the folder contains, all of the collection's items are the details of the last file in the folder.

I'm trying to use a loop to go through each item contained in the folder and then adding the item details to a collection with the following line

nCounter += 1
collFileDetails.Add(NameOfItem, nCounter)
Next NameOfItem

View 3 Replies

VS 2008 Can't Loop Through Items In A Collection

Apr 15, 2010

CAUTION: Noob using Win32 API ahead

I want to display a list of open windows on my computer in a rich text box. So, I'm trying to store the names of all windows in myString, and then set RichTextBox1.Text = myString.

Here's the code

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

[Code].....

View 15 Replies

VS 2010 Group A Collection Of Items?

Jun 15, 2012

I am trying to work out a way to group a collection of items like an enum so that I can access them through code else where in my program. i.e I have list of items and sub items

[Code]...

View 13 Replies

Way To Group A Collection Of Items Like An Enum ?

Jun 15, 2012

I am trying to work out a way to group a collection of items like an enum so that I can access them through code else where in my program.

i.e I have list of items and sub items

CODE:

I would then like to be able to access then like: G1Level1.Leve2.Leve3

Would anyone be able to advise if this is possible or achievable.

View 2 Replies

.net - Execute A Command On A Collection Of Items, Functionally

Jul 12, 2011

I've done a little bit of LINQ but I almost exclusively return some elements from a collection based on some criteria. Now I'm trying to do something similar; let's say I have 50 winForm Controls in a collection - I want to set the .Visible property = True based on the controls name.

How would I do that? Below is my failed attempt.

myControls.AsEnumerable.Select( Function (myControl as Control) ( myControl.Visible = (myControl.Name <> "Hidden")) )

It compiles and even executes but does nothing.

View 1 Replies

Added To The Items Collection In Design Mode?

Nov 10, 2009

I have a ComboBox control with some items added to the items collection in design mode. What I want is when an item is picked from the ComboBox a different value is actually used in the code, instead of what is displayed. I am aware of the DisplayMember & ValueMember properties, but I'm not sure if that is what I need to use. All the examples I found were for bound data.

View 3 Replies

Get Collection Of Items Of ItemMouseHover Event In ListView?

Jun 16, 2009

I have a problem getting the collection of items in the ItemMouseHover event. I want to get the collection of all the items and subitems when the Mouse Hovers in the ListView. I have the property of Full Row select in the ListView.

View 4 Replies

Get The Collection Of Items In The ItemMouseHover Event In ListView?

Jun 16, 2009

i have a problem getting the collection of items in the ItemMouseHover event. I want to get the collection of all the items and subitems when the Mouse Hovers in the ListView. I have the property of Full Row select in the ListView.

View 2 Replies

How To Create Collection Class And Add List Of Items

Oct 13, 2009

How to create collection class and add list of items to collection class that stores list of items?

View 3 Replies

Items Collection Cannot Be Modified When DataSource Property Is Set

Apr 27, 2010

I am trying to refresh my combobox when an item is selected and added the highlighted code as shown below; [code]Items collection cannot be modified when the DataSource property is set.

View 3 Replies

Safely Modify A Collection's Items While Iterating Over It (using For Each)?

Sep 13, 2011

Can I safely modify -I mean: remove and re-add on a different index position- any item that I iterate over using a For Each loop in VB? We are talking about the VB Microsoft.VisualBasic.Collection class.And if yes: Is this by design, or an implementational detail, that I am building upon then?

View 3 Replies

Store Items In A Collection In String Array?

Sep 2, 2009

I have a string array called m_DirFileList and a collection called myFileCollection. I'm trying to loop through the items in the collection and store each one in the string array. It's not working though.

[code]...

View 2 Replies

Storing Items In A Collection In A String Array?

Sep 28, 2009

I have a string array called m_DirFileList and a collection called myFileCollection. I'm trying to loop through the items in the collection and store each one in the string arrayIt's not working though. This is what I'm trying to do:

Dim myFileCollection As Collection = clsFTPClientClass.GetFileList(fileFilter, True)
intItems = clsFTPClientClass.GetFileList(fileFilter, True).Count()

[code].....

View 2 Replies

Use LINQ To Query The Items Collection Of A ComboBox?

Jul 14, 2011

Is it possible to use LINQ to query the items collection of a ComboBox & get the index of the item that matches what I'm looking for?

View 4 Replies

VS 2005 Erasing Items In A Collection Using A Loop?

Jul 1, 2009

I often use a for/next loop to cycle through a collection to find a particular member of that collection based on some criteria. This works fine in every case except one: when I want to then erase the very member of the collection I just found. For example, if I wanted to give the heave-ho to every obect in a collection class that has a member veriable Color equal to Red:

[Code]...

The problem is once the collection class member is erased it makes to loop invalid because taking out the member reduces the total count of the collection in the middle of the loop. Eventually I get a "Index is out of range" error.This circumstance has bothered me every so often; I bet more experienced programmers have a different type of loop system that works better than this when you want to erase a collection member.

View 2 Replies

"Items Collection Cannot Be Modified When The DataSource Property Is Set?

Jun 17, 2010

I have a combobobox on my form, and I use the following code to populate the data from SQL Server:

cbname.DataSource = DSAssetName.Tables(0)
cbname.DisplayMember = "IDWLNAME"
cbname.ValueMember = "FIDWID"

[code].....

View 2 Replies

DNN Dal+ - Retrieve Individual Info Class Collection Items?

Dec 30, 2010

I can't seem to find any answers that work. Here's the setup:

Info class: Public Class ProductStageInfo

[Code]...

maybe I just don't understand the whole collection/index/thing. All the examples I've found are regarding single dimension collections - 'how to find name within this collection', and the responses use strings to search through them, but somehow the Info class is being treated differently, and I'm not sure how to translate this..

View 1 Replies

Remove Items From Microsoft.VisualBasic.Collection Where Key Contains String?

Jun 1, 2011

I have a Microsoft.VisualBasic.Collection where they keys are

"123,A"
"123,B"

Is there a way to remove all keys where they begin with 123?

View 1 Replies

Saving Items In A Collection To A Database Using A Loop Function?

May 12, 2011

I would like to be able to save to a db and send task to email or as text msg for instance nevermind that.. I have two variables of type ArrayList they are "_mList1" , "_mList2" and "_mList3"

_mList1 stores the list of Telephonenumbers , _mList2 stores the list of StaffIDs and _mList3 Some concatenated string.

theses variable lists will hold some data in runtime as explained and i would like to use them to populate a DB table again mentioned

tblTask (TaskID(PK), Action, StaffID(FK), AssignedBy, TelephoneContacted, NotesCotent)
tblStaff (StaffID(PK), FirstName, Surname, Telephone, Address etc..)

[Code]....

View 1 Replies

VS 2008 Event To Detect Changes In Items Collection In Listview

Jan 16, 2010

I'm trying to create an inherited version of the ListView control that (among other things) should have a different background image if the listview is empty to if it has items.Obviously you can easily enough test the count of the collection, but I need to be able to react to the event, however there doesn't appear to be any event related to adding/removing items.

View 3 Replies

VS 2008 Items Collection Cannot Be Modified When The DataSource Property Is Set

Feb 13, 2010

I have two tables "Person" and "Alias" in a one to many relationship. I have a DGV for "Person" and want to use a combobox for the Aliases. I also have a textbox and a button for the user to type the alias name and click the button to add the alias name to the combobox and update the dataset. I keep getting the "Items collection cannot be modified when the DataSource property is set." Error. But if i use a DGV instead of a combobox for aliases, I have no errors and everything works fine. If I remove the datasource property for the combobox it will not get populated with the data already entered, so how, or can I, set it up where the combobox gets populated when the form loads and able to add items to the combobox????

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved