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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Parker

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

    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(pArr ay(4, Record), "mmddyy")

    Which also works. Note the TradeDate value is converted to a
    VB6.FixedLength String 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).ToStrin g("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
  • Stephany Young

    #2
    Re: Date String Format issues converting from VB6 to VB 2005.NET

    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.To String("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.ParseE xact(CType(pArr ay(4, Record), String),
    "yyyy/MM/dd", Nothing).ToStri ng("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.


    "Brian Parker" <bparker@nemian life.luwrote in message
    news:6kva539bms afo37k4qcfeoh3g r8d8imjr9@4ax.c om...
    >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(pArr ay(4, Record), "mmddyy")
    >
    Which also works. Note the TradeDate value is converted to a
    VB6.FixedLength String 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).ToStrin g("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

    Comment

    • Brian Parker

      #3
      Re: Date String Format issues converting from VB6 to VB 2005.NET

      On Fri, 25 May 2007 02:09:34 +1200, "Stephany Young" <noone@localhos t>
      wrote:

      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.To String("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. 8-) I said this...
      Note the TradeDate value is converted to a VB6.FixedLength String 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.FixedLength String(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.ParseE xact(CType(pArr ay(4, Record), String),
      >"yyyy/MM/dd", Nothing).ToStri ng("MMddyy")
      I do think it's a pity when something simple like

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

      ends up as

      TradeDate = DateTime.ParseE xact(CType(pArr ay(4, Record), String),
      "yyyy/MM/dd", Nothing).ToStri ng("MMddyy")

      and that is progress. But that is whole other can of worms. 8-)
      >
      >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.
      >
      >
      >"Brian Parker" <bparker@nemian life.luwrote in message
      >news:6kva539bm safo37k4qcfeoh3 gr8d8imjr9@4ax. com...
      >>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.Valu e = VB6.Format(pArr ay(4, Record), "mmddyy")
      >>
      >Which also works. Note the TradeDate value is converted to a
      >VB6.FixedLengt hString by the wizard and the variant array pArray
      >becomes an object.
      >>
      >Trying manually to make this more fully VB.NET-like I tried:-
      >>
      >TradeDate.Valu e = pArray(4, Record).ToStrin g("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

      Comment

      Working...