You must Sign In to post a response.
  • Category: ASP.NET

    How to write a method having multiple input and output parameters in C#?


    Are you looking for information on Onclick event? Want to know how to write a method having multiple input and output parameters in C#? Read this thread to learn more about Onclick event with examples.



    I wanted to create a method in which I can pass subject marks as input para and to return total of all subject and percentage.How to do that?

    // Onclick event of button
    // call add(txtphysic.Text,txtchemistry.Text,txtBiology.Text)


    // public void Add(int physics,int chemistry,int bio)
    {
    int Total = physic+chemistry+bio
    float per = Total/6 *100
    }

    I know method can have multiple input para having different datatype.Method can return only one para.But if multiple output parameter exist,how to handle that?
  • #731231
    You can pass an araylist object to a mehtod and get the Total result from that as



    public decimal Add(ArrayList marks)
    {
    decimal total=0.0;
    foreach(Item a in Marks)
    {
    total = total + Convert.ToDecimal(a);
    }
    return total;
    }


    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #731245
    Hi Anil,thanks for ur reply.But I wanted to return 2 variable(called total and percentage) not single.

  • #731277
    Hi pinky,

    You can return Generic type as ArrayList,Hashtable simply use ArrayList

    Follow this



    public ArrayList Add(int physics,int chemistry,int bio)
    {
    ArrayList ar=new ArrayList();
    int Total = physic+chemistry+bio
    float per = Total/6 *100
    ar.Add(Total);
    ar.Add(per);
    return ar;
    }


    Regards
    Sekhar Babu,
    www.aspdotnet-sekhar.blogspot.in

  • #731282
    Hi Pinky,

    You can try with below code snippet.

    private void button1_Click(object sender, EventArgs e)
    {
    ArrayList AllMarks = new ArrayList();
    ArrayList Result = new ArrayList();
    AllMarks.Add(Convert.ToDecimal(textBox1.Text));
    AllMarks.Add(Convert.ToDecimal(textBox2.Text));
    Result = AddMarks(AllMarks); // calling function
    textBox3.Text = Convert.ToString(Result[0]);
    textBox4.Text = Convert.ToString(Result[1]);
    }

    public ArrayList AddMarks(ArrayList Marks)
    {
    decimal Total = 0, Percantage;
    ArrayList ar = new ArrayList();
    int c= Marks.Count;
    for (int i = 0; i < c; i++)
    {
    Total = Total + Convert.ToDecimal(Marks[i]);
    }
    Percantage = (Total / c) * 100;
    ar.Add(Total);
    ar.Add(Percantage);
    return ar;
    }


    Best Regards
    Bashar

  • #731397
    Do not try to pass multiple parameters to a method it will not feasible solution instead you can create an array, Arraylist or a structure and send it as parameters.
    In method defination you need to loop on the array and create a sum and avg the marks then by using a string with some separator you can return from function
    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


  • Sign In to post your comments