C# public variable hatası nasıl çözülür?

Beta64

Centipat
Katılım
27 Aralık 2021
Mesajlar
43
Daha fazla  
Cinsiyet
Erkek
bu gormus oldugunuz kodda line 4'deki kodu her defa hesapliyor. Bunu disari cikartip public static yazdigimda problem veriyor. Onerileriniz nedir?

IMG-20220708-WA0001.jpg
 
Son düzenleyen: Moderatör:
Muthis konua acma sekli. Verilen hata yok. Kod butun olarak yok. Nasıl yardim etmemizi bekliyorsunuz?
 
C#:
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 WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        private int moveborder;
        public Form1()
        {
            InitializeComponent();
            moveborder = (Screen.PrimaryScreen.Bounds.Width - this.Size.Width) / 2 + button1.Width / 2;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (0 < Control.MousePosition.X - moveborder && Control.MousePosition.X - moveborder < 920)
            {
                button1.Location = new Point(Control.MousePosition.X - moveborder, button1.Location.Y);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

}
}
 
1 defa hesaplanmasını istiyorsanız Case kullanın.
Bir global int atayın 0 değerli.
Case 0 iken hesaplasın ve int değere 1 eklesin. O int değer 1 olunca Case için değer olmayacağı için daha fazla işlem yapmayacak.

Kod
int++;
Break;
gibi.

@pottie4r Konuyu anlayamadıysan neye disslike atıyorsunuz?
Adam sadece 1 defa hesaplanmasını istiyor değerin. En basit öneriyi yaptım.
 
Son düzenleme:

Geri
Yukarı