HarryBedi

Hi guys I keep getting the following error when I try to iterate through an array

foreach (DateTime TimeStamp in theTickData[0])//Cannot Convert type 'char' to System.DateTime'

{

if (TimeStamp >= dtEntryTime)

{

foreach(string PG in theTickData)

{

if(PG == theDirection)

{

foreach (double theRate in theTickData[1])

{

alEntryPointEURCHF.Add(theRate);

}

alEntryPointEURCHF.Add(TimeStamp);

alEntryPointEURCHF.Add(Direction);

}

}

}

}

}

The foreach statement comes back with Cannot Convert type 'char' to System.DateTime' Does anybody know why and what I should do to correct this mistake

Thanks,

Harry



Re: Visual C# Language Cannot Convert type 'char' to System.DateTime'

TilakGopi

Is TickData a Jagged array

Could u please it's decleration and allocations.

Thanx,

Ch.T.Gopi Kumar.






Re: Visual C# Language Cannot Convert type 'char' to System.DateTime'

HarryBedi

Hi sorry no its not a jagged array - its an array list.

I am not sure what you mean by "it's decleration and allocations".

Ta

Harry





Re: Visual C# Language Cannot Convert type 'char' to System.DateTime'

TilakGopi

If it is arraylist (of which type again),

how can theTickData[0] be of type DateTime and theTickData[1] be double type as u r using in for each loops

Thanx,

Ch.T.Gopi Kumar.






Re: Visual C# Language Cannot Convert type 'char' to System.DateTime'

HarryBedi

Because I have previously parsed them in a previouse foreach statement.



Re: Visual C# Language Cannot Convert type 'char' to System.DateTime'

TilakGopi

Then,

why didn't u get the error at the previous foreach statement

Thanx,

Ch.T.Gopi Kumar.






Re: Visual C# Language Cannot Convert type 'char' to System.DateTime'

Marcelo Guerra - MSFT

Hi,

theTickData[0] is an Array of DateTimes and the theTickData[1] is an Array of doubles, correct

The error suggests that theTickData[0] is either an Array of char or an String, is that possible

Can you post the code where you assign theTickData[0]





Re: Visual C# Language Cannot Convert type 'char' to System.DateTime'

Peter Ritchie

if theTickData is defined as "char theTickData" then there is nothing to convert a char to DateTime.

If you explain what you're storing in theTickData, maybe someone can offer a suggestion on how to convert it to a DateTime object.




Re: Visual C# Language Cannot Convert type 'char' to System.DateTime'

HarryBedi

Guys,

This is a larger sample of my code.

foreach (string sLine in TickData)

{

theTickData = sLine.Split('\t');

Trading.PaidGivenData ThePaidGivenData = new Trading.PaidGivenData();

ThePaidGivenData.TimeStampGiven = DateTime.FromOADate(double.Parse(theTickData[0]));

ThePaidGivenData.Rate = double.Parse(theTickData[1]);

ThePaidGivenData.PaidGiven = theTickData[2];

ThePaidGivenData.CcyCcy = theTickData[3];

foreach (DateTime TimeStamp in theTickData[0])

{

if (TimeStamp >= dtEntryTime)

{

foreach(string PG in theTickData)

{

if(PG == theDirectionExit)

{

foreach (double theRate in theTickData[1])

{

alExitPointEURCHF.Add(theRate);

}

alExitPointEURCHF.Add(TimeStamp);

alExitPointEURCHF.Add(Direction);

}

}

}

}

}

}

Thanks,

Harry





Re: Visual C# Language Cannot Convert type 'char' to System.DateTime'

TilakGopi

Hi,

Still i few queries regd ur code.

1)foreach (DateTime TimeStamp in theTickData[0]) - if the theTickData[0] is string , how can u iterate for DateTime ,that's why ur getting error :

Cannot Convert type 'char' to System.DateTime'

So to iterate for DateTimes the loop should be

foreach (string strDateTime in theTickData)

{

DateTime TimeStamp = DateTime.FromOADate(double.Parse(strDateTime));

....

}

2)foreach (double theRate in theTickData[1]) - Here again theTickData[1] is string only , how can u iterate for double values in it.

If it can be conveted to

foreach (string strRate in theTickData)

{

double theRate = double.Parse(strRate);

alExitPointEURCHF.Add(theRate);

}

I don't know how far it is correct as one time theTickData array is getting converted to DateTime and second time it's getting converted to double.

So far it is appropriate in ur code , i can't guess.

Hope these pointouts will help u in solving ur problem.

Thanx,

Ch.T.Gopi Kumar.






Re: Visual C# Language Cannot Convert type 'char' to System.DateTime'

ShellShock

String.Split returns an array of strings, so theTickData[0] will be a string. You need to convert this string into a DateTime - you can use DateTime.Parse, e.g.,

DateTime timeStamp = DateTime.Parse(theTickData[0]);

I don't think you need the "foreach (DateTime TimeStamp in theTickData[0])". theTickData[0] is a string, whereas foreach needs a collection.





Re: Visual C# Language Cannot Convert type 'char' to System.DateTime'

TilakGopi

Hi,

Harry,

I've two observations in ur code:

1)foreach (DateTime TimeStamp in theTickData[0])

Here theTickData[0] is of type string ,then how can u iterate it for DATETIME , one thing u can do is modify it as

foreach (string strTimeStamp in theTickData)

{

DateTime TimeStamp = DateTime.FromOADate(double.Parse(strTimeStamp));

.......

}

2)foreach (double theRate in theTickData[1])

Here also theTickData[1] is of type string and u r iterating for double in that..,it also can be modified to ..

foreach (string strRate in theTickData)

{

double theRate = double.Parse(strRate);

alExitPointEURCHF.Add(theRate);

}

but here, i have got 2 case where theTickData data can be formatted to DateTime once to double once.I'm not sure whether this fits in ur code..

Thanx,
Ch.T.Gopi Kumar.






Re: Visual C# Language Cannot Convert type 'char' to System.DateTime'

Peter Ritchie

The line "foreach(DateTime TimeStamp in theTickData[0])" is trying to convert each char in the theTickData string at index 0. You cannot implicitly convert a character to a DateTime object.

Maybe you want to parse each string resulting from the Split call Something like:
foreach(String text in theTickData)
{
DateTime timeStamp;
if(true == DateTime.TryParse(text, out timeStamp))
{
// TODO: do something with timeStamp
}
}




Re: Visual C# Language Cannot Convert type 'char' to System.DateTime'

micvos

thTickData is an array of strings so theThickData[0] is a string. Calling the iterator from a string (which is what the foreach instruction does) will result in the return of char.




Re: Visual C# Language Cannot Convert type 'char' to System.DateTime'

ShellShock

As String.Split returns an array of strings, then theTickData[0] will be a string; you are using foreach on a string, whereas it should be used for collections.

Use DateTime.Parse to convert theTickData[0] into a DateTime type.