Date String Format issues converting from VB6 to VB 2005.NET

B

Brian Parker

I am beginning to work with VB2005.NET and I'm getting some problems
with string formatting converting an application from VB6.

VB6 code:-

sTradeDate = Format(pArray(4,i Record), "mmddyy")

pArray is a variant array containing a date string at pArray(4,
iRecord) in the format "yyyy/mm/dd"

The format function call changes that value into an mmddyy string no
problem, which is what I need it to do.

After running the upgrade wizard I get a converted line like:-

TradeDate.Value = VB6.Format(pArray(4, Record), "mmddyy")

Which also works. Note the TradeDate value is converted to a
VB6.FixedLengthString by the wizard and the variant array pArray
becomes an object.

Trying manually to make this more fully VB.NET-like I tried:-

TradeDate.Value = pArray(4, Record).ToString("mmddyy")

but I get a "Conversion from string "mmddyy" to type 'Integer' is not
valid."

I rather suspect that I have mis-understood something here. Any
assistance would be most gratefully received.

Thanks a lot,
Brian
 
S

Stephany Young

You are absolutely correct. You have misunderstood something.

There is a breaking change from VB6 to VB.Net relating to date formatting
characters.

Where you used mm in VB6 for a 2 digit month you now need to use MM (upper
case). mm (lower case) now represents a 2 digit minute.

For example: DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss")

You haven't mentioned what the data type of TradeDate is but because you are
attempting to assign a formatted value I will assume it is a string.

The String data type does not have a Value property so theres no point in
even thinking about attempting to assign something to that.

An assignment to a string is simply: <stringvariable> = <string expression>

You need to ascertain exactly what is in pArray(4, Record). It won't be a
variant because we don't have those anymore. Instead it will be an Object
which, from what you say, may contain a string. To do this you can use:

MessageBox.Show(pArray(4, Record).GetType.ToString)

which will give you the data type.

If it is, in fact, a string, then to get the value out of it you can use:

CType(pArray(4, Record), String)

Assuming that it contains a string in yyyy/MM/dd format, then to convert it
to MMddyy format you need to convert it to a date and then reconvert it to a
string.

TradeDate = DateTime.ParseExact(CType(pArray(4, Record), String),
"yyyy/MM/dd", Nothing).ToString("MMddyy")

Another thing you need to do is make sure that you have both Option Explicit
and Option Strict turned on. Use of these will highlight many 'problem'
areas that might otherwise be hidden from you.
 
B

Brian Parker

Thanks a lot for the quick and helpful reply.
You are absolutely correct. You have misunderstood something.

There is a breaking change from VB6 to VB.Net relating to date formatting
characters.

Where you used mm in VB6 for a 2 digit month you now need to use MM (upper
case). mm (lower case) now represents a 2 digit minute.

For example: DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss")

Thanks for that, my eye just read right over that one!
You haven't mentioned what the data type of TradeDate is but because you are
attempting to assign a formatted value I will assume it is a string.

Yes I did. :cool: I said this...
Note the TradeDate value is converted to a VB6.FixedLengthString by
the wizard and the variant array pArray blah blah

But you correctly assumed it was a string.
The String data type does not have a Value property so theres no point in
even thinking about attempting to assign something to that.

The conversion wizard puts the Value property in for all cases where
it has converted _fixed length strings_

Dim TradeDate As New VB6.FixedLengthString(6)
gets a .Value for assignment after the wizard.

I will move away from the VB6 fixed length strings when I figure out
what to replace them with, but at the moment it's how the fields are
organised to the correct columns in the program's output.
You need to ascertain exactly what is in pArray(4, Record). It won't be a
variant because we don't have those anymore. Instead it will be an Object
which, from what you say, may contain a string. To do this you can use:

MessageBox.Show(pArray(4, Record).GetType.ToString)

Thanks - useful.
Assuming that it contains a string in yyyy/MM/dd format, then to convert it
to MMddyy format you need to convert it to a date and then reconvert it to a
string.

Right. I think this was the core bit I was getting wrong.
TradeDate = DateTime.ParseExact(CType(pArray(4, Record), String),
"yyyy/MM/dd", Nothing).ToString("MMddyy")

I do think it's a pity when something simple like

sTradeDate = Format(pArray(4,i Record), "mmddyy")

ends up as

TradeDate = DateTime.ParseExact(CType(pArray(4, Record), String),
"yyyy/MM/dd", Nothing).ToString("MMddyy")

and that is progress. But that is whole other can of worms. :cool:
Another thing you need to do is make sure that you have both Option Explicit
and Option Strict turned on. Use of these will highlight many 'problem'
areas that might otherwise be hidden from you.

Thanks, strict was off. Catching a lot of stuff now.

Again many thanks for taking the time to help.
 

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

Top