Results 1 to 9 of 9

Thread: Value of type 'TimeSpan' cannot be converted to 'TimeSpan'

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2016
    Posts
    10

    Value of type 'TimeSpan' cannot be converted to 'TimeSpan'

    Hello,

    I am reading in a file which contains the following:
    Process started 2023/03/04 22:17:53
    Process completed 2023/03/04 22:18:40
    Based on this, I wish to be able to place the start/stop times into DateTime, and then subtract start time from stop time, and use this resultant value to list the actual time the process ran.

    I have a function which returns a TimeSpan, and this value is then included in a string that will be displayed in MessageBox()
    I get the same error message for each determination of stop-start values.
    What do I need to do to get this to work?
    I am using VS2022, and Net 6.

    Thank you for any suggestions.

    Code:
    Public Shared Function CalcTime(Index As Byte) As TimeSpan
            Dim duration As New TimeSpan
    
    . . .
    Dim startTime As New DateTime(22,17,53)
                                Dim endTime As New DateTime(22,18,40)
                                
                                duration = endTime - startTime        'Subtract start time from end time -> error message: Value of type 'TimeSpan' cannot be converted to 'TimeSpan'
                                'duration = endTime.Subtract(startTime) 'Subtract start time from end time -> error message: Value of type 'TimeSpan' cannot be converted to 'TimeSpan'
    Return duration
        End Function
    
    Private Shared Sub Test()
    Dim duration As TimeSpan
    Dim durStr As String
                        duration = CalcTime(0)
                        durStr = String.Format("{0:hh\\:mm\\:ss}", duration)
                        Try
                            durStr = String.Format("{0:hh\\:mm\\:ss}", duration)
                        Catch e As FormatException
                            durStr = "Invalid Format"
                        End Try
      End Sub

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Value of type 'TimeSpan' cannot be converted to 'TimeSpan'

    Two things jump out at me right away:
    1) You don't need to dim duration as a new TimeSpan ... it will get assigned a TimeSpan object as part of the subtraction. Just dim it as a TimeSpan
    2) You're using the wrong constructor for the DateTime ... the only three parameter constructor has YEAR, MONTH, and DAY .... not Hour, Minute, Second as you're trying to use.
    Public Sub New (year As Integer, month As Integer, day As Integer)


    You'll want this one:

    Or use .ParseExact and pass in a string "2023-03-06 22:17:53" or what ever date you want.
    https://learn.microsoft.com/en-us/do...t?view=net-7.0


    It's possible you've got garbage in, getting garbage out.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Value of type 'TimeSpan' cannot be converted to 'TimeSpan'

    lol I answered this same question on StackOverflow yesterday:-
    https://stackoverflow.com/questions/...49453#75649453
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Value of type 'TimeSpan' cannot be converted to 'TimeSpan'

    Quote Originally Posted by Niya View Post
    lol I answered this same question on StackOverflow yesterday:-
    https://stackoverflow.com/questions/...49453#75649453
    So the question now is whether the OP is a student in the same class as that poster on StackOverflow or they are the same person and have simply ignored the help they asked for when it was provided. The fact that the code has the same weird indenting as on SO suggests the latter.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Value of type 'TimeSpan' cannot be converted to 'TimeSpan'

    Oh it's definitely the same dude. Program code is like a signature, and the code here is almost exactly the same as over there on SO.

    My guess is that he is trying to maximize coverage by posting on multiple sites which isn't really a bad strategy but he should probably check back to see if he got answers. He probably hasn't checked back at SO yet.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Re: Value of type 'TimeSpan' cannot be converted to 'TimeSpan'

    I draw your attention to this resolved thread, here on VBForums..

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Value of type 'TimeSpan' cannot be converted to 'TimeSpan'

    Quote Originally Posted by pourkascheff View Post
    I draw your attention to this resolved thread, here on VBForums..
    I don't think the actual calculation is the issue here. Maybe that will be an issue later but this question is about the fact that the name TimeSpan doesn't seem to refer to the System.TimeSpan type, so the OP seems to have another type in or referenced by their project with that name.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Value of type 'TimeSpan' cannot be converted to 'TimeSpan'

    More likely, they cast a net widely. They got an answer there, and that might well be good enough. I have no problem with somebody asking a question in multiple forums in the hopes of getting an answer.

    I'm more interested in the error message, though, and that has not been answered. Without looking into it further, I would guess that there are multiple structures with the name TimeSpan. Therefore, the error message is essentially right, but would have been more helpful had it fully decorated the object name such that it read along the lines of:

    "Value of type X.TimeSpan cannot be converted to Y.TimeSpan."

    I am not aware of multiple TimeSpan structures, but TG's answer does seem to support the idea. After all, creating a new DateTime could work with a year of 22, but there is no month 17, nor a month with 53 days, so it seems like there should have been an ArgumentOutOfRangeException thrown. The documentation supports that. Therefore, for the code to even have gotten far enough to get the error that was reported, something else is going on.
    My usual boring signature: Nothing

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Value of type 'TimeSpan' cannot be converted to 'TimeSpan'

    Quote Originally Posted by Shaggy Hiker View Post
    More likely, they cast a net widely. They got an answer there, and that might well be good enough. I have no problem with somebody asking a question in multiple forums in the hopes of getting an answer.
    The issue is not about posting in multiple forums. I'd enourage that to. The issue is that the question was posted there first, then several hours passed before an answer was provided, then several more hours passed before the question was posted here. If you're posting in several places altogether then that's fine. If you've already posted elsewhere and don't bother to go back and check whether someone has responded and keep posting the same question elsewhere then you're wasting people's time for no good reason. You're wasting your own time too. I would conjecture that a person who does that will also fail to indicate that an issue has been resolved when they do see an answer elsewhere.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width