4 Replies - 1115 Views - Last Post: 16 November 2012 - 04:53 AM Rate Topic: -----

#1 Bladewing51   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 79
  • Joined: 26-August 11

How to impliment and use custom classes Generic List(Of myclass)

Posted 15 November 2012 - 12:37 PM

Hi everyone,
I currently am trying to use a custom class to make a generic list capable to holding multiple rows and columns of data. Example [BarcodeL, DescL, PriceL]. I've been following the few tutorials on this I can find but none so far seem to work and I just get the feeling that I'm not doing this right.

My custom class
Imports System
Imports System.Collections.Generic
Imports System.Text

Public Class genlist

    Private _barcodeL As String

    Public Property barcodeL As String
        Get
            Return _barcodeL
        End Get
        Set(ByVal value As String)
            _barcodeL = value
        End Set
    End Property

    Private _priceL As Decimal

    Public Property priceL As Decimal
        Get
            Return _priceL
        End Get
        Set(ByVal value As Decimal)
            _priceL = value
        End Set
    End Property

    Private _descL As String

    Public Property descL As String
        Get
            Return _descL
        End Get
        Set(ByVal value As String)
            _descL = value
        End Set
    End Property

    Public Sub New()

    End Sub

    Public Sub New(ByVal name As String)
        Me.barcodeL = String.Empty
        Me.priceL = 0.0
        Me.descL = String.Empty
    End Sub

    Public Sub New(ByVal barcode As String, ByVal price As Decimal, ByVal desc As String)
        Me.barcodeL = barcode
        Me.priceL = price
        Me.descL = desc
    End Sub
End Class




Imports System.Data.SqlClient
Imports System.Data
Imports System.Windows
Imports Barcodeapp.genlist

Class populate
    Public Sub senbarlist()

        Dim classg As New genlist

        Dim list As New List(Of genlist)

        Dim datarowview As DataRowView
        For Each DataRowView In lv2.Items

            list.Add( _
            genlist.descL = datarowview.Item("ItemName").ToString)
        Next
end sub
end class




To be honest I would be more than happy to start over with this if someone was able to point me in the right direction.

Is This A Good Question/Topic? 0
  • +

Replies To: How to impliment and use custom classes Generic List(Of myclass)

#2 sela007   User is offline

  • D.I.C Addict

Reputation: 139
  • View blog
  • Posts: 841
  • Joined: 21-December 11

Re: How to impliment and use custom classes Generic List(Of myclass)

Posted 15 November 2012 - 02:15 PM

everything is fine until line 16. You have the list of "genlist" and you want to add the new "genlist" to the list. All you need is to create new instance of the class "genlist", set barcodeL,priceL and descL and then add to the list. You have already create a new instance of the "genlist" in line 9 (classg) but it's never used. You need to create new instance for each row in lv2.
Was This Post Helpful? 1
  • +
  • -

#3 Bladewing51   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 79
  • Joined: 26-August 11

Re: How to impliment and use custom classes Generic List(Of myclass)

Posted 15 November 2012 - 02:43 PM

OK thanks, I will try this when I get back to the office, fingers crossed.
Was This Post Helpful? 0
  • +
  • -

#4 LilGhost   User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 98
  • Joined: 12-October 12

Re: How to impliment and use custom classes Generic List(Of myclass)

Posted 15 November 2012 - 05:59 PM

To put into code what sela007 is saying, and someone correct me if i'm wrong, your code should look like this:
Imports System.Data.SqlClient
Imports System.Data
Imports System.Windows
Imports Barcodeapp.genlist
 
Class populate
    Public Sub senbarlist()
 
        Dim classg As New genlist
 
        Dim list As New List(Of classg)
 
        Dim datarowview As DataRowView
        For Each DataRowView In lv2.Items
            list.Add( _
            classg.descL = datarowview.Item("ItemName").ToString)
        Next
end sub
end class


to explain this for people, not just the thread poster but anyone who's wondering, this is generally what's going on; Genlist is a class which is declared elsewhere. Genlist has some basic properties (such as barcodeL). When we create a new instance of this class (classg in this program) we're giving the new instance the basic variables that are in the class (classg now has the variable barcodeL which can be accessed via saying: classg.barcodeL). This is what we call an object. Classg when set = to a new instance of genlist (our class) makes classg an object. Objects have variables and different aspects to them. They can preform functions, store variables and have other characteristics. An object can have the values in it manipulated via the program invoking the object and saying ObjectName.VariableInObject = Value (ie. Classg.barcodeL = "potato" (assuming barcodeL is a public member of the class)). And that's basically what's going on in a nutshell. More experienced members, correct me if i'm wrong please.

This post has been edited by LilGhost: 15 November 2012 - 06:07 PM

Was This Post Helpful? 0
  • +
  • -

#5 lucky3   User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 233
  • View blog
  • Posts: 770
  • Joined: 19-October 11

Re: How to impliment and use custom classes Generic List(Of myclass)

Posted 16 November 2012 - 04:53 AM

LilGhost your solution is wrong, and is not doing what sela007 is saying in the last sentence:

Quote

You need to create new instance for each row in lv2.


You'd add the same object's information to the list of classg?!?! as many times, as there are rows in lv2.

And trying to do the work for someone else here, is not the kind of help, this site is about. Helping someone here is all about understanding, learning new things,... not doing the work for them.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1