c# 如何在 declare 每個 variable 時執行同一動作

例如
    float leftBtnL = 0.02f * Screen.width;
    float topBtnL = 0.12f * Screen.width;
    float widthBtnL = 0.1f * Screen.width;
    float heightBtnL = 0.04f * Screen.width;

點先可以唔使每次都打 * Screen.width?
如果唔得的話可唔可以 declare 左之後先一次過  * Screen.width?
thanks

用array同for loop / foreach loop

TOP

但 array 只可用 int 唔可以用 string 做 index
我改完點對應返個 variable name?

TOP

回覆 1# bennetng
寫 utility function..
  1. float leftBtnL = scaleScreenWidth(0.02f);
  2. float topBtnL = scaleScreenWidth(0.12f);
  3. float widthBtnL = scaleScreenWidth(0.1f);
  4. float heightBtnL = scaleScreenWidth(0.04f);
複製代碼
冇得懶O既...

TOP

但 array 只可用 int 唔可以用 string 做 index
我改完點對應返個 variable name?
bennetng 發表於 2015-1-12 17:25


public const int LEFT = 0;
public const int TOP = 1;
public const int WIDTH = 2;
public const int HEIGHT = 3;

?

如果真係要用string做key, 你要用Dictionary
但係會慢好多

TOP

本帖最後由 snoopy11hk 於 2015-1-12 21:30 編輯
回覆  bennetng
寫 utility function..冇得懶O既...
LoneGumMan 發表於 2015-1-12 17:51



    有
float a = w(0.2f)
float b = w(0.4f)
etc.....

之後就refactor

TOP

Declare Vector Type?

TOP

Declare Vector Type?
燕飛 發表於 2015-1-12 21:51



    唔係
其實可以諗下 function refactor 呢樣野黎慳功夫

TOP