|
-
Mar 6th, 2023, 04:46 PM
#1
Thread Starter
New Member
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
-
Mar 6th, 2023, 04:56 PM
#2
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
-
Mar 6th, 2023, 07:45 PM
#3
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
-
Mar 6th, 2023, 08:17 PM
#4
Re: Value of type 'TimeSpan' cannot be converted to 'TimeSpan'
 Originally Posted by Niya
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.
-
Mar 6th, 2023, 09:23 PM
#5
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.
-
Mar 7th, 2023, 12:15 AM
#6
Hyperactive Member
Re: Value of type 'TimeSpan' cannot be converted to 'TimeSpan'
I draw your attention to this resolved thread, here on VBForums..
-
Mar 7th, 2023, 12:18 AM
#7
Re: Value of type 'TimeSpan' cannot be converted to 'TimeSpan'
 Originally Posted by pourkascheff
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.
-
Mar 7th, 2023, 11:06 AM
#8
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
 
-
Mar 7th, 2023, 06:59 PM
#9
Re: Value of type 'TimeSpan' cannot be converted to 'TimeSpan'
 Originally Posted by Shaggy Hiker
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|