469,927 Members | 1,777 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

"Format" Question

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
Nov 21 '05 #1
16 3777
"Al Reid" <ar*****@reidDASHhome.com> schrieb
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

Nov 21 '05 #2
"Armin Zingler" <az*******@freenet.de> wrote in message news:ez**************@TK2MSFTNGP10.phx.gbl...
"Al Reid" <ar*****@reidDASHhome.com> schrieb
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


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
Nov 21 '05 #3
"Al Reid" <ar*****@reidDASHhome.com> schrieb

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?


No. They are just different:
http://msdn.microsoft.com/library/en...gesInVBNET.asp

Armin

Nov 21 '05 #4
"Armin Zingler" <az*******@freenet.de> wrote in message news:OS*************@TK2MSFTNGP14.phx.gbl...
"Al Reid" <ar*****@reidDASHhome.com> schrieb

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?


No. They are just different:
http://msdn.microsoft.com/library/en...gesInVBNET.asp

Armin


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.

Thanks,

--

Al Reid
Nov 21 '05 #5
"Al Reid" <ar*****@reidDASHhome.com> schrieb

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

Nov 21 '05 #6
"Armin Zingler" <az*******@freenet.de> wrote in message news:OK**************@TK2MSFTNGP14.phx.gbl...
"Al Reid" <ar*****@reidDASHhome.com> schrieb

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


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
Nov 21 '05 #7
"Al Reid" <ar*****@reidDASHhome.com> schrieb

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)?


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

Nov 21 '05 #8
"Al Reid" <ar*****@reidDASHhome.com> wrote in message
news:uh**************@TK2MSFTNGP09.phx.gbl...
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.
Nov 21 '05 #9
"Armin Zingler" <az*******@freenet.de> wrote in message news:Ox**************@TK2MSFTNGP12.phx.gbl...

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


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(stringex pression),"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.
Nov 21 '05 #10
"Phill. W" <P.A.Ward@o-p-e-n-.-a-c-.-u-k> wrote in message news:db*********@yarrow.open.ac.uk...
"Al Reid" <ar*****@reidDASHhome.com> wrote in message
news:uh**************@TK2MSFTNGP09.phx.gbl...
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.


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
Nov 21 '05 #11
"Al Reid" <ar*****@reidDASHhome.com> schrieb
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(stringex pression),"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

Nov 21 '05 #12
"Armin Zingler" <az*******@freenet.de> wrote in message news:eH*************@TK2MSFTNGP09.phx.gbl...
"Al Reid" <ar*****@reidDASHhome.com> schrieb
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(stringex pression),"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


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
Nov 21 '05 #13
"Al Reid" <ar*****@reidDASHhome.com> schrieb
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
Nov 21 '05 #14
"Armin Zingler" <az*******@freenet.de> wrote in message news:Op*************@TK2MSFTNGP14.phx.gbl...
"Al Reid" <ar*****@reidDASHhome.com> schrieb
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


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
Nov 21 '05 #15
"Al Reid" <ar*****@reidDASHhome.com> schrieb

You misunderstand me, but enough of that.
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

Nov 21 '05 #16
"Armin Zingler" <az*******@freenet.de> wrote in message news:%2****************@TK2MSFTNGP15.phx.gbl...


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,

--
Al Reid
Nov 21 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Tony | last post: by
5 posts views Thread by Robin Tucker | last post: by
22 posts views Thread by campbellbrian2001 | last post: by
4 posts views Thread by hg | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.