Results 1 to 6 of 6

Thread: [RESOLVED] Problem writing to a file using Fixed Array

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    5

    Resolved [RESOLVED] Problem writing to a file using Fixed Array

    Hi Everybody,

    I am trying to write a program that will use a Structure with <VBFixedString(4), VBFixedArray(10)> Public Test1() As String.
    Back in vb6 it was in the Type statement as Test1(10) as string *4
    When I hit the line ".Test1(X) = TB1" i get the following error:
    Object reference not set to an instance of an object.

    Below is the code, any help is greatly appriciated.

    Code:
    Option Strict Off
    Option Explicit On
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
        Structure FileFormat
            <VBFixedString(8)> Public Username As String
            <VBFixedString(4), VBFixedArray(10)> Public Test1() As String
            <VBFixedString(4), VBFixedArray(10)> Public Test2() As String
            <VBFixedString(4), VBFixedArray(10)> Public Test3() As String
            <VBFixedString(4), VBFixedArray(10)> Public Test4() As String
            <VBFixedString(4), VBFixedArray(10)> Public Test5() As String
        End Structure
    
    	Dim AllFileFormat As FileFormat
    	Dim Username As String
    	Dim TB1 As String
    	Dim TB2 As String
    	Dim TB3 As String
    	Dim TB4 As String
    	Dim TB5 As String
                 Dim FN As String
    	Dim FBuf As Short
    	Dim FLen As Integer
    	Dim Rec As Integer
                 Dim Num As Short
                 Dim X As Integer
    	
    	Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
            Rec = 1
    		Num = 1
            Username = "TEST"
    		TB1 = UCase(Trim(T1.Text))
    		TB2 = UCase(Trim(T2.Text))
    		TB3 = UCase(Trim(T3.Text))
    		TB4 = UCase(Trim(T4.Text))
    		TB5 = UCase(Trim(T5.Text))
    		
            FN = "D:\TestFixedArray.txt"
    		FBuf = FreeFile
    		FLen = Len(AllFileFormat)
    		
            FileOpen(FBuf, FN, OpenMode.Random, OpenAccess.Write, OpenShare.Shared, FLen)
            .Username = Username
            For X = 1 To 10
                With AllFileFormat
                    .Test1(X) = TB1
                    .Test2(X) = TB2
                    .Test3(X) = TB3
                    .Test4(X) = TB4
                    .Test5(X) = TB5
                End With
            Next X
            FilePut(FBuf, AllFileFormat, 1)
            FileClose(FBuf)
        End Sub
    End Class

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Problem writing to a file using Fixed Array

    When you loop X = 1 to 10, you are trying to assign the same text value to each element of the array in your structure. So if TB1 equals "TEST" then every element of the Test1 array is going to be the same value of "TEST" so I am not sure if that is what you are looking to do, it doesn't make much sense to me.

    The specific reason for your error is that your elements of your structure (test1, test2, etc.) you declared as arrays, so you need to redim them with an actual bounds before you can use them.

    Like

    ReDim AllFileFormat.Test1(9)

    Keep in mind in .NET all arrays start at index 0, You can't make an array that has a range of 1 to 10 like you could in VB6.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    5

    Re: Problem writing to a file using Fixed Array

    That was just an example as I wanted to keep the exact code private. I will give the 0 to 10 and reDim a shot an see if that works.

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Problem writing to a file using Fixed Array

    Just note if you redim your array with an upperbound of 10, it will contain 11 elements (since it starts at 0)

    If you only want 10 elements in your array redim it with a bounds of 9 so it will be 0 - 9 (10 elements)

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    5

    Re: Problem writing to a file using Fixed Array

    Alright, I did the ReDim AllFileFormat.Test1(10) and wnt For X = 0 to 10 and it worked.

    Thanks for all the help kleinma.

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Problem writing to a file using Fixed Array

    No problem. Please mark the thread resolved using the thread tools menu above, and welcome to VBForums.

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