いつもお世話になております。
引数が多い場合、引数をクラスなどにまとめて渡すことは可能でしょうか?
以下にサンプルプログラムを用意しました。
azまでの変数を引数として渡すとき、いちいち「az」を書き込んでいくのは、大変でありプログラムも見づらくなってしまうと思います。改善案としましてはどのようなものがありますでしょうか?
ご指導ご鞭撻の程よろしくお願い致します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) { double sum = 0; float a1 = 1.5; int b1 = 2; int c1 = 3; int d1 = 4; int e1 = 5; int f1 = 6; float g1 = 7.5; // ... int z = 26 math(ref sum, a1, b1, c1,d1,e1,f1,g1 /* ... z */); } private void math (ref double sum, float a1, int b1, int c1, int d1, int e1, int f1, float g1 /* ... int z */) { sum = a1 + b1 + c1 + d1 + e1 + f1 + g1 /* ・・・ + z1 */; } }
}
※ 追記1
試行錯誤した結果、クラスを引数として渡せたと思うのですが、この方法はあっていますでしょうか?
他に最適な方法などありますでしょうか?
お手数おかけして申し訳ございませんが、再度ご教授願えませんでしょうか?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
class hensuu
{
public double sum;
public double a1; public int b1; public int c1; public int d1; public int e1; public int f1; public double g1; // ... int z = 26 } public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void Form1_Load(object sender, EventArgs e) { hensuu h = new hensuu(); h.a1 = 1.5; h.b1 = 2; h.c1 = 3; h.d1 = 4; h.e1 = 5; h.f1 = 6; h.g1 = 7.5; math(ref h); } private void math (ref hensuu h) { h.sum = h.a1 + h.b1 + h.c1 + h.d1 + h.e1 + h.f1 + h.g1 /* ・・・ + z1 */; } }
}

回答5件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/10/26 09:41