Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fixed Length String Value

Status
Not open for further replies.

Maii

Programmer
Aug 28, 2003
54
BD
Hi there,
What is the initial value of the following string?
Dim strTemp as String * 10


Thanks
maii
 
It has no value, it is simply reserved as a fixed-length string of 10 characters. See this sample. It displays nothing if you don't use the second line

Code:
Option Explicit

Private Sub Form_Load()
  Dim a As String * 10
  a = "a"
  MsgBox (a) & "C"
End Sub

-David
2006 Microsoft Valueable Professional (MVP)
 
But how can i validate it

Private Sub Form_Load()
Dim a As String * 10
If a = vbNullString Then
MsgBox "Not Assign"
Else
MsgBox "Assign"
End If
End Sub

Thanks
maii
 
Private Sub Form_Load()
Dim a As String * 10
If len(a) = 0 Then
MsgBox "Not Assign"
Else
MsgBox "Assign"
End If
End Sub
 
The code shows assign, but should be not assign
 
In answer to the original question "What is the initial value of the following string?
Dim strTemp as String * 10"

It is a string containing 10 * CHR(0)
 

Dim a as string * 10

Print len(a) 'will give 10

The string will contain 10 ascii zero characters, chr$(0)

If a = string$(len(a),0) then
'its appears to be unassigned
else
'.........
end if

Hugh
 
Oh.. of course, the len will always be 10. Here is a trick:

dim a as string * 10
If InStr(a, " ") = 0 Then
MsgBox "Not Assign"
Else
MsgBox "Assign"
End If


** Note that " " is not space!
a = "123456789" ' 9 characters
Text1 = a

Select the last (it is like space), copy it and paste it in the Instr function.


Hope that helps!
 
I did some tinkering around, and here are my conclusions.

Fixed string variables are first initialized as null characters (chr$(0)).

When you assign a value to the variable, unassigned characters in the string become spaces (chr$(32)).

Therefore[tt]
Mid(myVar, 1, 1) = chr$(0)
[/tt]
will always be true if the variable has never been assigned, and always false if it has.

Note that if the variable is explicitly assigned the value of "", the result will be all spaces, not all nulls. This creates the following interesting phenomenon:
[tt](from the debug window, with x declared as string * 10)
x = ""
? x = ""
False
[/tt]However:
[tt]? x = space(10)
True
[/tt]
So, if you want to treat a reassignment to a blank string as unassigned, you can evaluate[tt]
Mid(myVar, 1, 1) = chr$(32)
[/tt]for true or false.

Tipgiver, your trick won't work as stated if the assigned value is the full length of the string:
[tt](from the debug window, with x declared as string * 10)
x = "HelloHello"
? instr(x, " ") = 0
True
[/tt]

HTH

Bob
 
<So, if you want to treat a reassignment to a blank string as unassigned, you can evaluate...
No, you can't, of course. Nothing says that " BobRodes" isn't a valid string assignment. You would have to use space(10), or space(len(x)) for a more generic solution.

My bad...

Bob
 
Bob,

ref your;

<<Therefore
Mid(myVar, 1, 1) = chr$(0)
will always be true if the variable has never been assigned, and always false if it has. >>

Maybe not quite true because I expect it it is possible to reassign Myvar back to String$(len(MyVar),0).

Regs Hugh

 
Bob,

I do see where you're coming from though;

MyVar = String$(len(MyVar),0) is my preferred test.

Reset back to String$(len(MyVar),0) from an assigned value is unlikely unless intended.

regards Hugh
 
Just as you say, Hugh. I thought I tried putting a chr$(0) in there and having it turn into a space, but I was hallucinating apparently. You can explicitly assign null characters, too, and extra locations will be spaces again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top