Results 1 to 7 of 7

Thread: [RESOLVED] confused now ...

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    63

    Resolved [RESOLVED] confused now ...



    Hi, this code below adds records to the collection as commented by the developer (not me), but how come not all fields are saved in the array but then those fields which are not saved can be retrieved???

    Please help, see comments with " This is adding to collection base ..."


    Code:
    Public Class NewMsgText
    
        Public Txt As String
        Public HtmTxt As String
    
    End Class
    
    
    Public Class RecordDefinition
    
        Public WarnCode As String = ""
        Public WarnDesc As String = ""
        Public WarnType As String = ""
        Public NewHdr As New NewMsgText()
        Public NewFtr As New NewMsgText()
        
    End Class
    
    
    <Serializable()> _
    Public Class WarningAdvisor
        Inherits CollectionBase
        
        Public Sub New()
        End Sub
        
        Public Sub Add(ByVal _newItem As RecordDefinition)
            list.Add(_newItem)
        End Sub
    
        Public Function Add(ByVal xCode As String, ByVal xDescr As String, ByVal xType As String) As RecordDefinition
    
            Dim _thisItem As New RecordDefinition()
            With _thisItem
                .WarnCode = xCode
                .WarnDesc = xDescr
                .WarnType = xType
            End With
            Add(_thisItem)
            Return _thisItem
    
        End Function
        
        <XmlElement(ElementName:="Items")> _
        Default Public Overridable ReadOnly Property Item(ByVal index As Integer) As RecordDefinition
            Get
                Return CType(Me.List(index), RecordDefinition)
            End Get
        End Property
    
    End Class
    
    Public Class Project Class
    
    Public oWarning As New WarningAdvisor()   
    
            Dim dsCode As String
            Dim dsDescr As String
            Dim dsHeadText As String
            Dim dsHeadHTML As String
            Dim dsFootText As String
            Dim dsFootHTML As String
            Dim dsType As String
    
    Dim oReader As SqlDataReader
    Dim myRecords As RecordDefinition()
    
    oReader = oCommand.ExecuteReader()
    
    
              ‘oReader HAVE 10 ROWS WITH 7 COLUMNS here
                While oReader.Read()
                    dsCode = "" & oReader("Code")
                    dsDescr = "" & oReader("Descrip")
                    dsType = "" & oReader("Type")
                    dsHeadText = "" & oReader("TextHead")
                    dsHeadHTML = "" & oReader("HTMLHead")
                    dsFootText = "" & oReader("TextFoot")
                    dsFootHTML = "" & oReader("HTMLfoot")
                   
                    
                    ‘THIS IS ADDING TO COLLECTION BASE, BUT WHY ONLY 3 FIELDS???
    
                    myRecords = oWarning.Add(dsCode, dsDescr, dsType)
    
                    With myRecords.NewHdr
                        .Txt = dsHeadText
                        .HtmTxt = dsHeadHTML
                    End With
                    With myRecords.NewFtr
                        .Txt = dsFootText
                        .HtmTxt = dsFootHTML
                    End With
                   
                 End While
    End Class

  2. #2

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    63

    Re: confused now ...

    does "myRecords" object holds the reference/pointer value to the 10 rows with 7 columns? (like a multidimensional array???)

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: confused now ...

    When you create a RecordDefinition object those last two fields are implicitly created as well.
    Code:
    Public Class RecordDefinition
    
        Public WarnCode As String = ""
        Public WarnDesc As String = ""
        Public WarnType As String = ""
        Public NewHdr As New NewMsgText()
        Public NewFtr As New NewMsgText()
        
    End Class
    A New RecordDefinition automatically contains those two New NewMsgText objects.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    63

    Re: confused now ...

    yes, but in the collections list, how come the "add" method only adds 3 fields and not all of them, and yet when the program accesses the object "myRecords" it can display those extra 4 fields as well?

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    63

    Re: confused now ...

    Quote Originally Posted by layne



    ‘oReader HAVE 10 ROWS WITH 7 COLUMNS here
    While oReader.Read()
    dsCode = "" & oReader("Code")
    dsDescr = "" & oReader("Descrip")
    dsType = "" & oReader("Type")
    dsHeadText = "" & oReader("TextHead")
    dsHeadHTML = "" & oReader("HTMLHead")
    dsFootText = "" & oReader("TextFoot")
    dsFootHTML = "" & oReader("HTMLfoot")


    ‘THIS IS ADDING TO COLLECTION BASE, BUT WHY ONLY 3 FIELDS???

    myRecords = oWarning.Add(dsCode, dsDescr, dsType)

    With myRecords.NewHdr
    .Txt = dsHeadText
    .HtmTxt = dsHeadHTML
    End With
    With myRecords.NewFtr
    .Txt = dsFootText
    .HtmTxt = dsFootHTML
    End With

    End While
    End Class[/CODE]
    the program adds to collection, but the remaining 4 fields are being overwritten by the "with - end with" blocks, so how come the object "myRecords" seem to have the rows???

    thanks

  6. #6
    Member Daarklord's Avatar
    Join Date
    Jul 2008
    Posts
    54

    Re: confused now ...

    oWarning.Add() initializes myRecords and sets the value of the first three variables.

    After that, the "with - end with" blocks are setting values to myRecords.NewHdr.Txt etc. by direct assignment.

    So now you have a RecordDefinition instance called myRecords with 7 variables that have been assigned some values. What's the question? What do you mean by "rows"?

    Edit: To be more specific, myRecords is being created in the above way then added to the instance of WarningAdvisor called oWarning each step of the while loop. If oReader has 10 "rows" and one row is passed every time its Read() function is called, then you have an object of type WarningAdvisor called oWarning that contains 10 objects of type RecordDefinition, each of which has 7 variables that now have values which were contained in the "collumns" of oReader.

    Hope that made sense.
    Last edited by Daarklord; Jul 18th, 2008 at 06:23 AM.
    0xABADA55D00D

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    63

    Re: confused now ...

    geez, thanks for that Daarklord, I never noticed the:

    Public Function Add(ByVal xCode As String, ByVal xDescr As String, ByVal xType As String) As RecordDefinition

    Dim _thisItem As New RecordDefinition()

    that it creates a NEW object then add to collection. I hope I can figure the rest.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width