Results 1 to 12 of 12

Thread: [RESOLVED] VB.Net 2005 and Fixed Length Variables???

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Resolved [RESOLVED] VB.Net 2005 and Fixed Length Variables???

    Can VB.Net 2005 assign fixed lengths to string variables?

    Ex:

    Dim strVar as string * 25

    I know in VB6 you can do this. If this can't be done....what is the best way to create a text file with fixed length fields?

    Thanks,
    Blake

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

    Re: VB.Net 2005 and Fixed Length Variables???

    No it can't. You can apply the VBFixedString attribute to a String field, but that does not make the managed string a fixed length. That makes it appear as though it is a fixed length when passed to unmanaged code, so it is only used in instances where an API function requires a structure with fixed length fields.

    If you want to create fixed-width column sin a text file then you should use the String.PadRight method. For instance, if you want a column of data in a text file that is 20 characters wide then do this:
    vb Code:
    1. myStreamWriter.Write(myString.PadRight(20))
    That will return a string that is 20 characters wide and write it to the stream. If your original string is less than 20 characters long it will be padded with enough spaces to make it 20 characters wide.
    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

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: VB.Net 2005 and Fixed Length Variables???

    Thanks JMC....I'll give that a try and see if it produces what I need!
    Blake

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: VB.Net 2005 and Fixed Length Variables???

    JMC,

    I try this and I get an error. The way I'm doing it isn't exactly as you have it. Here is my code:

    Code:
        Private Sub WriteOutputFile(ByVal recType As String)
            Try
                Dim strDetailLine As String = ""
    
                Select Case recType
                    Case "POSBH"
                        strDetailLine = stcBatchHdr.recType.PadRight(5) & _
                                        stcBatchHdr.merchBatchLvlDiscData.PadRight(25) & _
                                        stcBatchHdr.locationCD.PadRight(14) & _
                                        stcBatchHdr.merchBatchID.PadRight(10) & _
                                        stcBatchHdr.submissionDate.PadRight(6) & _
                                        stcBatchHdr.submissionTime.PadRight(6) & _
                                        stcBatchHdr.creationDate.PadRight(6) & _
                                        stcBatchHdr.filler.PadRight(628)
                        W1.WriteLine(strDetailLine)
                    Case "POSTH"
                        W1.WriteLine(stcTransHdr)
                    Case "MOFUL"
                        W1.WriteLine(stcMobile)
                    Case "POSTD"
                        W1.WriteLine(stcProduct)
                    Case "POSTT"
                        W1.WriteLine(stcTransTrailer)
                    Case "POSBT"
                        W1.WriteLine(stcBatchTrailer)
                End Select
    
            Catch ex As Exception
                strProcedure = "WriteOutputFile()"
                Msg = ex.Message
                ErrorHandler(strModule, strProcedure, Msg)
            End Try
        End Sub
    When I run this, I get the error:

    "Ojbect reference not set to an instance of an object"

    on the highlighted code!
    Blake

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

    Re: VB.Net 2005 and Fixed Length Variables???

    And what reference is Nothing? Use the debugger. That issue is nothing to do with PadRight specifically. It means that you're trying to call a method on a member that is returning Nothing. It's up to you to make sure that your references are not Nothing before attempting to use them.
    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

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: VB.Net 2005 and Fixed Length Variables???

    If I leave the statement just as it is except remove the PadRight parameters...it works fine!
    Code:
        Private Sub WriteOutputFile(ByVal recType As String)
            Try
                Dim strDetailLine As String = ""
    
                Select Case recType
                    Case "POSBH"
                        strDetailLine = stcBatchHdr.recType & _
                                        stcBatchHdr.merchBatchLvlDiscData & _
                                        stcBatchHdr.locationCD & _
                                        stcBatchHdr.merchBatchID & _
                                        stcBatchHdr.submissionDate & _
                                        stcBatchHdr.submissionTime & _
                                        stcBatchHdr.creationDate & _
                                        stcBatchHdr.filler                    
                        W1.WriteLine(strDetailLine)
                    Case "POSTH"
                        W1.WriteLine(stcTransHdr)
                    Case "MOFUL"
                        W1.WriteLine(stcMobile)
                    Case "POSTD"
                        W1.WriteLine(stcProduct)
                    Case "POSTT"
                        W1.WriteLine(stcTransTrailer)
                    Case "POSBT"
                        W1.WriteLine(stcBatchTrailer)
                End Select
    
            Catch ex As Exception
                strProcedure = "WriteOutputFile()"
                Msg = ex.Message
                ErrorHandler(strModule, strProcedure, Msg)
            End Try
        End Sub
    Blake

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: VB.Net 2005 and Fixed Length Variables???

    What are the types of these?
    Code:
    stcBatchHdr.recType
    stcBatchHdr.merchBatchLvlDiscData
    stcBatchHdr.locationCD
    stcBatchHdr.merchBatchID
    stcBatchHdr.submissionDate
    stcBatchHdr.submissionTime
    stcBatchHdr.creationDate                                    
    stcBatchHdr.filler
    If they are string then you should initialize them propriately when declared such as in this example
    Code:
    'Declare a string without initialize it will throw null exception
    Dim s As String
    MsgBox(s.PadRight(10))  's is Nothing, thus when you try to do PadRight it'll throw a null exception on this line
    
    'The code above can be fixed by properly initialize the variable
    Dim s As String = String.Empty 
    MsgBox(s.PadRight(10))

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: VB.Net 2005 and Fixed Length Variables???

    Stanav,

    I like to use Structures. Here is the definition!

    Code:
        Public Structure BatchHeader
            Dim recType As String
            Dim merchBatchLvlDiscData As String
            Dim locationCD As String
            Dim merchBatchID As String
            Dim submissionDate As String
            Dim submissionTime As String
            Dim creationDate As String
            Dim filler As String
        End Structure
        Public stcBatchHdr As BatchHeader
    Is there anyway of writing a Function where I can pass the field/variable, it's desired length, and return a value with the appropriate width?

    Thanks,
    Blake

  9. #9
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: VB.Net 2005 and Fixed Length Variables???

    Quote Originally Posted by blakemckenna
    Stanav,

    I like to use Structures. Here is the definition!

    Code:
        Public Structure BatchHeader
            Dim recType As String
            Dim merchBatchLvlDiscData As String
            Dim locationCD As String
            Dim merchBatchID As String
            Dim submissionDate As String
            Dim submissionTime As String
            Dim creationDate As String
            Dim filler As String
        End Structure
        Public stcBatchHdr As BatchHeader
    Is there anyway of writing a Function where I can pass the field/variable, it's desired length, and return a value with the appropriate width?

    Thanks,
    Using a structure is OK, you just have to properly initialize it.
    Since you've declared a variable type BatchHeader, just make sure to initialize it before using. .. So somewhere in the code, you just do this then it should work.
    Code:
    stcBatchHdr.recType = String.Empty
    stcBatchHdr.merchBatchLvlDiscData = String.Empty
    stcBatchHdr.locationCD = String.Empty
    stcBatchHdr.merchBatchID = String.Empty
    stcBatchHdr.submissionDate = String.Empty
    stcBatchHdr.submissionTime = String.Empty
    stcBatchHdr.creationDate = String.Empty                                    
    stcBatchHdr.filler = String.Empty
    Alternatively, you can make BatchHeader a class instead of structure and initialize all variables in the Sub New of the class.

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

    Re: VB.Net 2005 and Fixed Length Variables???

    If you were to use properties instead of fields then this would all be easy. That's what properties are for. E.g.
    vb Code:
    1. Public Structure BatchHeader
    2.  
    3.     Private _recType As String
    4.     Private _recTypeWidth As UShort
    5.  
    6.     Public Property RecType As String
    7.         Get
    8.             If Me._recType Is Nothing
    9.                 Me._recType = String.Empty
    10.             End If
    11.  
    12.             Return Me._recType.PadRight(CInt(Me._recTypeWidth))
    13.         End Get
    14.         Set(ByVal value As String)
    15.             Me._rectType = value
    16.         End Set
    17.     End Property
    18.  
    19.     Public Property RecTypeWidth As UShort
    20.         Get
    21.             Return Me._recTypeWidth
    22.         End Get
    23.         Set(ByVal value As UShort)
    24.             Me._rectTypeWidth = value
    25.         End Set
    26.     End Property
    27.  
    28.     '....
    29.  
    30. End Structure
    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

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: VB.Net 2005 and Fixed Length Variables???

    To initialize, how about just

    stcBatchHdr = Nothing
    Blake

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

    Re: VB.Net 2005 and Fixed Length Variables???

    That doesn't help because the problem is that your String fields ARE Nothing by default. Unless you initialise them with a String object then those fields refer to no object and you cannot call the PadRight method of an object that doesn't exist. If your type was a class instead of a structure then you could initialise the String fields to an empty string on the line that declares them. That's not possible with a structure.
    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

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