發表文章

目前顯示的是 7月, 2020的文章

C#內建class物件Math【超短筆記】-static class

int g =(Math.Max(12, 88)); MessageBox.Show(""+g); //前兩句可縮短為:MessageBox.Show("" + Math.Max(12,88)); MessageBox.Show("" + Math.Min(Math.Min(80,100),69)); 上面程式碼是運用內建class【Math】來做數學等等一些功能,Math這個class內含有許多數學式的method可以運用喔。 這些內建的或者其他人寫好的class通常我們是不會去更改其內容,運用這些已寫好的功能來加速程式撰寫的效率,一般來說使用者是不會去更改其內容,所以在已查到的資料中一般來說這些也是一種static class。

C#運用宣告static的變數與class

編輯中.... 【關於static變數】 Static變數在當個class程式碼中都會共用同一個變數,簡單說,初始static變數為10,在該變數未修改時,若程式運作中有運用到該變數都是一樣為10,若在運作中去修改該值,則在修改數值後的程式碼都會變成修改後的數值,有點像是C的全域變數 成績資料判斷及格分數為例: 主程式:             passscore = 40; //刪去此行則會變成60及格             if (A.pass(Int32.Parse(textBox1.Text)))             {                 MessageBox.Show("pass");                 //x.Text="pass";(建置label x)             }             else             {                 MessageBox.Show("false");                 //x.Text="false";(建置label x)             } Class物件程式碼:  public int score;  public static int passscore = 60; //初始設定60及格  public bool pass(int score)         {             if (score >= passscore)             {                 return true;             }             else             {                 return false;             }         } 【關於static class】

C#運用宣告static的method【含Convert.ToString(int to string)】

這一篇道理很簡單,但搞懂的過程稍微複雜,但不難懂,多思考多看兩遍即可。 本篇主要說明: 運用static就可以不用新增物件即可使用class裡面的method。 static method運用 要運用到新增的物件 ( Class ) ,一定要記得新增一個 class 。 【 (class name)  ( 自定義 name) = new (class name) 】 例如比大小,建立一個叫做 compare 的 class ,然後在主程式 (Form1) 建立,程式碼如下: compare integer = new compare(); 然後建立一個可存取數值的變數 ( 此變數取值於物件的回傳值 ) ,再呼叫 compare 的 method ,傳遞比較的數值給 class(compare) 中的 max 函式。 int c = integer.max (   Int32.parse(textBox1.Text)  , Int32.parse(textBox2.text)   ) 如何可以省去每次都要建立新的物件呢? 【省略 compare integer = new compare(); 】 運用static在宣告method的地方,就可以直接呼叫class裡面的method出來用了。 主程式傳遞與呼叫程式碼: int c = compare.max (Int32.Parse(textBox1.Text), Int32.Parse(textBox2.Text)); class物件method程式碼: public static int max(int a,int b) 物件 (compare) 的 method(max) 接收到 Form1 傳遞的兩個數值後,去進行比較,然後 return 一個值回 Form1 。 public int static max(int a , int b) { int c; If(a>=b) { c = a; } Else { c = b; } Return c; }   ****若沒有用static來 宣告 method時 則 為 public int max(in

C#自我程式複習

圖片
基本的資料介面輸入與輸出資料。 主程式: private void button1_Click(object sender, EventArgs e)         {             if (string.IsNullOrEmpty(namebox.Text) || string.IsNullOrWhiteSpace(textBox2.Text) || string.IsNullOrWhiteSpace(textBox3.Text) || string.IsNullOrWhiteSpace(textBox4.Text))             {                 MessageBox.Show("請再次確認資料");             }             else             {                 Student_data A1 = new Student_data(namebox.Text, (int)numericUpDown1.Value, (int)numericUpDown2.Value, (int)numericUpDown3.Value, (int)numericUpDown4.Value,                     (int)numericUpDown6.Value, (int)numericUpDown5.Value, Int32.Parse(textBox2.Text), Int32.Parse(textBox3.Text), Int32.Parse(textBox4.Text));                 MessageBox.Show(A1.result(), "成績查詢");                             }         }         private void button2_Click(object sender, EventArgs e)         {             if (string.IsNullOrEmpty(namebox.Text) || string.IsNullOrWhiteSpace(textBo