Массив структур: нет доступа к элементам - C#
Формулировка задачи:
объявляю структуру:
затем в кнопке нужно обьявить массив структур и обратиться к ее элементам.
помогите мне это исправить...
namespace WindowsFormsApplication1 { public struct Mas { Complex ABC; delegate string InTo(Complex ABC); } public partial class Form1 : Form
private void button18_Click(object sender, EventArgs e) { Mas [] Dsa = new Mas[5]; Dsa[0]. //не могу вызвать элементы.. }
Complex - это отдельный класс.
Решение задачи: «Массив структур: нет доступа к элементам»
textual
Листинг программы
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { Complex A = new Complex(); Complex B = new Complex(); Complex C = new Complex(); delegate string toStr(Complex A); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { A = new Complex(Convert.ToDouble(textBox1.Text),Convert.ToDouble(textBox2.Text)); B = new Complex(Convert.ToDouble(textBox3.Text), Convert.ToDouble(textBox4.Text)); } private void button2_Click(object sender, EventArgs e) { C = A + B; richTextBox1.AppendText(Convert.ToString(C._Re)+"+"+Convert.ToString(C._Im)+"\n"); } private void button3_Click(object sender, EventArgs e) { C = A - B; richTextBox1.AppendText(Convert.ToString(C._Re) + "+" + Convert.ToString(C._Im) + "\n"); } private void button4_Click(object sender, EventArgs e) { C = A * B; richTextBox1.AppendText(Convert.ToString(C._Re) + "+" + Convert.ToString(C._Im) + "\n"); } private void button5_Click(object sender, EventArgs e) { C = A / B; richTextBox1.AppendText(Convert.ToString(C._Re) + "+" + Convert.ToString(C._Im) + "\n"); } private void button6_Click(object sender, EventArgs e) { ++A; richTextBox1.AppendText(Convert.ToString(A._Re) + "+" + Convert.ToString(A._Im) + "\n"); } private void button7_Click(object sender, EventArgs e) { ++B; richTextBox1.AppendText(Convert.ToString(B._Re) + "+" + Convert.ToString(B._Im) + "\n"); } private void button8_Click(object sender, EventArgs e) { --A; richTextBox1.AppendText(Convert.ToString(A._Re) + "+" + Convert.ToString(A._Im) + "\n"); } private void button9_Click(object sender, EventArgs e) { --B; richTextBox1.AppendText(Convert.ToString(B._Re) + "+" + Convert.ToString(B._Im) + "\n"); } private void button10_Click(object sender, EventArgs e) { double i = (double) A; richTextBox1.AppendText(Convert.ToString(i)+"/n"); } private void button11_Click(object sender, EventArgs e) { double i = Convert.ToDouble(textBox5.Text); Complex A = (Complex) i; richTextBox1.AppendText(Convert.ToString(A._Re) + "+" + Convert.ToString(A._Im)+"\n"); } private void button12_Click(object sender, EventArgs e) { double i = (double) B; richTextBox1.AppendText(Convert.ToString(i)+"\n"); } private void button13_Click(object sender, EventArgs e) { double i = Convert.ToDouble(textBox5.Text); Complex B = (Complex) i; richTextBox1.AppendText(Convert.ToString(B._Re) + "+" + Convert.ToString(B._Im) + "\n"); } private void button14_Click(object sender, EventArgs e) { if (A == B) richTextBox1.AppendText("True\n"); else richTextBox1.AppendText("False\n"); } private void button15_Click(object sender, EventArgs e) { if (A != B) richTextBox1.AppendText("True\n"); else richTextBox1.AppendText("False\n"); } private void button16_Click(object sender, EventArgs e) { if (A) System.Console.WriteLine("true"); else System.Console.WriteLine("false"); } private void button17_Click(object sender, EventArgs e) { toStr Sss = new toStr(A.toString); string str; str = Sss(A); richTextBox1.AppendText(str); Sss = new toStr(A.toReplace); str = Sss(B); richTextBox1.AppendText(str); } private void button19_Click(object sender, EventArgs e) { Mas[] Dsa = new Mas[5]; Dsa[0].ABC = A; Mas.InTo del = new Mas.InTo(A.toString); richTextBox1.AppendText("7"); } private void Form1_Load(object sender, EventArgs e) { } } } public struct Mas { public Complex ABC; public delegate string InTo(Complex ABC); } class Complex { private double Re; private double Im; public double _Re { get { return Re; } set { Re = value; } } public double _Im { get { return Im; } set { Im = value; } } public Complex() { Re = 5; Im = -7; } public Complex(double re, double im) { Re = re; Im = im; } public static Complex operator +(Complex A, Complex B) { return new Complex(A.Re + B.Re, A.Im + B.Im); } public static Complex operator -(Complex A, Complex B) { return new Complex(A.Re - B.Re, A.Im - B.Im); } public static Complex operator *(Complex A, Complex B) { return new Complex((A.Re * B.Re - A.Im * B.Im) / (B.Re * B.Re + B.Im * B.Im), (B.Im * A.Re + B.Re * A.Im) / (B.Re * B.Re + B.Im * B.Im)); } public static Complex operator /(Complex A, Complex B) { return new Complex(A.Re * B.Re + A.Im * B.Im, A.Re * B.Re - A.Im * B.Im); } public static Complex operator ++(Complex A) { return new Complex(A.Re + 1, A.Im); } public static Complex operator --(Complex A) { return new Complex(A.Re - 1, A.Im); } public static bool operator true(Complex A) { return (A.Re != 0) || (A.Im != 0); } public static bool operator false(Complex A) { return (A.Re == 0) && (A.Im == 0); } public static implicit operator Complex(double a) { return new Complex(a, 0); } public static explicit operator double(Complex A) { return Math.Sqrt(A.Re * A.Re + A.Im * A.Im); } public static bool operator ==(Complex A, Complex B) { if (A.Re == B.Re && A.Im == B.Im) return true; else return false; } public static bool operator !=(Complex A, Complex B) { if (A.Re != B.Re || A.Im != B.Im) return true; else return false; } public string toString(Complex A) { return ("z1 = " + Convert.ToString(A.Re) + " + " + Convert.ToString(A.Im) + "\n"); } public string toReplace(Complex A) { return ("z1 = " + Convert.ToString(A.Im) + " + " + Convert.ToString(A.Re) + "\n"); } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д