-
Sep 23rd, 2022, 07:48 PM
#1
Thread Starter
PowerPoster
[RESOLVED] Where are my dots ?
Hi,
Here is a short extract from a fully working project. Everything works exactly as it ought except...
Lines 5 and 6 do not display the final full stop (period).
vb.NET Code:
For i = 0 To spt.Length - 1
chk = spt(i).Trim : If chk.Length <> 5 Then Continue For
TextBox1.Text &= chk.ToLower & ", "
lst1.Add(chk)
Opener.Label2.Text = i.ToString("###,###: Words checked in.")
Opener.Label3.Text = lst1.Count.ToString("###,###: Words verified.")
Opener.Refresh()
Next
Why not ?
There is ample room in the length of both Labels.
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Sep 23rd, 2022, 08:13 PM
#2
Re: Where are my dots ?
This is a "me being too lazy to look up to see for sure" shot at it. The period is probably being treated as a formatting parameter and not as a literal character, so I would try something like escaping it by putting a \ immediately before it.
-
Sep 23rd, 2022, 10:10 PM
#3
Re: Where are my dots ?
I just opened the documentation for the Int32.ToString method that you're using. A couple of links later, I was reading the information specifically relevant to this question.
https://learn.microsoft.com/en-us/do...om-specifier-1
The "." custom format specifier inserts a localized decimal separator into the result string. The first period in the format string determines the location of the decimal separator in the formatted value; any additional periods are ignored.
https://learn.microsoft.com/en-us/do...acter-literals
Format specifiers that appear in a custom numeric format string are always interpreted as formatting characters and never as literal characters.
There are two ways to indicate that characters are to be interpreted as literal characters and not as formatting characters, so that they can be included in a result string or successfully parsed in an input string:
By escaping a formatting character. For more information, see The "" escape character.
By enclosing the entire literal string in quotation apostrophes.
ALWAYS read the relevanht documentation.
-
Sep 25th, 2022, 05:56 AM
#4
Thread Starter
PowerPoster
Re: Where are my dots ?
Thanks guys,
(Why am I not getting 'your thread has been answered' emails ? )
I'd forgotten that I'd posted this question, I'd found the relevant documentation, and I'd figured-out the probable answer prior to that.
The 'fix' I used was to remove the dot from the string and add & "." to the end of the line.
Code:
Opener.Label2.Text = i.ToString("###,###: Words checked in") & "."
Poppa.
Last edited by Poppa Mintin; Sep 25th, 2022 at 06:04 AM.
Reason: Typo
Along with the sunshine there has to be a little rain sometime.
-
Sep 25th, 2022, 06:23 AM
#5
Re: Where are my dots ?
Originally Posted by Poppa Mintin
Thanks guys,
(Why am I not getting 'your thread has been answered' emails ? )
I'd forgotten that I'd posted this question, I'd found the relevant documentation, and I'd figured-out the probable answer prior to that.
The 'fix' I used was to remove the dot from the string and add & "." to the end of the line.
Code:
Opener.Label2.Text = i.ToString("###,###: Words checked in") & "."
Poppa.
If you were going to do that then it would make more sense to take out everything that wasn't specifically for the number. In fact, I wouldn't call ToString on the number there in the first place. If you want to incorporate a number or date or the like into a longer string, use string interpolation:
vb.net Code:
Opener.Label2.Text = $"{i:###,###}: Words checked in."
Also, your format specifier is overly verbose. "#,#" and "n0" would have the same effect.
-
Sep 25th, 2022, 12:42 PM
#6
Re: Where are my dots ?
Originally Posted by Poppa Mintin
(Why am I not getting 'your thread has been answered' emails ? )
There is currently an issue with that, the admins are dealing with it:
https://www.vbforums.com/showthread....meone-responds
-
Sep 26th, 2022, 05:23 AM
#7
Thread Starter
PowerPoster
Re: Where are my dots ?
Originally Posted by si_the_geek
Thanks for the explanation Si.
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Sep 26th, 2022, 05:26 AM
#8
Thread Starter
PowerPoster
Re: Where are my dots ?
Originally Posted by jmcilhinney
If you were going to do that then it would make more sense to take out everything that wasn't specifically for the number. In fact, I wouldn't call ToString on the number there in the first place. If you want to incorporate a number or date or the like into a longer string, use string interpolation:
vb.net Code:
Opener.Label2.Text = $"{i:###,###}: Words checked in."
Also, your format specifier is overly verbose. "#,#" and "n0" would have the same effect.
Thanks John,
Sadly at my age, I like to stick with stuff I think I understand.
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Sep 26th, 2022, 03:19 PM
#9
Re: [RESOLVED] Where are my dots ?
@Poppa Mintin - JMcIlhinney's example uses String interpolation which is essentially calling String.Format (documentation) under the hood.
In other words, his example is functionally equivalent to this:
Code:
Opener.Label2.Text = String.Format("{0}: Words checked in.", i.ToString("###,###"))
The difference is that with String interpolation you do not need to keep track of the index in relation to the parameter, instead you inject it right there into the String. A more subtle difference is that since the variable you're using in String interpolation is going to be implicitly converted to a String, you can pass the formatting by using the : character.
-
Sep 26th, 2022, 09:10 PM
#10
Re: [RESOLVED] Where are my dots ?
Originally Posted by dday9
A more subtle difference is that since the variable you're using in String interpolation is going to be implicitly converted to a String, you can pass the formatting by using the : character.
You can do that with String.Format too:
vb.net Code:
Opener.Label2.Text = String.Format("{0:###,###}: Words checked in.", i)
I tend to use string interpolation as a first choice these days but there are times that I prefer String.Format for specific reasons. Sometimes using string interpolation can produce a long line of code where String.Format allows you to use a shorter pattern and put the parameters on separate lines. Also, if you use the same expression multiple times then you'd have to evaluate it multiple times or introduce an extra variable using string interpolation, where String.Format lets you use it once directly.
vb.net Code:
Dim s1 = $"some text here {someExpression} some more text here {someOtherExpression} yet more text here {someExpression} OMG even more text here"
'or
Dim s1 = String.Format("some text here {0} some more text here {1} yet more text here {0} OMG even more text here",
someExpression,
someOtherExpression)
Last edited by jmcilhinney; Sep 26th, 2022 at 09:17 PM.
-
Sep 26th, 2022, 09:42 PM
#11
Re: [RESOLVED] Where are my dots ?
That makes sense and since strings are immutable I can see situations where it would be more performant to use String.Format.
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
|