?
Solved

Loading items into a collection class

Posted on 2016-08-31
3
Medium Priority
?
69 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
  • 2
3 Comments
 
LVL 36

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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Through the video, you can check the migration process of Outlook PST file to PDF. Kernel for Outlook to PDF tool can convert Outlook emails with all attributes like Subject, To, From, Cc, Bcc and other folders such as Inbox, Outbox, Sent Items, Jun…
Did you know PowerShell can save you time with SaaS platforms? Simply leverage RESTfulAPIs to build your own PowerShell modules. These will kill repetitive tickets and tabs, using the command Invoke-RestMethod. Tune into this webinar to learn how…

569 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