"Format" Question

  • Thread starter Thread starter Al Reid
  • Start date Start date
A

Al Reid

First, I'm using vb2005. I have a string that is read from a barcode reader into a TextBox. The string is 6 characters long and
represents a date (mmddyy). I want to display it to the user in a date format of "mm/dd/yy" For example the barcode contains
"112303" and I want to format it to display "11/23/03"

If I use the microsoft.visualbasic.strings.format with a format string of "##/##/##" or "00/00/00" I get the format string in the
TextBox. If I use the microsoft.visualbasic.compatibility.vb6.format it works as I expect.

What Am I missing here? I would prefer not to use the VB6 compatibility.

TIA,
 
Al Reid said:
First, I'm using vb2005. I have a string that is read from a
barcode reader into a TextBox. The string is 6 characters long and
represents a date (mmddyy). I want to display it to the user in a
date format of "mm/dd/yy" For example the barcode contains "112303"
and I want to format it to display "11/23/03"

If I use the microsoft.visualbasic.strings.format with a format
string of "##/##/##" or "00/00/00" I get the format string in the
TextBox. If I use the
microsoft.visualbasic.compatibility.vb6.format it works as I expect.

What Am I missing here? I would prefer not to use the VB6
compatibility.

Convert it to a Date variable first. Internally always work with it. To
convert to a string, use it's ToString method.

dim s as string = "112303"
dim d as date

d = date.parseexact(s, "MMddyy", nothing)
textbox1.text = d.tostring("MM\/dd\/yy")


Armin
 
Armin Zingler said:
Convert it to a Date variable first. Internally always work with it. To
convert to a string, use it's ToString method.

dim s as string = "112303"
dim d as date

d = date.parseexact(s, "MMddyy", nothing)
textbox1.text = d.tostring("MM\/dd\/yy")


Armin

Thanks, but that doesn't answer the question about the two variations of the Format function. Do you know the correct format string
required to make the Strings.Format function work properly?
 
Al Reid said:
Ok, I see. I have to convert the string to a number (CInt in my
case) before it will apply the specified numeric format string.

Why not convert to Date (because it is Date)?


Armin
 
Armin Zingler said:
Why not convert to Date (because it is Date)?


Armin

Because I don't want to keep converting it back and forth. The displayed format is what I need in this specific instance. I agree
that I could have just converted it to a date as you previously suggested, BUT I wanted to understand why he Strings.Format function
did not give the expected results. I now know why and I learned something and that was what I was after.

Thanks again!
 
Al Reid said:
Because I don't want to keep converting it back and forth.


I don't understand. If you use CInt, you also have to convert twice. First
from "112303" to Integer, then Format to a string. IMO, Integer isn't better
than Date (IMO it's even worse). Tomorrow, you might want to make
calculations with the day (add one week etc). You would have to change your
code again.

The
displayed format is what I need in this specific instance. I agree
that I could have just converted it to a date as you previously
suggested, BUT I wanted to understand why he Strings.Format function
did not give the expected results. I now know why and I learned
something and that was what I was after.

I see, but I still don't understand why you want to use Integer instead of
Date. :-)


Armin
 
Al Reid said:
I have a string that is read from a barcode reader into a TextBox. .. . .
I want to display it to the user in a date format of "mm/dd/yy"
For example the barcode contains "112303" and I want to format
it to display "11/23/03"

If I use the microsoft.visualbasic.strings.format with a format string
of "##/##/##" or "00/00/00" I get the format string in the TextBox.

These formatting characters only work on /numeric/ values.
To "format" something that's /already/ a String, you can use "@",
as in :

Strings.Format( "123456", "@@/@@/@@" )

If you're /sure/ about the order the value will appear in, just slice
and dice it, as in

userDate = "120345"
cleanDate = userDate.substring( 0, 2 ) _
& "/" & userDate.substring( 2, 2 ) _
& "/" & userDate.substring( 4, 2 )

HTH,
Phill W.
 
Armin Zingler said:
I don't understand. If you use CInt, you also have to convert twice. First
from "112303" to Integer, then Format to a string. IMO, Integer isn't better
than Date (IMO it's even worse). Tomorrow, you might want to make
calculations with the day (add one week etc). You would have to change your
code again.



I see, but I still don't understand why you want to use Integer instead of
Date. :-)


Armin

Armin,

I don't want to use the Integer, I want to use a formatted string, BUT to get that with the format function I needed to do this:

Microsoft.VisualBasic.Strings.Format(CInt(stringexpression),"00/00/00") in order to get the correct formatting. Sure, I could
have easily followed your advice and never understood why the format function did not work as expected, but instead I took the time
to understand it.

