convert to Datetime array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NareshN
    New Member
    • Aug 2010
    • 45

    convert to Datetime array

    Hi All,

    I have stringarray called Timesplit with time like(17:00-2:00).How to store Timeplit array values into datetime array value.I am having 7 values that's why i am using TimeIn[arrayIndex].I will increment arrayIndex for 7 times for 7 timeIn.

    I am getting error while trying to store TimesPlit[0] to TimeIn[arrayIndex].I want 17:00 into TimeIn[arrayIndex] and 2:00 into TimeOut[arrayIndex].If i use datetime will get date also so please give me better options to store TimeIn and TimeOut.

    Object reference error
    Code:
       DateTime[] TimeOut = null;
       TimeIn[arrayIndex]=Convert.ToDateTime(TimeSplit[0]); 
       TimeOut[arrayIndex] = TimeSplit[1];
    Last edited by Frinavale; Sep 28 '10, 01:49 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    You can access the Date or the Time portions of a DateTime Type.


    As for the error you're getting...it's a Null Reference error ("Object reference Not set to an instance"). This occurs when you attempt to use an object that has not been initialized. In other words, the object is Null (or, in VB.NET, Nothing).

    When you attempt to access a property or method of something that is Null or Nothing, you will get a Null Reference error.


    In your case you are setting the TimeOut array to null and then a couple of lines latter you are trying to use it! Obviously this is going to cause a problem.

    You should properly initialize the array before you use it:
    Code:
    DateTime[] TimeOut;
    TimeOut= new DateTime[7];
    (You should do the same for the TimeIn array)

    -Frinny

    Comment

    Working...