return an array or list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • temlehdrol
    New Member
    • Feb 2010
    • 5

    return an array or list

    What I'm trying to do is get two values back to the original section (ParseMessage). .. Right now I'm just focusing on the csvClientReader portion, once I get that working I can get the second part working.
    Right now it passes back a single value into strClient, what I really need is for it to pass back the values of:
    parsedData[intX][intY] ----- in this case intY would equal 1
    parsedData[intX][intY] ----- in this case intY would equal 2

    I'm using C# 2005
    I'm still cleaning up some of the code so please bare with me as I'm still fairly new to C#... Thank you in advance...

    Code:
    void ParseMessage(string strCompleteMsg, string strCheckForSubject)
            {
                string strFileName = "";
                Int16 intFound = 0;
                string strClient = "";
                string strSubject;
                
                //Call ClientReader to get information about file from strCheckForSubject
                strClient = csvClientReader(strClient, strSubject = strCheckForSubject);
                
                strFileName = strClient;
                string strOne = "";
                strOne = csvFileReader(strFileName, strOne);
            }
    
    public string csvClientReader(string strSubject, string strClient)
            {
                using (StreamReader readFile = new StreamReader("M:\\pgtreg\\MailLog\\clients\\clientList.csv"))
                {
                    string line;
                    string[] row;
                    int intY = 1;
                    int intX = 0;
                    string strChkClient = "";
                    List<string[]> parsedData = new List<string[]>();
                    while ((line = readFile.ReadLine()) != null)
                    {
                        row = line.Split(',');
                        parsedData.Add(row);
                        strChkClient = parsedData[intX][0];
                        if (strClient.Contains(strChkClient))
                        {
                            strClient = parsedData[intX][intY];
                        }
                        intX++;
                    }
                    intX--;
                    return strClient;
                }
                
            }
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Originally posted by OriginalPoster
    I have xxx, then do yyy then zzz happens. Here's my code ...
    Ok. You know what you have.
    What you don't have is a question. Nobody here knows why you posted this since you haven't asked anything. What is it you are looking for?
    I recommend you read the FAQ about How to ask a good question so the volunteers will be able to help you.


    What I'm trying to do is get two values back to the original section (ParseMessage). ..
    I don't understand. If you want to get two values passed *to* the ParseMessage method then just pass them when you call it.

    When you say "pass back" do you mean 'return'? What the method returns to the caller when it is done?

    Right now it passes back a single value into strClient
    It doesn't return anything: It's return type is void.

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      You can use out parameters if you like...

      for example...
      Code:
      public bool GetParams(string source, out int val1, out int val2)
      {
          bool success = false;
          val1 = -1; val2 = -1;
      
          if (source != null)
          {
              string[] tokens = source.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
              if (tokens.Length >= 2)
              {
                  if (int.TryParse(tokens[0].Trim(), out val1) && int.TryParse(tokens[1].Trim(), out val2))
                  {
                      success = true;
                  }
              }
          }
      
          return success;
      }
      Code:
      int a, b;
      if (GetParams("3, 5", out a, out b))
      {
          Console.WriteLine(string.Format("Success! a = {0}, b = {1}", a, b));
      }
      else
      {
          Console.WriteLine("Failure!");
      }
      Will something like that work for you?

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        If that is your goal, to have the ParseMessage method return values then you can also just change the return type of the method from void to an int[], and return those values.

        Code:
        [HIGHLIGHT]void[/HIGHLIGHT] ParseMessage(string strCompleteMsg, string strCheckForSubject)
        what I really need is for it to pass back the values of:
        parsedData[intX][intY] ----- in this case intY would equal 1
        parsedData[intX][intY] ----- in this case intY would equal 2
        Well... your parsedData[][] method returns a string, as we see from this line:
        Code:
        strClient = parsedData[intX][intY];
        Which is already being returned.
        Code:
        return strClient;
        Maybe I'm just really thick today, but I can't figure out with "it" you need to return which values of what types - that you don't already have getting returned.

        Comment

        • temlehdrol
          New Member
          • Feb 2010
          • 5

          #5
          I'm sorry I guess in my in-experience I though I was clear in what I was asking.
          I'll describe what I need to happen perhaps that will help.
          I have a CSV file with client names, file name, number of fields. I need to search though that file and hopefully find a match (col 1), then I need to return the file name (col 2) and number of fields in that file (col 3). As of right now I've only gotten it to work to the point of it, opens the file searches col 1 and returns the value of col 2 into strClient. I have tried making strClient an array and list and have gotten all kinds of errors, now I'm sure I was doing it wrong and if it would make it easier I'll be happy to spend some time redoing what caused the errors and posting them.

          Now as you will see on Line 13 of the code I have a second call to a similar function (that I did not add in a effort to save space), that one will work in the same way but will be returning on average 4+ "cells" of data. However I figure if I can get the first one working then I can apply a similar fix to the second, thus the reason I left that part out.

          Comment

          Working...