This is not the correct forum to discuss the details of my application and I wouldn't expect you to understand the details of what I
need to do with the decoded barcode data.

BTW, I've been programming for 25 years in many different languages and I am also a MCSD. I know what I need to do for my
application. I am new to VB.Net and now have learned more about the product and that's a good thing<g>. Given what I now Know
about the Strings.Format function, along with other formatting options, I'll choose the implementation that is most suitable for my
specific application.

Again, thanks.
 
Phill. W said:
These formatting characters only work on /numeric/ values.
To "format" something that's /already/ a String, you can use "@",
as in :

Strings.Format( "123456", "@@/@@/@@" )

If you're /sure/ about the order the value will appear in, just slice
and dice it, as in

userDate = "120345"
cleanDate = userDate.substring( 0, 2 ) _
& "/" & userDate.substring( 2, 2 ) _
& "/" & userDate.substring( 4, 2 )

HTH,
Phill W.

Phil,

Thanks for the input. I just tried that and got "@@/@@/@@" as the function return value. Perhaps this is an issue with VB2005. I
should probably try it with 2003.

I am sure that the data extracted from the barcode will always be in that format. The user never enters the date, just validates
that the barcode was read correctly (the barcode label shows "11/23/03" while the extracted data is "112303"). I had thought of
building it myself, as you indicated, but I thought it would be easy enough to do with the Format function.

Thanks,
 
Al Reid said:
I don't want to use the Integer, I want to use a formatted string,
BUT to get that with the format function I needed to do this:

Microsoft.VisualBasic.Strings.Format(CInt(stringexpression),"00/00/00")
in order to get the correct formatting. Sure, I could have easily
followed your advice and never understood why the format function
did not work as expected, but instead I took the time to understand
it.

I did not say you should not understand it, but now that you understood it,
I was interested in why you still use your solution, not mine. You didn't
provide any reason why, thus I will not be able to understand it.


Armin
 
Armin Zingler said:
I did not say you should not understand it, but now that you understood it,
I was interested in why you still use your solution, not mine. You didn't
provide any reason why, thus I will not be able to understand it.


Armin

The existing COM component is expecting the barcode data to be passed in a specific format (I didn't write it and I'm not going to
modify it because it works). It is expecting the date to be passed as a string in the "mm/dd/yy" format. Therefore, I want to
convert it once to that format and use it that way up to the point of passing it to the COM object. At that point, I'm done with
it. I just don't see the need to convert it once to display it, then again to pass it to the object, rather that converting it once
and using it again when I need it.
 
Al Reid said:
The existing COM component is expecting the barcode data to be
passed in a specific format (I didn't write it and I'm not going to
modify it because it works). It is expecting the date to be passed
as a string in the "mm/dd/yy" format. Therefore, I want to convert
it once to that format and use it that way up to the point of
passing it to the COM object. At that point, I'm done with it. I
just don't see the need to convert it once to display it, then again
to pass it to the object, rather that converting it once and using
it again when I need it.


You misunderstand me, but enough of that.


Armin
 
Armin Zingler said:
You misunderstand me, but enough of that.


Armin

Perhaps we are miscommunicating<g>

How's this:

txtDischDate.Text = Date.ParseExact(strDischDate, "MMddyy", Nothing).ToString("MM/dd/yy")

instead of the Format function?
 
Al Reid said:
Perhaps we are miscommunicating<g>
Probably.

How's this:

txtDischDate.Text = Date.ParseExact(strDischDate, "MMddyy",
Nothing).ToString("MM/dd/yy")

instead of the Format function?


It's soooo great. :-) Now you have noticed that you don't have a reason for
your version. ;-) SCNR

But seriously, I was only curious why you insisted on your version.

I think it's time for EOT now, isn't it? :)


Aah, one hint: "MM/dd/yy" would be converted to "07.19.05" here. To keep the
"/" literally, you need "MM\/dd\/yy".


Armin
 
Armin Zingler said:
It's soooo great. :-) Now you have noticed that you don't have a reason for
your version. ;-) SCNR

But seriously, I was only curious why you insisted on your version.

I think it's time for EOT now, isn't it? :)


Aah, one hint: "MM/dd/yy" would be converted to "07.19.05" here. To keep the
"/" literally, you need "MM\/dd\/yy".


Armin

I'm still learning the .Net Framework so I naturally gravitate toward familiar territory. I think the misunderstanding was that I
thought you were telling me to keep the date in a date variable and to convert it each time I used it. It finally dawned on me that
I could roll it all together, do it once and go about business.

I appreciate the heads up on the date format and I'll definitely keep that in mind. And I agree that we've hit EOT <g>

Thanks,
 

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