String array Function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sarabonn
    New Member
    • Nov 2008
    • 69

    String array Function

    hallo,

    I have a function which takes 2 values as input and i want to return that 2 values, so how should write the return statement to return 2 values of String array. I also i should retrieve these values in a button click event. here is my code.
    Code:
    public String newop(string userna,string pwd)
            {
               cwe_pro.GetFolderContentRequestType preq = new cwe_pro.GetFolderContentRequestType();
               cwe_pro.GetFolderContentReplyType pres = new cwe_pro.GetFolderContentReplyType();
                try
                {
                    preq.depth = 0;
                    preq.folderId = docid;
                    ws.Url = "http://www.cwe-projects.eu/bscw/bscw.cgi";
                    ws.Credentials = new System.Net.NetworkCredential(userna, pwd);
                    pres = ws.GetFolderContent(preq);
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(pres.folderContent);
                    XmlNodeList ni = doc.GetElementsByTagName("dcterms:modified");
                    st = ni[0].InnerText;
                    if (st.Equals(lmod))
                    {
                        
                        MessageBox.Show("No modification In the File");
                        textBox3.Visible = false;
                        groupBox3.Visible = true;
                       
                    }
                    else
                    {
                       
                        MessageBox.Show("Last modified by another user :" + st);
                        textBox3.Visible = false;
                        groupBox3.Visible = true;
                        
                       
                    }
                }
                catch (Exception e1)
                {
                    MessageBox.Show(e1.ToString());
    
                    return;
                }
          }
    
    this is my function and how should write the return statement that return a array of those two values username and pwd. i have retreive these 2 values usrname and pwd in button click event.
    
    private void button3_Click(object sender, EventArgs e)
            {
    }
    sorry iam newbie to c# i dont know this simple task itself. any idea ?..

    Thank you.
    Dinesh.
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    You could use "out" parameters. You declare two parameters and mark them with the keyword "out", then make sure you assign them before the end of the method:

    Code:
           public void newop(string userna, string pwd, out string resultString1, out string resultString2)
           {
               // make sure you assign the results to the two Out parameters before the method exits
               resultString1 = result1;
               resultString2 = result2;
           }
    You would then call the method like this:

    Code:
     public void Test()
           {
               string result1;
               string result2;
               newop("user", "pass", out result1, out result2);
           }
    Result1 and result2 now contain your return values.

    Alternatively, if you really want, you can return the results in a generic list (or another collection type):

    Code:
    public List<string> newop(string userna,string pwd)
               {
                   List<string> results = new List<string>();
                   results.Add(string1);
                   results.Add(string2);
                   return results;
               }

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      The last return would need to be
      Code:
      return results;

      Comment

      • nukefusion
        Recognized Expert New Member
        • Mar 2008
        • 221

        #4
        Originally posted by r035198x
        The last return would need to be
        Code:
        return results;
        Thanks r035198x, must have missed that. I've corrected the code sample.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by nukefusion
          Thanks r035198x, must have missed that. I've corrected the code sample.
          No problem.

          r035198x <-----Nitpicky .NET village idiot who knows nothing about .NET

          Comment

          • sarabonn
            New Member
            • Nov 2008
            • 69

            #6
            Originally posted by nukefusion
            You could use "out" parameters. You declare two parameters and mark them with the keyword "out", then make sure you assign them before the end of the method:

            Code:
                   public void newop(string userna, string pwd, out string resultString1, out string resultString2)
                   {
                       // make sure you assign the results to the two Out parameters before the method exits
                       resultString1 = result1;
                       resultString2 = result2;
                   }
            You would then call the method like this:

            Code:
             public void Test()
                   {
                       string result1;
                       string result2;
                       newop("user", "pass", out result1, out result2);
                   }
            Result1 and result2 now contain your return values.

            Alternatively, if you really want, you can return the results in a generic list (or another collection type):

            Code:
            public List<string> newop(string userna,string pwd)
                       {
                           List<string> results = new List<string>();
                           results.Add(string1);
                           results.Add(string2);
                           return results;
                       }
            HI nuke ,

            Thank you for ur reply. It work fine.

            Dinesh.

            Comment

            Working...