Click here to Skip to main content
15,911,139 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a array of string .which have two types of values Double and DateTime .And i want to perform some operation when at the first index Datetime comes else other operation.

string receivedData="77.6867#25.437#2018.01.08 14:58:28#12.8241#77.6867#25.375#2018.01.08 14:58:28#12.8241#77.6867#25.437#2018.01.08 14:58:346#25.437#2018.01.08 15:03:20#12.8241#77.6866#25.437#2018.01.08 1#25.437#2018.01.08 14:24:06#12.8241#77.6866#25.312#2018.01.08 14:24:06#12.8241#77.6866#25.375#2018.01.08 14:24:06#12.8241#77.6866#25.375#2018.01.08 14:24:06#12.8241#77.6866#25.375#2018.01.08 14:24:06#12.8241#77.6866#25.375#2018.01.08 14:24:06#12.8241#77.6866#25.375#2018.01.08 14:24:12#12.8241#77.6866#25.375#2018.01.08 14:24:12#12.8241#77.6866#25.375#2018.01.08"
After Removing the delemeter .I always want the 2nd element of the array is of Datetime.

What I have tried:

Thread.Sleep(10000);
SerialPort sp = (SerialPort)sender;
serialPortData = sp.ReadExisting();
int indx = serialPortData.IndexOf("#");
serialPortData = serialPortData.Remove(0, indx + 1);
string[] Data = serialPortData.Split(new string[] { "#" },
StringSplitOptions.RemoveEmptyEntries);
while(Data[1].GetType()!=typeof(System.DateTime))
{
Data = Data.Skip(1).ToArray();
}
this code is not showing any error.
when i put "==" inside while loop it exiting the loop for all values.
when i Put "!=" inside while loop it keep on executing the same loop till last.
thank u in advance.
Posted
Updated 22-Jan-18 1:47am
Comments
Dotnet_Dotnet 22-Jan-18 7:33am    
Sir store in a string array by split func that u did .
into p =0,I=0;
While(I<array.length)
{
if(p==2)
{
Ur work
P=0
}
I++;
}

Data is a string array so that the value returnd by Data[1].GetType() is never the same as typeof(System.DateTime).

You have to try to convert the strings to a double and/or DateTime to check what kind of data are received. See
Double.TryParse Method (String, NumberStyles, IFormatProvider, Double) (System)[^] and DateTime.TryParseExact Method (String, String, IFormatProvider, DateTimeStyles, DateTime) (System)[^] :
C#
DateTime dt;
if (DateTime.TryParseExact(Data[1], "yyyy.MM.dd hh:mm:ss", 
    CultureInfo.InvariantCulture, DateTimeStyles.None, dt)) 
{
    // Is a DateTime
}

double dVal;
// en-US for the period as decimal point
CultureInfo enUS = new CultureInfo("en-US"); 
// You might need to add styles or change it (e.g. for leading sign)
if (Double.TryParse(Data[1], NumberStyles.AllowDecimalPoint, enUS, dVal)) 
{
    // Is a double
}
 
Share this answer
 
Comments
Karthik_Mahalingam 22-Jan-18 7:55am    
5
after
string[] Data = serialPortData.Split(new string[] { "#" }, 

you have string data - so it will never have the type datetime.
I'd suggest just go through each entry and try to parse it into a DateTime


foreach(string strData in Data)
{
.... for every second element or each element that could be converted
DateTime dtData = DateTime.Parse(strData); // Or use tryparse if not shure
}
 
Share this answer
 
I would just iterate through the parsed string and use DateTime.TryParse(Data[x]) - if it parses do the DateTime action, if it doesn't, do the Double action. This assumes the parsed string will only contain DateTimes or Doubles, because anything other than a DateTime will be treated as a Double.
 
Share this answer
 
Use DateTime.ParseExact to find out if the text can be converted to datetime given a specific format.

DateTime dt;
while (DateTime.TryParseExact(Data[1], "yyyy.MM.dd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out dt))


You probably have other logic errors in your code though.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900