|
-
Mar 26th, 2007, 05:30 PM
#1
Thread Starter
PowerPoster
[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,
-
Mar 26th, 2007, 05:41 PM
#2
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:
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.
-
Mar 26th, 2007, 05:48 PM
#3
Thread Starter
PowerPoster
Re: VB.Net 2005 and Fixed Length Variables???
Thanks JMC....I'll give that a try and see if it produces what I need!
-
Mar 26th, 2007, 06:10 PM
#4
Thread Starter
PowerPoster
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!
-
Mar 26th, 2007, 06:16 PM
#5
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.
-
Mar 27th, 2007, 09:16 AM
#6
Thread Starter
PowerPoster
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
-
Mar 27th, 2007, 11:17 AM
#7
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))
-
Mar 27th, 2007, 11:29 AM
#8
Thread Starter
PowerPoster
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,
-
Mar 27th, 2007, 02:36 PM
#9
Re: VB.Net 2005 and Fixed Length Variables???
 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.
-
Mar 27th, 2007, 05:23 PM
#10
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:
Public Structure BatchHeader
Private _recType As String
Private _recTypeWidth As UShort
Public Property RecType As String
Get
If Me._recType Is Nothing
Me._recType = String.Empty
End If
Return Me._recType.PadRight(CInt(Me._recTypeWidth))
End Get
Set(ByVal value As String)
Me._rectType = value
End Set
End Property
Public Property RecTypeWidth As UShort
Get
Return Me._recTypeWidth
End Get
Set(ByVal value As UShort)
Me._rectTypeWidth = value
End Set
End Property
'....
End Structure
-
Mar 28th, 2007, 09:11 AM
#11
Thread Starter
PowerPoster
Re: VB.Net 2005 and Fixed Length Variables???
To initialize, how about just
stcBatchHdr = Nothing
-
Mar 28th, 2007, 05:18 PM
#12
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|