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


ADVERTISEMENT

Dereferencing Objects - Make CopiedCollection A True Copy (not A Reference) To The Listbox1.Items Collection?

Jun 30, 2010

Okay I've been wondering about this for a while now, but whenever you assign variable values to objects they tend to just become references to the original object

For example:

Dim copiedCollection As ListBox.ObjectCollection = ListBox1.Items
For Each L As String In copiedCollection
If L = "Goodbye" Then ListBox1.Items.Remove(L)
Next

As soon as it encounters the string "Goodbye" and removes the item from the listbox, the program will throw an error saying the the collection (copiedCollection) has been modified. How can I make copiedCollection a true copy (not a reference) to the Listbox1.Items collection?

View 14 Replies

Define A Variable That Is A Collection Of Interfaces?

May 6, 2010

I want to define a variable that is a collection of interfaces - it could be any interface that is defined for use in my app.

e.g. Dim x as New List(Of InterfaceBase)

Is there a base type for all interfaces that I can use for InterfaceBase?

View 1 Replies

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

Add Object To Collection (copy Not Reference)?

Mar 27, 2009

i have a user define object which i'm initializing with data from external datafiles.... the purpose of this object is to reduce the amount of disk/data access and processing required every time i need to add an instance of this object to a collection.so, whenever i add a copy of this object to a collection, i would simply reference the "base" object, and avoid having to process all the data and perform the conversion and so on...the issue is when i add the object

Collection.add(Base_Object, Key)

the collection ITEM is a Reference to the base object and not a copy.

View 6 Replies

Get A Reference To The Screens Collection In VB Express 8?

Mar 2, 2010

I've tried this example directly from MSDN:

Dim Screens() As System.Windows.Forms.Screens

and I can't find a way to get a reference to the Screen. I've checked my references and they seem fine but I may have missed something. Anyone experience this or know of a bug?

View 1 Replies

Readonly Reference To Parent/collection?

Feb 22, 2011

When it comes to creating trees or collections, sometimes I want child elements to have a reference to the parent. Obviously it is important that the parent and child references to each other remain in sync. It seems natural to have code in the parent "addchild" and "removechild" methods that updates the child's parent reference.t also, the reference to the parent should probably be readonly, so it can only be changed at the appropriate time.Two examples in .Net that I've seen are the TreeNode and datagridview controls

View 11 Replies

Reference ContextMenuStrip From Controls Collection?

Jan 6, 2010

I am writing a simple control translation function that reads an XML file containing control names and their localized text. Within the application, each time a form is loaded, it iterates through every control and tests to see if that control is defined in the XML file. If it is, it then sets the control text to whatever localized text is defined in the XML file.

There are some tests and additional requirements (i.e. nested loops) for TabControl and MenuStrip etc., but the control that is giving me the most trouble is the ContextMenuStrip.[code]...

View 7 Replies

Collection To Place These Objects In For Easy Reference?

Nov 11, 2011

I have an error while using a collection that has been stumping me for years.I wrote a class to allow users to write equations in a txt file using RPN (stack based notation).The class runs through a select statement while reading the equation and then ends up with a final number as the answer. I have a collection to place these objects in for easy reference. For whatever reason I get random object reference not set to an instance of an object errors when there is nothing in the program that would remove objects from the collection, kill objects, or change the collection in any way. It is possible for one thread to call the calculate function while another thread calls the CurrentValue property. I'm not sure if this could be causing the error, but once it happens I get an error every time the program enters that code until I run out of memory.

[Code]...

View 4 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

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

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







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