Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Loading items into a collection class

Posted on 2016-08-31
3
Medium Priority
?
64 Views
Last Modified: 2016-09-13
I have a custom object, InvestmentPriceCollection, that is a collection of InvestmentPrice. In a Windows form, I have code that successfully creates a new InvestmentPriceCollection, and by iterating through a gridview, creates new instances of InvestmentPrice (Dim inv as New InvestmentPrice), appending each to InvestmentPriceCollection. This all works great. Naturally, I can't be content with that  :-)

Rather than storing this iteration routine (let's call it LoadFromGridview) at the form level, I'd like to make it more available across the application, and incorporate it into InvestmentPriceCollection. Here's where I run into a problem.

When I create my public function LoadFromGridview in InvestmentPriceCollection, and attempt to define a new InvestmentPrice (Dim inv as New InvestmentPrice), I received the following error:

'New' cannot be used on a type parameter that does not have a 'New' constraint.

I resolved this error by changing the collection class declaration from Public Class InvestmentPriceCollection(Of InvestmentPrice) to:

Public Class InvestmentPriceCollection(Of InvestmentPrice As {New})

But now I'm having problems assigning values to the properties of InvestmentPrice (assume dateparameter is a valid date).

Dim inv as New InvestmentPrice
inv.AsOfDate = dateparameter

The error states that 'AsOfDate' is not a member of 'InvestmentPrice' - but it is! And I've been able to refer to it in the aforementioned form code.

Am I going about this in the wrong way? Will I be able to include LoadFromGridview in the InvestmentPriceCollection object? Is there a better or different way to store it elsewhere in the app?
0
Comment
Question by:Bryant Farley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 34

Expert Comment

by:it_saige
ID: 41778378
It depends on your implementation of the collection class; e.g. -

Form1.vb -
Public Class Form1
	Private Sub OnLoad(sender As Object, e As EventArgs) Handles MyBase.Load
		If Prices Is Nothing Then
			Prices = New InvestmentPriceCollection(From i In Enumerable.Range(0, 20) Select New InvestmentPrice() With {.ID = i, .Name = String.Format("InvestmentPrice{0}", i), .Price = ((i + 0.2) * 2.6), .AsOfDate = DateTime.Now.AddDays(-(12 * i))})
		End If

		DataGridView1.DataSource = Prices
	End Sub
End Class

Module Globals
	Public Property Prices() As InvestmentPriceCollection
End Module

Class InvestmentPriceCollection
	Inherits List(Of InvestmentPrice)

	Sub New()
		MyBase.New()
	End Sub

	Sub New(capacity As Integer)
		MyBase.New(capacity)
	End Sub

	Sub New(collection As IEnumerable(Of InvestmentPrice))
		MyBase.New(collection)
	End Sub
End Class

Class InvestmentPrice
	Public Property ID() As Integer
	Public Property Name() As String
	Public Property Price() As Decimal
	Public Property AsOfDate() As DateTime
End Class

Open in new window

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.DataGridView1 = New System.Windows.Forms.DataGridView()
		CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).BeginInit()
		Me.SuspendLayout()
		'
		'DataGridView1
		'
		Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
		Me.DataGridView1.Dock = System.Windows.Forms.DockStyle.Fill
		Me.DataGridView1.Location = New System.Drawing.Point(0, 0)
		Me.DataGridView1.Name = "DataGridView1"
		Me.DataGridView1.Size = New System.Drawing.Size(470, 261)
		Me.DataGridView1.TabIndex = 0
		'
		'Form1
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(470, 261)
		Me.Controls.Add(Me.DataGridView1)
		Me.Name = "Form1"
		Me.Text = "Form1"
		CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).EndInit()
		Me.ResumeLayout(False)

	End Sub
	Friend WithEvents DataGridView1 As System.Windows.Forms.DataGridView

End Class

Open in new window

Produces the following output -Capture.JPG
-saige-
0
 

Accepted Solution

by:
Bryant Farley earned 0 total points
ID: 41789884
I created a helper class, and that seems to work.
0
 

Author Closing Comment

by:Bryant Farley
ID: 41795698
The user who answered didn't quite understand what I was asking, and in the meantime I'd created a helper class, which works fine.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question