Setting MaxLength to <VBFixedString(x)>?

  • Thread starter Thread starter The Confessor
  • Start date Start date
T

The Confessor

I declare the following variable in one of my structures...

<VBFixedString(17)> Dim Name As String

Fixed to 17 because it's written to a random-access file.

Is there any way to tie the MaxLength of a TextBox to this value?

Thanks,
The Confessor
 
The Confessor said:
<VBFixedString(17)> Dim Name As String

Fixed to 17 because it's written to a random-access file.

Is there any way to tie the MaxLength of a TextBox to this value?

\\\
<VBFixedString(17)> Private X As String
..
..
..
Dim fi As FieldInfo = _
Me.GetType().GetField( _
"X", _
BindingFlags.Instance Or BindingFlags.NonPublic _
)
MsgBox( _
DirectCast( _
fi.GetCustomAttributes( _
GetType(VBFixedStringAttribute), _
False _
)(0), _
VBFixedStringAttribute _
).Length _
)
///
 
\\\
<VBFixedString(17)> Private X As String
.
.
.
Dim fi As FieldInfo = _
Me.GetType().GetField( _
"X", _
BindingFlags.Instance Or BindingFlags.NonPublic _
)
MsgBox( _
DirectCast( _
fi.GetCustomAttributes( _
GetType(VBFixedStringAttribute), _
False _
)(0), _
VBFixedStringAttribute _
).Length _
)
///

Thank you very much for your reply, Herfried.
 
I'm completely confused. What exactly are we trying to accomplish here?
Normally I can figure stuff out, but I'm drawing a blank on this one.
 
I'm completely confused. What exactly are we trying to accomplish here?
Normally I can figure stuff out, but I'm drawing a blank on this one.

Strings used in Random Access files require a <VBFixedString()> modifier
because each record in a RAF must be of equivalent length.

If your user inputs that data in a TextBox, there's a chance he'll proceed
beyond that allotted length, in which case the data will be trunecated...
so it's helpful to set the TextBox's MaxLength = to the number of
characters the string is restricted to...

Unfortunately, this results in repetition of effort: a change must be
reflected in *both* places... which can lead to discrepencies.

It's much easier to find a way to set TextBox's MaxLength = to the
fixedstring length, and only worry about modifying *one* line of code to
make such a change.
 
Ok, I get it. But if the string is always 17 and the TextBox.Length is
always 17, it seems like 2 lines of code would be easier than the
multi-lined code earlier in this thread:

<VBFixedString(17)> Dim X as String
TextBox1.MaxLength=17
 
Terry said:
Ok, I get it. But if the string is always 17 and the TextBox.Length is
always 17, it seems like 2 lines of code would be easier than the
multi-lined code earlier in this thread:

<VBFixedString(17)> Dim X as String
TextBox1.MaxLength=17

Or...

Public Const STRING_LENGTH As Integer = 17

<VBFixedString (STRING_LENGTH)> Dim X As String
TextBox1.MaxLength = STRING_LENGTH

Now, you only have to change the constant...
 
Touche' :-)

Tom Shelton said:
Or...

Public Const STRING_LENGTH As Integer = 17

<VBFixedString (STRING_LENGTH)> Dim X As String
TextBox1.MaxLength = STRING_LENGTH

Now, you only have to change the constant...
 
The said:
I declare the following variable in one of my structures...

<VBFixedString(17)> Dim Name As String

Fixed to 17 because it's written to a random-access file.

Might there be a platform issue using that? Could it be a Unicode string on
NT platforms but ANSI on W98?[1] And how does the requirement for a fixed
number of bytes in a file fight with the potentially variable number of
bytes of a UTF-8-encoded string?

[1]
http://msdn.microsoft.com/library/default.asp?url=/archive/en-us/dnaraskdr/html/askgui06042002.asp
Just after the third grey code block: "The VBFixedString attribute tells the
runtime that we will be using a fixed-length string that is 128 characters
long. The MarshalAs attribute lets the runtime know how to handle this
string at run time. What we are telling the compiler is that this string
will be 128 characters long (SizeConst:=128) and that the string should be
treated as a character set based on the platform (ByValTStr)."

Andrew
 
Or...

Public Const STRING_LENGTH As Integer = 17

<VBFixedString (STRING_LENGTH)> Dim X As String
TextBox1.MaxLength = STRING_LENGTH

Now, you only have to change the constant...

Which does what I want far more simply than the previous code!

Thank you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top