Click here to Skip to main content
15,902,032 members
Home / Discussions / C#
   

C#

 
AnswerRe: EF 6 and C# applications (Web & Windows) Pin
Mycroft Holmes1-Aug-16 13:01
professionalMycroft Holmes1-Aug-16 13:01 
QuestionHow To Create Math Parser? Pin
Daniyaltjm31-Jul-16 2:12
Daniyaltjm31-Jul-16 2:12 
AnswerRe: How To Create Math Parser? Pin
OriginalGriff31-Jul-16 2:29
mveOriginalGriff31-Jul-16 2:29 
GeneralRe: How To Create Math Parser? Pin
Daniyaltjm31-Jul-16 4:07
Daniyaltjm31-Jul-16 4:07 
GeneralRe: How To Create Math Parser? Pin
OriginalGriff31-Jul-16 4:19
mveOriginalGriff31-Jul-16 4:19 
Questionneed help witch creating method Pin
Member 1138393529-Jul-16 23:55
Member 1138393529-Jul-16 23:55 
AnswerRe: need help witch creating method Pin
OriginalGriff30-Jul-16 0:11
mveOriginalGriff30-Jul-16 0:11 
QuestionParsing a string with multiple values - JSON or Retro? Pin
Michael Breeden29-Jul-16 1:48
Michael Breeden29-Jul-16 1:48 
I'm getting unstructured data in a string field. I told the Db guy to put it in a JSON string and I use NewtonSoft to parse it out. I know what I am getting (maybe) and it is different for each Business Unit and even for parts of Business Units. For example, originally I got strings from the Db guy like:
"MCCFL: unique_id=1709", "MCCFL: DetailID=80" and "TCCM: unique_id=1166"

Not too helpful, so for the next business unit, I said I wanted JSON, so I got something like:
{"BU":"MRXE","unique_id":"4208","job_id":"1395","track_number":"9012712162","job_count:1"}

That would make the previous strings look like:
{"BU":"MCCFL","unique_id":"1709"}, {"BU":"MCCFL","DetailID":"80"} and {"BU":"TCCM","unique_id":"1166"}

Notice that all of these represented as a data structure or object, would be quite different, so I parsed it with NewtonSoft thus:
C#
dynamic deserialized = Newtonsoft.Json.JsonConvert.DeserializeObject(strData);
if (deserialized.BU == null)
    strError = "BU";
else
{
    this.BusinessUnit = deserialized.BU;
    strBusinessUnit = this.BusinessUnit;
}
    if (deserialized.job_id == null)
    strError = "job_id";
else
    this.str_job_id = deserialized.job_id;
...
...

Now the point of this is that I had been considering do I want to use NewtonSoft. It's supposed to be light weight, but I could go retro and get my string like this:
BU:MRXE|unique_id:4208|job_id:1395|track_number:9012712162|job_count:1

Then I could use this:
C#
/// </summary>
/// <param name="strInBarSeperator">'|' separated string of ':' separated string pairs</param>
/// <param name="strValueToFind">First member of string pair</param>
/// <param name="strValueFound">Second member of string pair</param>
/// <param name="iValueFound">Integer value of Second member of string pair</param>
/// <returns>integer value returned. -1 is error.</returns>
public static string Return_StringValueForString(string strInBarSeperator, string strValueToFind, out string strValueFound, out int iValueFound)
{
    strValueFound = String.Empty;
    string strError = "Error-";
    iValueFound = 0;

    if ((strValueToFind == null) || (strInBarSeperator == null))
    {
        strValueFound = strError;
        return strError;
    }

    string[] strarr = strInBarSeperator.Split('|');
    if (strarr.Length < 1)
    {
        strValueFound = strError;
        return strError;
    }

    int iPositionInString = 0;
    for (iPositionInString = 0; iPositionInString < strarr.Length; iPositionInString++)
    {
        if (strarr[iPositionInString].Contains(strValueToFind) == true)
            break;
    }

    if (iPositionInString == strarr.Length)
    {
        strValueFound = strError;
        return strError;
    }

    string[] strarr2 = strarr[iPositionInString].Split(':');
    if (strarr2.Length < 2)
    {
        strValueFound = strError;
        return strError;
    }

    strValueFound = strarr2[1];

    int ii = 0;
    if (Int32.TryParse(strValueFound, out ii) == true)
        iValueFound = ii;
    else
        iValueFound = 0; // -1; so does not return error since this is only looking for a string
    return strValueFound;
}

