Konuyu Oyla:
  • Derecelendirme: 0/5 - 0 oy
  • 1
  • 2
  • 3
  • 4
  • 5
C# Metoda Parametre Geçme
#1
Kusuruma bakmayın C# ile ilgili olacak forum kurallarına aykırı ise konuyu silebilirsiniz.
Bizim Delphi de yaptığımız 
Function Topla(i1,i2:TEdit):string;
şu olayın karşılığı... Aslında denemek istediğim tam olarak şu

class Ogrenci
   {
       
       private string ogrenciadsoyad;
       private string bolum;
       private int ogrencino;

       public string Ogrenciadsoyad
       {
           get { return ogrenciadsoyad; }
           set { ogrenciadsoyad = value; }
       }   
       public string Bolum
       {
           get { return bolum; }
           set { bolum = value; }
       }
       public int Ogrencino
       {
           get { return ogrencino; }
           set
           {
               if (value < 0)
               {
                   ogrencino = 0;
               }
               else
               {
                   ogrencino = value;
               }
           }
       }
       public void ekle(ListBox listBox)
       {
           //this.listBox.Items.Add(Ogrenciadsoyad);
           //this.listBox.Items.Add(Bolum);
           //this.listBox.Items.Add(Ogrencino.ToString());
       }
   }


Ogrenci clas = new Ogrenci
           {
               Ogrenciadsoyad = "deneme",
               Bolum = "yok",
               Ogrencino = 15,
               //ekle(listBox1)         
           };

Consol örneğini Form üzerinde denemek istemiştim  Sad
Cevapla
#2
Listbox, tek kolona sahip nesne olduğundan öğrenci bilgilerini yan yana gösteremezsiniz. Grid veya Listview kullanmanız gerekiyor.
Sınıfa bir kurucu(consructor) metot eklenirse daha kullanışlı olur. Buna göre sınıf aşağıdaki gibi olur.

class Ogrenci
{

private string _ogrenciadsoyad;
private string _bolum;
private int _ogrencino;

public string Ogrenciadsoyad {
get { return _ogrenciadsoyad; }
set { _ogrenciadsoyad = Value; }
}

public string Bolum {
get { return _bolum; }
set { _bolum = Value; }
}

public int Ogrencino {
get { return _ogrencino; }
set { _ogrencino = value < 0 ? 0 : value; }
}

Ogrenci(int no, string adsoyad, string bolum)
{
_ogrencino = no;
_ogrenciadsoyad = adsoyad;
_bolum = bolum;
}

public void Ekle(ListBox listBox)
{
listBox.Items.Add(string.Format("Öğrenci No: {0}, Öğrenci Ad/Soyad: {1}, Bölüm: {2}", _ogrencino, _ogrenciadsoyad, _ogrencino));
}

}

Çağırmak için;

void NewOgrenci()
{
Ogrenci ogr = new Ogrenci(1, "Hayati C#", "Bilgisayar");
ogr.Ekle(this.Listbox1);
}
Cevapla
#3
@anemos teşekkürlerimi sunuyorum, çalıştıramadım ama olayı anladım (IDE yi ve kod düzenini tanımaya çalışıyorum) ayrıca "Öğrenci No: {0}" şu olayda güzelmiş sadece python da var sanıyordum.
Cevapla
#4
Rica ederim. Kullandığım string formatter eskisiydi. Yeni syntax daha güzel, kısa ve kullanışlı.

listBox.Items.Add($"Öğrenci No: {_ogrencino}, Öğrenci Ad/Soyad: {_ogrenciadsoyad}, Bölüm: {_ogrencino}")

Kolay gelsin...
Cevapla




Konuyu Okuyanlar: 1 Ziyaretçi