Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,080 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,585 people online right now. Registration is fast and FREE... Join Now!




Typecasting objects to doubles

 
Reply to this topicStart new topic

Typecasting objects to doubles

Rocket452
7 Apr, 2008 - 07:53 AM
Post #1

New D.I.C Head
*

Joined: 3 Apr, 2008
Posts: 10

So I've got an array of objects that return a number of seconds based upon a SQL query.

CODE
static ArrayList returnValueArray3 = new ArrayList();


I want to convert the values in this arrray through a method that will convert it into an easy to read format. (something like hrs/min/sec or something like that.)

I tried using this, and similiar methods:

CODE
TimeSpan.FromSeconds(returnValueArray3[i])


the problem is that I have an array of objects and I need to cast them to something like a double. I've tried several things to try and cast, but nothing has worked.

User is offlineProfile CardPM
+Quote Post

zakary
RE: Typecasting Objects To Doubles
7 Apr, 2008 - 07:58 AM
Post #2

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 404



Thanked: 6 times
Dream Kudos: 175
My Contributions
string time = DateTime.Parse(TimeSpan.FromSeconds(returnValueArray3[i])).ToString("hh:mm:ss")
User is offlineProfile CardPM
+Quote Post

Rocket452
RE: Typecasting Objects To Doubles
7 Apr, 2008 - 08:08 AM
Post #3

New D.I.C Head
*

Joined: 3 Apr, 2008
Posts: 10

I tried your solution, and I'm still having the same typecast problems.

Error 1
The best overloaded method match for 'System.TimeSpan.FromSeconds(double)' has some invalid arguments

Error 2
Argument '1': cannot convert from 'object' to 'double'
User is offlineProfile CardPM
+Quote Post

zakary
RE: Typecasting Objects To Doubles
7 Apr, 2008 - 08:17 AM
Post #4

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 404



Thanked: 6 times
Dream Kudos: 175
My Contributions
string val = Convert.ToDouble(returnValueArray3[i].ToString());

then string time = DateTime.Parse(TimeSpan.FromSeconds(val).ToString("hh:mm:ss")
User is offlineProfile CardPM
+Quote Post

Rocket452
RE: Typecasting Objects To Doubles
7 Apr, 2008 - 08:22 AM
Post #5

New D.I.C Head
*

Joined: 3 Apr, 2008
Posts: 10

val = Convert.ToDouble(returnValueArray3[i].ToString());

Error 1 Cannot implicitly convert type 'double' to 'string'

That error, plus the same errors as before.
User is offlineProfile CardPM
+Quote Post

zakary
RE: Typecasting Objects To Doubles
7 Apr, 2008 - 08:25 AM
Post #6

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 404



Thanked: 6 times
Dream Kudos: 175
My Contributions
yeah my bad sorry string val = should be double val LOL
User is offlineProfile CardPM
+Quote Post

Rocket452
RE: Typecasting Objects To Doubles
7 Apr, 2008 - 08:36 AM
Post #7

New D.I.C Head
*

Joined: 3 Apr, 2008
Posts: 10

Thanks so much for your help so far.

Now its giving me:

DateTime.Parse(TimeSpan.FromSeconds(val).ToString("hh:mm:ss"));

Error 1
No overload for method 'ToString' takes '1' arguments

Are you using some sort of special reference or namespace for this overload of ToString()?
User is offlineProfile CardPM
+Quote Post

zakary
RE: Typecasting Objects To Doubles
7 Apr, 2008 - 09:33 AM
Post #8

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 404



Thanked: 6 times
Dream Kudos: 175
My Contributions
No it is System.ToString(); try "HH:mm:ss" but that will return it in military time. but the issue could be with the value you have in your array if you post your code I could help you much easier

u don't have to use this:
DateTime.Parse(TimeSpan.FromSeconds(val).ToString("hh:mm:ss"));

u can do this.
System.TimeSpan time = TimeSpan.FromSeconds(val);


User is offlineProfile CardPM
+Quote Post

Rocket452
RE: Typecasting Objects To Doubles
7 Apr, 2008 - 10:14 AM
Post #9

New D.I.C Head
*

Joined: 3 Apr, 2008
Posts: 10

OK, now things compile cleanly. Thanks so much

After all of this, I'm not sure how much I like the FromSeconds method output format.
14.04:47:10 is sort of counter-intuitive


But things shouldnt be hard to change now that the datatype conversions are ok.
User is offlineProfile CardPM
+Quote Post

zakary
RE: Typecasting Objects To Doubles
7 Apr, 2008 - 10:16 AM
Post #10

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 404



Thanked: 6 times
Dream Kudos: 175
My Contributions
Well we are here to help and pass us on to your freinds
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 07:30PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month