Now there is another version more specialized for getting an integer, but that is a changed line of code:
C#
public static int Return_IntValueForString(string strInBarSeperator, string strValueToFind, out string strValueFound, out int iValueFound)

The point of posting this is what do you think? I think I'm going to remove the NewtonSoft assembly. I have no need for it and this code should be both simpler and more efficient. Partly it is a question about the efficiency of NewtonSoft and partly abous the "dynamic" variable. The method is a bit quick and dirty and cannot be used to return a value of -1, but nothing in the application should be able to generate a negative number anyway... and that would be an easy update if desired. This should be more versatile actually, a bit less coding. Can you see any problems with the plan? I'm going to need this in a lot of places. T'anks. M
AnswerRe: Parsing a string with multiple values - JSON or Retro? Pin
Richard Deeming29-Jul-16 2:09
mveRichard Deeming29-Jul-16 2:09 
GeneralRe: Parsing a string with multiple values - JSON or Retro? Pin
Michael Breeden29-Jul-16 2:43
Michael Breeden29-Jul-16 2:43 
GeneralRe: Parsing a string with multiple values - JSON or Retro? Pin
Richard Deeming29-Jul-16 2:47
mveRichard Deeming29-Jul-16 2:47 
GeneralRe: Parsing a string with multiple values - JSON or Retro? Pin
Michael Breeden29-Jul-16 3:18
Michael Breeden29-Jul-16 3:18 
GeneralRe: Parsing a string with multiple values - JSON or Retro? Pin
Richard Deeming29-Jul-16 3:36
mveRichard Deeming29-Jul-16 3:36 
GeneralRe: Parsing a string with multiple values - JSON or Retro? Pin
Michael Breeden29-Jul-16 4:02
Michael Breeden29-Jul-16 4:02 
Questionconvert Ms access project to c# Pin
dabbourabd28-Jul-16 23:00
dabbourabd28-Jul-16 23:00 
AnswerRe: convert Ms access project to c# Pin
Richard MacCutchan28-Jul-16 23:08
mveRichard MacCutchan28-Jul-16 23:08 
AnswerRe: convert Ms access project to c# Pin
OriginalGriff28-Jul-16 23:09
mveOriginalGriff28-Jul-16 23:09 
GeneralRe: convert Ms access project to c# Pin
dabbourabd28-Jul-16 23:37
dabbourabd28-Jul-16 23:37 
GeneralRe: convert Ms access project to c# Pin
dabbourabd28-Jul-16 23:32
dabbourabd28-Jul-16 23:32 
AnswerRe: convert Ms access project to c# Pin
BillWoodruff29-Jul-16 3:16
professionalBillWoodruff29-Jul-16 3:16 
GeneralRe: convert Ms access project to c# Pin
dabbourabd29-Jul-16 11:00
dabbourabd29-Jul-16 11:00 
AnswerRe: convert Ms access project to c# Pin
Eddy Vluggen29-Jul-16 4:39
professionalEddy Vluggen29-Jul-16 4:39 
GeneralRe: convert Ms access project to c# Pin
dabbourabd29-Jul-16 10:58
dabbourabd29-Jul-16 10:58 
GeneralRe: convert Ms access project to c# Pin
Eddy Vluggen29-Jul-16 14:26
professionalEddy Vluggen29-Jul-16 14:26 
Questionc#, .NET, Application.Idle Event: Confusion with MSDN documentation Pin
User 1106097928-Jul-16 20:11
User 1106097928-Jul-16 20:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.