c sharp ta oluşturulan class tan erişim problemi

Kodla Büyü

kade38

Üye
Mesajlar
484
iyi günler arkadaşlar,
c# ta class içerine özellik ve metod eklemeyi anlattım öğrencilerime. bir örnekte takıldım. uygulama 2 textboxtan alınan 2 değeri toplatıp geriye değer döndürmeden(değer döndürerek yapabiliyorum) metod içerisinde label3 e yazdırma işlemi. Ama olmadı. Kod Aşağıda;
////buton kısmı////
double say1 = double.Parse(textBox1.Text);
double say2 = double.Parse(textBox2.Text);
dortislem di = new dortislem();
di.top(say1, say2);
/////class kısmı/////
public class dortislem
{
public double sonuc;
public void top(double s1, double s2)
{
sonuc = s1 + s2;
Form2 yf = new Form2();
yf.label3.ForeColor = Color.Red;
yf.label3.Text = sonuc.ToString();
}
}
yardımcı olursanız sevinirim. Kolay gelsin
 
Kod:
        public Form1()
        {
            InitializeComponent();
            frm = this;
        }
        public static Form1 frm;
        private void button1_Click(object sender, EventArgs e)
        {
            double say1 = double.Parse(textBox1.Text);
            double say2 = double.Parse(textBox2.Text);
            dortislem di = new dortislem();
            di.top(say1, say2);

        }
        public class dortislem
        {
            public double sonuc;
            public void top(double s1, double s2)
            {
                sonuc = s1 + s2;
                frm.label1.ForeColor = Color.Red;
                frm.label1.Text = sonuc.ToString();

            }
        }
 
Metoda parametre olarak label gönderin. Yani (double s1, double d2, Label lb)
Metod içinde de lb. Text=sonuc. Tostrıng
 
Kod:
        public Form1()
        {
            InitializeComponent();
            frm = this;
        }
        public static Form1 frm;
        private void button1_Click(object sender, EventArgs e)
        {
            double say1 = double.Parse(textBox1.Text);
            double say2 = double.Parse(textBox2.Text);
            dortislem di = new dortislem();
            di.top(say1, say2);

        }
        public class dortislem
        {
            public double sonuc;
            public void top(double s1, double s2)
            {
                sonuc = s1 + s2;
                frm.label1.ForeColor = Color.Red;
                frm.label1.Text = sonuc.ToString();

            }
        }
hocam yardımınız için teşekkür ederim ama olmadı. Aşağıdaki hatayı verdi
MVX4nM.jpg
 
Hocam bir de form1 f=New form değil de
Form1 f = (Form1)Application.OpenForms["Form1"]; böyle kullanırsanız çalışabilir
 
Hocam Form2 yf = new Form2(); cikartip, yf yerine this.label3 diye denerseniz olabilir.
 
Geri
Üst