Question array is overwrite all elements everytime

dnyandeo_bhar

New member
Joined
Jan 16, 2009
Messages
1
Programming Experience
Beginner
Hi
I am basically C# developer and new to VB.Net. I am trying to write webservice using xsd.exe. I create class from xsd and try to use it. I am getting problem to get strucure as everytime values are getting ovewriiren so I am always getting last row in my xml structure. Following is code for my function and clss create from xsd.exe

Foolwong is my function



VB.NET:
Public Function getDataFromCSV1(ByVal mFileName As String, ByVal mFolder As String) As XMLSerializationVB.tests
    Dim ConnectionString, CommandText As String
    Dim conn As OleDb.OleDbConnection
    Dim Command As OleDbCommand
    Dim Count As Integer
    Dim strMsg As String
    ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mFolder & "\;Extended Properties='text;HDR=Yes'"
    'CommandText = "select * from Employee.txt"
    CommandText = "select * from " & mFileName
    conn = New System.Data.OleDb.OleDbConnection(ConnectionString)
    Command = New System.Data.OleDb.OleDbCommand(CommandText, conn)
    conn.Open()
    Dim da As OleDbDataAdapter = New OleDbDataAdapter(CommandText, conn)
    Dim ds As DataSet = New DataSet
    ' fill dataset
    da.Fill(ds, "Employees")
    Dim t2 As XMLSerializationVB.tests = New XMLSerializationVB.tests()
    Dim tt1 As XMLSerializationVB.test = New XMLSerializationVB.test()

    ' Follwoing code has problem it is ovewrwriing all values in array so all location its same value
    For Count = 0 To ds.Tables(0).Rows.Count - 1 Step +1
        'dim t2.mytest(count) as XMLSerializationVB.test = New XMLSerializationVB.test()
        ReDim Preserve t2.mytest(Count)
        If (t2.mytest(Count) Is Nothing) Then
            tt1.id = ds.Tables(0).Rows(Count).Item(0)
            tt1.FName = ds.Tables(0).Rows(Count).Item(1)
            tt1.LName = ds.Tables(0).Rows(Count).Item(2)
            t2.mytest(Count) = tt1
            t2.mytest(Count).id = ds.Tables(0).Rows(Count).Item(0)
            t2.mytest(Count).FName = ds.Tables(0).Rows(Count).Item(1)
            t2.mytest(Count).LName = ds.Tables(0).Rows(Count).Item(2)
        End If
    Next
    Return t2
End Function

VB.NET:
'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated by a tool.
' Runtime Version:2.0.50727.1433
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------
Option Strict Off
Option Explicit On
Imports System.Xml.Serialization
'
'This source code was auto-generated by xsd, Version=2.0.50727.42.
'
Namespace XMLSerializationVB
    '''<remarks/>
    <System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42"), _
    System.SerializableAttribute(), _
    System.Diagnostics.DebuggerStepThroughAttribute(), _
    System.ComponentModel.DesignerCategoryAttribute("code"), _
    System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="http://tempuri.org/XSDSchema1.xsd"), _
    System.Xml.Serialization.XmlRootAttribute([Namespace]:="http://tempuri.org/XSDSchema1.xsd", IsNullable:=False)> _
    Partial Public Class tests
        Private mytestField() As test
        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute("mytest")> _
        Public Property mytest() As test()
            Get
                Return Me.mytestField
            End Get
            Set(ByVal value As test())
                Me.mytestField = value
            End Set
        End Property
    End Class
    '''<remarks/>
    <System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42"), _
    System.SerializableAttribute(), _
    System.Diagnostics.DebuggerStepThroughAttribute(), _
    System.ComponentModel.DesignerCategoryAttribute("code"), _
    System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://tempuri.org/XSDSchema1.xsd")> _
    Partial Public Class test
        Private idField As String = "a"
        Private fNameField As String = "a"
        Private lNameField As String = "a"
        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute(DataType:="integer")> _
        Public Property id() As String
            Get
                Return Me.idField
            End Get
            Set(ByVal value As String)
                Me.idField = value
            End Set
        End Property
        '''<remarks/>
        Public Property FName() As String
            Get
                Return Me.fNameField
            End Get
            Set(ByVal value As String)
                Me.fNameField = value
            End Set
        End Property
        '''<remarks/>
        Public Property LName() As String
            Get
                Return Me.lNameField
            End Get
            Set(ByVal value As String)
                Me.lNameField = value
            End Set
        End Property
    End Class
End Namespace

Thanks for help in advance
Dnyandeo_bhar
 
Last edited by a moderator:
You have only created one XMLSerializationVB.test object, as referred to by tt1 variable. In your loop you merely change this same object all the times and places it to all array positions. Create new XMLSerializationVB.test objects in the loop.

If you are going to set the size of t2.mytest array same as row count, why don't you do this right away before the loop? Changing array size and preserving the content has a bad performance and memory impact, because new arrays are created for each resize and all elements have to be copied over. If you can't determine initial size you can still do this more economical, especially using a collection of some sort.

Take some more care with formatting next time you post. Your original post was about as bad as they get, not readable at all.
 
Back
Top