看板 C_Sharp 關於我們 聯絡資訊
感謝chirsQQ的構想 現在正照您的想法實寫中,有遇到一小部份問題 請你再看一下 ※ 引述《chrisQQ (ChrisLiu)》之銘言: : public struct rectangleInfo : { : public Point topLeft { get; set; } : public Point topRight { get; set; } : public Point bottomLeft { get; set; } : public Point bottomRight { get; set; } : } 我在class的副函式外部寫上了上述結構宣告 出現了以下的error 錯誤3'DIB.MConnectedLabel.rectangleInfo.topRight.get' 不是標記成 abstract 或 extern,因此必須宣告主體 等好幾句 上網查了下,有人說是設定問題 我是stdio 2005版 sdk2.0 請問是我漏寫了什麼部份嗎? : 你的副程式 // 假裝是這樣 : public ArrayList calculateRectangle() : { : ArrayList resultArray = new ArrayList; : rectangleInfo rInfo = null; : // 計算座標 : // 建立 rInfo instance : rInfo = new rectangleInfo(); : rInfo.topLeft = new Point(1,1); : rInfo.topRight = new Point(1,1); : rInfo.bottomLeft = new Point(1,1); : rInfo.bottomRight = new Point(1,1); : // 將得到的座標加進 ArrayList : resultArray.Add(rInfo); : // 加入第二個矩形資料 : rInfo = new rectangleInfo(); : ... : ... : ... : // 最後回傳這個 ArrayList : return resultArray; : } 我的副函式片段如下 . . . ........ //前面有一些運算,以下是求每個長方形的4點座標 //ObjectNum表示長方形的個數目 ArrayList resultArray = new ArrayList(); rectangleInfo rInfo = null; //找每個物件的4點座標// for (int a = 0; a < ObjectNum; a++) { for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { //符合我要的條件就將取出這點座標 if (image_label[x, y] == LevelNew[a]) { TempX = x; MaxX = (TempX > MaxX) ? TempX : MaxX; //MaxX是x座標的最大值 MinX = (TempX < MinX) ? TempX : MinX; //MinX是x座標的最小值 TempY = y; MaxY = (TempY > MaxY) ? TempY : MaxY; //MaxY是Y座標的最大值 MinY = (TempY < MinY) ? TempY : MinY; //MinY是Y座標的最小值 } } } //參照您的方式來存x,y的座標----------------------------------------// // 建立 rInfo instance rInfo = new rectangleInfo(); rInfo.topLeft = new Point(MinX, MinY); rInfo.topRight = new Point(MaxX, MinX); rInfo.bottomLeft = new Point(MinX, MaxY); rInfo.bottomRight = new Point(MinX, MaxY); // 將得到的座標加進 ArrayList resultArray.Add(rInfo); //------------------------------------------------------------// //運算歸零 MaxY = 0; MinY = Height; MaxX = 0; MinX = Width; TempX = 0; TempY = 0; } return resultArray; 請問這樣可以將每個長方形座標都存進rInfo嗎? (共有ObjectNum個長方形,希望知道長方形誰是誰) : // 主程式或其他要接收此資料的類別 : public static void main() : { : ArrayList rectInfo = calculateRectangle(); : foreach (rectangleInfo rInfo in rectInfo) : { : // 取得矩形左上角的座標 rInfo.topLeft : } : } 如果我要在主函式取出每個長方形的座標 請問要如何一個一個分別取出不同的長方形呢? 是用for迴圈一個一個選出長方形,再提出座標嗎? : /* : foreach 的敘述可能有錯,我每次都跟 php 搞混ˊˋ,如果錯了可能就是 : foreach as。 : 也許可以為 struct rectangleInfo 加上一個 method 來一次設定所有點的值 : 例如: : public void setAllPoint(Point tl, Point tr, Point bl, Point br) : { : this.topLeft = tl; : this.topRight = tr; : // ... 以此類推 : } : 這樣似乎不用寫這麼多行 XD : */ 以上問題有點多 麻煩您的賜教~ 再次感謝~ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.117.163.249 ※ 編輯: awd 來自: 122.117.163.249 (01/02 20:15)
a761007:他的寫法是C# 3.0才有的 01/02 21:17
a761007:去找C#2.0 set/get寫法吧 01/02 21:17
remmurds:因為Point結構需要透過new關鍵字產生實體 如果你只有宣告 01/02 23:03
remmurds:public Point p { get; set; } 當然會產生編譯錯誤 01/02 23:05
remmurds:這跟.Net Framework是不是3.0版沒啥關聯 01/02 23:07
remmurds:也跟set/get寫法沒啥關連 C#的存取子向來都是長成那樣 01/02 23:09