problem with len in VB10
-
martes, 06 de marzo de 2012 1:12
Hello,
There are a lot of threads about the function len but I could not find an answer to the following problem: I try to determine the lenght of a string variable, but get the wrong outcome when using: testje = Len(testSTR). When I insert the following line of code: testje = testSTR.Length the answer is still wrong but when I click "Length" I see the correct value. How to get at that value, so I can use it?
dr f.j.meijer
Todas las respuestas
-
martes, 06 de marzo de 2012 1:21Can you post the content of testSTR? Which value does the Len() function and the Length property return, and which value do you expect?
Armin
-
martes, 06 de marzo de 2012 1:22
Hi,
If testje
is an Integer as in
Dim testje As Integer
then as soon as the line
testje = testSTR.Length
is executed the result from Length will be stored in testje
Try this line straight after for proof.>>
MessageBox.Show(testje.ToString)
Do not confuse the LENGTH of a string with the last character index for say the SubString function.
"John" will have a length of 4
The "n" is at character position 3 as the "J" is at character position zero.
I hope this helps. :-D
Regards,

Click this link to see the NEW way of how to insert a picture into a forum post.
Installing VB6 on Windows 7
App Hub for Windows Phone & XBOX 360 developers.
- Editado John Anthony Oliver martes, 06 de marzo de 2012 1:27
- Marcado como respuesta drfjm martes, 06 de marzo de 2012 23:45
-
martes, 06 de marzo de 2012 23:45Yes, it did, thanks
dr f.j.meijer
-
martes, 06 de marzo de 2012 23:51
Hello to John Oliver,
Do I understand well that it is better to use the instructions you provided, instead of the function LEN? When reading the help offered by VB about LEN it says two answers are possible, to avoid confusion therefore perhaps also better to use your solution.
dr f.j.meijer
-
miércoles, 07 de marzo de 2012 11:39
Hello to John Oliver,
Do I understand well that it is better to use the instructions you provided, instead of the function LEN? When reading the help offered by VB about LEN it says two answers are possible, to avoid confusion therefore perhaps also better to use your solution.
dr f.j.meijer
Hi,
It is better to use .Length for a STRING or any other type where .Length
is available as it makes your code more readable and intelligible.
I hope this helps. :)
You can use LEN but be aware that you can use LEN for the following TYPEs:
- Boolean
- Sbyte
- Byte
- Short
- UShort
- Integer
- UInteger
- Long
- ULong
- Decimal
- Single
- Double
- Date
- Char
- String
- Object
That is because the LEN function is said to be overloaded, meaning you can use it more than one manner.
The LEN function is still in Visual Basic from previous versions and I believe it is even
in VB6 and earlier versions therefore it is often referred to as a 'legacy' command.
Try this code and you will see the result is the same however for either.>>
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim teststring As String teststring = "Dr F.J.Meijer" 'For a string it is okay to use .Length >> Dim strLength As Integer = teststring.Length MessageBox.Show(strLength.ToString, "Using .Length = ") 'The result shown is 13 strLength = Len(teststring) MessageBox.Show(strLength.ToString, "Using LEN = ") 'The result shown is also 13 End Sub End ClassThe way Intellisense describes LEN is in a manner that I can not write better.
Any valid String expression or variable name.
If Expression is of type Object, the Len function returns the size
as it will be written to the file by the FilePut function.
See this thread for an example using LEN with a short program that uses FilePut >>
http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/50d7b930-edd3-4ba4-92fe-2c16e5235e51
Regards,

Click this link to see the NEW way of how to insert a picture into a forum post.
Installing VB6 on Windows 7
App Hub for Windows Phone & XBOX 360 developers.
- Editado John Anthony Oliver miércoles, 07 de marzo de 2012 11:58
-
miércoles, 07 de marzo de 2012 23:36
Hello
and thanks again. Do you mean the example saying to add two buttons to the form? It does not function, perhaps it needs adaptation to VB 10?
bye
fjm
dr f.j.meijer
-
jueves, 08 de marzo de 2012 0:08
Hello
and thanks again.
Do you mean the example saying to add two buttons to the form? It does not function, perhaps it needs adaptation to VB 10?
bye
fjm
dr f.j.meijer
Hi again,
If you are trying the code here.>>
http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/50d7b930-edd3-4ba4-92fe-2c16e5235e51
It does not need any changes for VB10 or 2010 editions of VB.Net
it should still work. What problem(s) are you facing trying to run it?
Regards,

Click this link to see the NEW way of how to insert a picture into a forum post.
Installing VB6 on Windows 7
App Hub for Windows Phone & XBOX 360 developers.
- Editado John Anthony Oliver jueves, 08 de marzo de 2012 0:09
-
jueves, 08 de marzo de 2012 4:49hello,
I placed a break point at the line: Button1.Text = "Save." in sub form_load; executing apparently does not touch that line as I get the message: "Verify that the file exists in the specified location". Am I doing something wrong?
bye
fjmdr f.j.meijer
-
jueves, 08 de marzo de 2012 13:22
Hello,
When at home I tried your program on the 32 bit computer and there if runs flawless!! So it seems VB is not ready for 64 bit?
Furthermore I was looking for answers about how to find EOF and found your answer of 2008. I tried your program and it runs too on the 32 bit one
bye
fjm.
dr f.j.meijer
-
jueves, 08 de marzo de 2012 13:29
When at home I tried your program on the 32 bit computer and there if runs flawless!! So it seems VB is not ready for 64 bit?
VB is ready for 64 bit, and the code posted in this thread is independent from the platform. What exactly is the code you're using now? Regarding the message "Verify that the file exists in the specified location" I found this thread:
http://social.msdn.microsoft.com/Forums/en/wpf/thread/77ea8475-305e-48d7-9eea-217e40c9c30bMaybe it applies to your situation, too.
Armin
-
jueves, 08 de marzo de 2012 13:37
Hello,
I tried once more Oliver's program, copied it anew and now it functions also on the 64 bit computer,
so I think the first time I copied it something went wrong. I am sorry for the fuss i made
bye and thanks
dr f.j.meijer

