• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

Array Objects and datasets...how to fill one with the other?

Hi all,
Hopefully I'll ask this correctly.  Here goes...

In vb.net, I've created an "email" object in a class by itself.   Call it email.vb.   I want to be able to use this object as an array, holding multiple "email addresses".  Specifically, I'm wanting to be able to fill this "email address array" with the rows from a populated dataset containing the same field counts and (and data types...strings, in this case).

1) Can I fill this email object with the rows of the dataset without iterating through the dataset row by row?   How?
2) Conversely, can I fill a dataset with a populated email object array in a similar manner?  How?


Below, I've pasted in my code for my "email" object for mutual reference.  If my email object is not 'built' right to be used as an array, please show me the err of my ways.

Your input is appreciated!!!
Terry


Public Class Email
    Private _email_address As String
    Private _email_name As String
    Public Property EmailAddress() As String
        Get
            Return _email_address
        End Get
        Set(ByVal Value As String)
            _email_address = Value
        End Set
    End Property
    Public Property EmailUserName() As String
        Get
            Return _email_name
        End Get
        Set(ByVal Value As String)
            _email_name = Value
        End Set
    End Property
End Class
0
colepc
Asked:
colepc
  • 5
  • 2
1 Solution
 
123654789987Commented:
U should create a collection object like EmailList to create a collection of Email object. U can use a ArrayList as a collection.

Dim emailObject as new Email

emailObject.EmailAddress = "abc@com"
emailObject.EmailuserName = "abc"
Dim emailList as new ArrayList.
emailList.Insert(emailObject)
0
 
colepcAuthor Commented:
Cool.  How could I pass that to a dataset (and a dataset to the emaillist)?
0
 
GlomCommented:
Hi,
For this purpose, I actually use one method that you will have to write in your Email class :

Public Sub Fill(ByRef DR as DataRow)
    Me.EmailAddress = DR("Address") ' Address is the name of the field in the database.
    Me.EmailUserName = DR("UserName")
    ...
End Sub

and then call it for each row of your DataSet :

Dim DR as DataRow
Dim DS as DataSet ' Your DataSet
Dim emailObject as Email

For Each DR in DS.Tables(TABLE_NAME).Rows
    emailObject = New Email()
    emailObject.Fill(DR)
Next
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
colepcAuthor Commented:
Glom, when interating through "for each dr in ds.tables...", will each iteration add another 'item' to the emailobject?  In other words, I'm assuming that the emailobject's upper bound will grow by 1 on each iteration.  Am I misreading that?
0
 
colepcAuthor Commented:
I just read my goofy comment above.  Please ignore that.
0
 
colepcAuthor Commented:
How would I fill the email object with multiple "records" (consisting each of name, and address) from a dataset?
0
 
GlomCommented:
You cannot simply fill one email object with the entire dataset. You would have to create an email object collection class.

Public Class EmailCollection
Inherits CollectionBase
   
    Default Public Property Item(ByVal index As Integer) As Email
        Get
            Return CType(List(index), Email)
        End Get
        Set(ByVal Value As Email)
            List(index) = Value
        End Set
    End Property

    Public Function Add(ByVal value As Email) As Integer
        Return List.Add(value)
    End Function 'Add


    ' ... And the other methods you need ...
    ' Also :
    Public Sub Fill(Byref DS as DataSet)
        Dim DR as DataRow
        Dim newEmail as Email

        For Each DR in DS.Tables(TABLE_NAME).Rows
            newEmail = new Email()
            newEmail.Fill(DR)
            Me.Add(newEmail)
        Next
    End Sub
End Class

Then after calling the Fill function in your EmailCollection object, you will have the entire content of your dataset, but in object representation.

I hope this will fit what you need.

Glom
0
 
colepcAuthor Commented:
Glom,
thanks for your input, however, I'm afraid I still don't get it.  I've got more homework to do, it appears.  I'll refer back to your examples after I've understood abit more about .net.  Thanks again!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now