|
-
Feb 6th, 2012, 06:53 AM
#1
Thread Starter
Addicted Member
.Net Framework 3.5
A change in the .net framework version has become necessary for my project and i'm running into some issues with changing a usercontrol. I narrowed done my code and got to just the bare minimum to make the error.
Code:
Public Class UserCTest
Private mMyCollection As Collection = New Collection
Public Property MyCollection As Collection
Get
Return mMyCollection
End Get
Set(ByVal value As Collection)
mMyCollection = value
End Set
End Property
End Class
As you can see i've got a collection property in the uc. Okay, so i run my project with a new UserCTest control and get two errors which read:
Code:
Could not find a type for a name. The type name was 'Microsoft.VisualBasic.Collection, Microsoft.VisualBasic, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f11d50a3a'. Line 2659, position 5.
And this which seems like it's being cause by the first one:
Code:
Unable to open file '...\Solution Folder\Project Folder\obj\x86\Debug\WindowsApplication1.Form1.resources': The system cannot find the file specified.
All the other properties in my uc are fine so it's just this one which causes an error. As you can see from the error message the problem is in the form resource file in the place where the collection data is stored. I did a test to see if there's a problem with any property that is saved in the .resx file like this
Code:
Public Class UserCTest
Private mMyImage As Image
Public Property MyImage As Image
Get
Return mMyImage
End Get
Set(ByVal value As Image)
mMyImage = value
End Set
End Property
End Class
It worked so as far as i know the problem is only with collections.
-
Feb 6th, 2012, 07:26 AM
#2
Re: .Net Framework 3.5
Never, never, NEVER use that Collection class. It is a hold-over from VB6 and exists really just to support upgraded VB6 code. It should never be used in new VB.NET code. If you want a property that is a collection type then it should either be a System.Collections.Generic.List(Of T) or your own class derived from the System.Collections.ObjectModel.Collection(Of T) class.
Also, such a collection property should never be read/write but rather always read-only. Can you think of any collection property in the .NET Framework that is read/write? Such a property should look like this:
vb.net Code:
Private _things As New List(Of Thing) Public ReadOnly Property Things() As List(Of Thing) Get Return _things End Get End Property
or, with lazy-loading:
vb.net Code:
Private _things As List(Of Thing) Public ReadOnly Property Things() As List(Of Thing) Get If _things Is Nothing Then _things = New List(Of Thing) End If Return _things End Get End Property
That's basically how every collection property in the .NET Framework is implemented, e.g. DataSet.Tables, DataTable.Rows, DataTable.Columns, Control.Controls, MenuStrip.Items, ToolStripMenuItem.DropDownItems, etc, etc.
-
Feb 8th, 2012, 03:43 AM
#3
Thread Starter
Addicted Member
Re: .Net Framework 3.5
Never, never, NEVER use that Collection class. It is a hold-over from VB6 and exists really just to support upgraded VB6 code. It should never be used in new VB.NET code.
Thanks, i'm glad to know that. I was using it because it was used in vb6. The reason why i was trying to use a collection is that i'd like to make my own imagelist which can hold large images with different sizes. Is there a way to get the normal image list to have large images?
I was managing with a collection in the 4.0 framework just fine. When you make a collection property it saves it in the resources and looks like this in the designer:
Code:
Me.UserControl11.MyCollection = CType(resources.GetObject("UserControl11.MyCollection"), Microsoft.VisualBasic.Collection)
But when i make a list of images property like this i see nothing in the form's designer saving the list. What would be the way of saving a list of images?
Code:
Private _things As New List(Of Image)
Public Property Things() As List(Of Image)
Get
Return _things
End Get
Set(ByVal value As List(Of Image))
_things = value
End Set
End Property
-
Feb 8th, 2012, 07:09 AM
#4
Re: .Net Framework 3.5
 Originally Posted by cheesebrother
What would be the way of saving a list of images?
You could save/load a list of bitmaps in a single file, see the top answer by JohnWein on MSDN forums here.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|