C# 委托和图形
C# delegates and graphics
我正在解决一个问题,我需要制作一个程序,让您可以从多个功能中进行选择;提供设置系数 a、b 和 c 的值以及参数 x 的起始值和结束值以及步长的能力。单击 Table 按钮后,将显示 table 个参数和函数值。
并且它必须以每个函数都是一个单独的子程序的方式进行。并创建一个功能选项卡方法,以便它可以将所选功能作为参数传递。
我取得了一些进展,但我遇到了一个错误 - “不可调用成员 'Form1.b' 不能像方法一样使用。”而且我一辈子都弄不明白我做错了什么。
目前的代码:
namespace MD2._5
{
public partial class Form1 : Form
{
int step;
double xEnd, xBegin;
double a, b, c;
public Form1()
{
InitializeComponent();
}
public delegate double DY(double a, double b, double c, int x); //declares delegate
public static double Y0(double a, double b, double c, int x) //function to calculate the result
{
return a * (x * x) + (b * x + c);
}
public static double Y1(double a, double b, double c, int x)
{
return (a / (x * x)) + (b / x + c);
}
public static double Y2(double a, double b, double c, int x)
{
return ((a * x) + b) / ((a * x) + c);
}
public DY[] Y = new DY[3] { Y0, Y1, Y2 }; //Creates a array of the functions above
private void radioButton1_Click(object sender, EventArgs e)
{
int buttonPressed = Convert.ToInt32(((RadioButton)sender).Tag);
DoTable(Y[buttonPressed]);
}
private void DoTable(DY y) //This will print the table
{
richTextBox1.Clear();
richTextBox1.AppendText(" x y ");
double howmanylines = xEnd + xBegin;
for (double x = howmanylines; x <= howmanylines; x += step)
{
richTextBox1.AppendText("\n " + x.ToString() + "\t" + y(x).ToString()); //error appearing here
}
}
private void step_TextChanged(object sender, EventArgs e) //sets the step int
{
step = Convert.ToInt32(stepText.Text);
}
private void x_end_TextChanged(object sender, EventArgs e)//Sets x end
{
xEnd = Convert.ToInt32(x_end.Text);
}
private void x_begin_TextChanged(object sender, EventArgs e) //sets x begin
{
xBegin = Convert.ToInt32(x_begin.Text);
}
private void textBoxA_TextChanged(object sender, EventArgs e) //sets a value
{
a = Convert.ToInt32(textBoxA.Text);
}
private void textBoxB_TextChanged(object sender, EventArgs e) //sets b value
{
b = Convert.ToInt32(textBoxB.Text);
}
private void textBoxC_TextChanged(object sender, EventArgs e) //sets c value
{
c = Convert.ToInt32(textBoxC.Text);
}
最终结果基本上应该是这样的(由于程序现在没有启动,因此绘制了数字):
第二部分是生成 table 的图形版本,但那是以后的事了,因为我还不知道如何着手去做那个..
非常感谢任何指点、批评或建议。
谢谢
'Non-invocable member 'Form1.b' cannot be used like a method.'
您收到此错误是因为在下面的行中
richTextBox1.AppendText("\n " + x.ToString() + "\t" + y(x).ToString());
当您将委托 DY 作为 y(x) 调用时,即通过传递一个参数,但委托 DY 需要 4 个参数 a、b、c 和 x,因为它找不到 b,因此会抛出该错误。
我正在解决一个问题,我需要制作一个程序,让您可以从多个功能中进行选择;提供设置系数 a、b 和 c 的值以及参数 x 的起始值和结束值以及步长的能力。单击 Table 按钮后,将显示 table 个参数和函数值。
并且它必须以每个函数都是一个单独的子程序的方式进行。并创建一个功能选项卡方法,以便它可以将所选功能作为参数传递。
我取得了一些进展,但我遇到了一个错误 - “不可调用成员 'Form1.b' 不能像方法一样使用。”而且我一辈子都弄不明白我做错了什么。
目前的代码:
namespace MD2._5
{
public partial class Form1 : Form
{
int step;
double xEnd, xBegin;
double a, b, c;
public Form1()
{
InitializeComponent();
}
public delegate double DY(double a, double b, double c, int x); //declares delegate
public static double Y0(double a, double b, double c, int x) //function to calculate the result
{
return a * (x * x) + (b * x + c);
}
public static double Y1(double a, double b, double c, int x)
{
return (a / (x * x)) + (b / x + c);
}
public static double Y2(double a, double b, double c, int x)
{
return ((a * x) + b) / ((a * x) + c);
}
public DY[] Y = new DY[3] { Y0, Y1, Y2 }; //Creates a array of the functions above
private void radioButton1_Click(object sender, EventArgs e)
{
int buttonPressed = Convert.ToInt32(((RadioButton)sender).Tag);
DoTable(Y[buttonPressed]);
}
private void DoTable(DY y) //This will print the table
{
richTextBox1.Clear();
richTextBox1.AppendText(" x y ");
double howmanylines = xEnd + xBegin;
for (double x = howmanylines; x <= howmanylines; x += step)
{
richTextBox1.AppendText("\n " + x.ToString() + "\t" + y(x).ToString()); //error appearing here
}
}
private void step_TextChanged(object sender, EventArgs e) //sets the step int
{
step = Convert.ToInt32(stepText.Text);
}
private void x_end_TextChanged(object sender, EventArgs e)//Sets x end
{
xEnd = Convert.ToInt32(x_end.Text);
}
private void x_begin_TextChanged(object sender, EventArgs e) //sets x begin
{
xBegin = Convert.ToInt32(x_begin.Text);
}
private void textBoxA_TextChanged(object sender, EventArgs e) //sets a value
{
a = Convert.ToInt32(textBoxA.Text);
}
private void textBoxB_TextChanged(object sender, EventArgs e) //sets b value
{
b = Convert.ToInt32(textBoxB.Text);
}
private void textBoxC_TextChanged(object sender, EventArgs e) //sets c value
{
c = Convert.ToInt32(textBoxC.Text);
}
最终结果基本上应该是这样的(由于程序现在没有启动,因此绘制了数字):
第二部分是生成 table 的图形版本,但那是以后的事了,因为我还不知道如何着手去做那个..
非常感谢任何指点、批评或建议。
谢谢
'Non-invocable member 'Form1.b' cannot be used like a method.'
您收到此错误是因为在下面的行中
richTextBox1.AppendText("\n " + x.ToString() + "\t" + y(x).ToString());
当您将委托 DY 作为 y(x) 调用时,即通过传递一个参数,但委托 DY 需要 4 个参数 a、b、c 和 x,因为它找不到 b,因此会抛出该错误。