Results 1 to 8 of 8

Thread: How to use ReDim in vb.net+ Sourabh Das

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    220

    How to use ReDim in vb.net+ Sourabh Das

    Hi,
    This code is in vb6. How do I use the redim keyword in vb.net.

    Code:
    Public Sub FillVendor()
    On Error GoTo err
    I = 0
    Set rs = New ADODB.Recordset
    rs.Open "select * from tbl_vendor where isdeleted=0", cn, 1, 1
    ReDim arrVendorId(rs.RecordCount)
    While Not rs.EOF = True
        cmbVendor.AddItem rs("Vendor_name")
        arrVendorId(I) = rs("Vendor_Id")
        I = I + 1
        rs.MoveNext
    Wend
    Exit Sub
    err:
        MsgBox err.Number & "- " & err.Description, vbInformation, err.Source
        Call I_Process_Errors("frmItem:FillVendor", err.Number, err.Description, err.Source)
    End Sub
    Regards,

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

    Re: How to use ReDim in vb.net+ Sourabh Das

    ReDim does in VB.NET exactly what it did in VB6. If you want to know how to use any key word, statement, property or method then just look it up in the MSDN library.

    http://search.msdn.microsoft.com/sea...=00&lang=en-us
    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

  3. #3
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: How to use ReDim in vb.net+ Sourabh Das

    vb Code:
    1. dim string() as string
    2.  
    3. dim counter = 6
    4. redim string(counter)
    5.  
    6. for i as integer = 0 to 6
    7.   string(i) = "whatever"
    8. next i

  4. #4
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: How to use ReDim in vb.net+ Sourabh Das

    Note that ReDim will reset any existing elements of the array to the default values. If you wish to keep the existing values then use the Preserve keyword.

    eg. ReDim Preserve sArray(20)

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    220

    Re: How to use ReDim in vb.net+ Sourabh Das

    Hi,
    I am unable to understand it.
    Please help me giving a small piece of code.
    Its urgent.
    Regards,

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    220

    Re: How to use ReDim in vb.net+ Sourabh Das

    Hi TalkRo,
    Why did you take counter as 6.
    actually this count should be the number of rows present in the table if i am not wrong..
    so instead of that should i write the count of records and if yes then
    what will be the change in the for loop.

  7. #7
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: How to use ReDim in vb.net+ Sourabh Das

    Code:
    'declare and allocate 2 elements to a string array 
    Dim sArray(1) As String
    
    'assign values to the 2 elements
    sArray(0) = "yes"
    sArray(1) = "no"
    
    'redimension the array allocating a third element
    ReDim Preserve sArray(2)
    
    'assign a value to the third elements
    sArray(2) = "hello"
    If you weren't to use the Preserve keyword above, the values "yes" and "no" would be lost and replaced with Nothing.

  8. #8
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: How to use ReDim in vb.net+ Sourabh Das

    vb Code:
    1. dim i as integer = 0
    2. dim str() as string
    3. dim counter as integer = ds.tables("yourTableName").Rows.count
    4. redim str(counter)
    5. for each row as datarow in ds.tables("yourTableName").Rows
    6.   string(i) = row("ID").toString()
    7.   i += 1
    8. next row

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