2013-04-12 88 views
1

我正在創建一個項目,該項目將使用streamreader讀取數據文件,並在Windows窗體中顯示數據。關於c#類與圖像的混淆

但是,該數據文件的其中一個屬性是圖像。由於我從來沒有真正使用過圖像,在c#中,我有點困惑。

做我創建了一個名爲team類和管理,建立Imageusing System.Drawing;的聲明,但是我不知道我是多麼應該建立在類的構造函數與圖像的一些現有的研究或獲取/設置方法。

我設法在網上找到的一般用途和Image類的提示沒有把它放在你自己創建的類中。這是我到目前爲止。

using System; 
using System.Collections.Generic; 
using System.Collections; 
using System.Linq; 
using System.Text; 
using System.Drawing; 

namespace teams_2 
{ 
class Team 
{ 
    string teamName; 
    string teamLeague; 
    string teamManager; 
    string teamNickname; 
    string teamStadium; 
    int leaguePosistion; 
    int leaguePoints; 
    int gamesPlayed; 
    int goalDifference; 
    int playerNumber; 
    Image teamLogo; //the problem 

    //constructor(image?) 
    //team order = teamName teamLeague teamManager teamNickname teamStadium leaguePosistions leaguePoints gamesPlayed goalDifference playerNumber teamLogo 

    public Team(string inTeamName, string inTeamLeague, string inTeamManager, string inTeamNickname, 
       string inTeamStadium, int inLeaguePosistion, int inLeaguePoints, int inGamesPlayed, 
       int inGoalDifference, int inPlayerNumber) 
    {// 
     teamName = inTeamName; 
     teamLeague = inTeamLeague; 
     teamManager = inTeamManager; 
     teamNickname = inTeamNickname; 
     teamStadium = inTeamStadium; 
     leaguePosistion = inLeaguePosistion; 
     leaguePoints = inLeaguePoints; 
     gamesPlayed = inGamesPlayed; 
     goalDifference = inGoalDifference; 
     playerNumber = inPlayerNumber; 

    }//end constructor 
    //begin getters 
    public string getTeamName() 
    { 
     return teamName; 
    } 
    public string getTeamLeague() 
    { 
     return teamLeague; 
    } 
    public string getTeamManager() 
    { 
     return teamManager; 
    } 
    public string getTeamNickname() 
    { 
     return teamNickname; 
    } 
    public string getTeamStadium() 
    { 
     return teamStadium; 
    } 
    public int getLeaguePosistion() 
    { 
     return leaguePosistion; 
    } 
    public int getLeaguePoints() 
    { 
     return leaguePoints; 
    } 
    public int getGamesPlayed() 
    { 
     return gamesPlayed; 
    } 
    public int getGoalDifference() 
    { 
     return goalDifference; 
    } 
    public int getPlayerNumber() 
    { 
     return playerNumber; 
    } 
    //end getters 

    //begin setters 
    public void setTeamName(string inTeamName) 
    { 
     teamName = inTeamName; 
    } 
    public void setTeamLeague(string inTeamLeague) 
    { 
     teamLeague = inTeamLeague; 
    } 
    public void setTeamManager(string inTeamManager) 
    { 
     teamManager = inTeamManager; 
    } 
    public void setTeamNickname(string inTeamNickname) 
    { 
     teamNickname = inTeamNickname; 
    } 
    public void setTeamStadium(string inTeamStadium) 
    { 
     teamStadium = inTeamStadium; 
    } 
    public void setLeaguePosistion(int inLeaguePosistion) 
    { 
     leaguePosistion = inLeaguePosistion; 
    } 
    public void setLeaguePoints(int inLeagePoints) 
    { 
     leaguePoints = inLeagePoints; 
    } 
    public void setGamesPlayed(int inGamesPlayed) 
    { 
     gamesPlayed = inGamesPlayed; 
    } 
    public void setGoalDifference(int inGoalDifference) 
    { 
     goalDifference = inGoalDifference; 
    } 
    public void setPLayerNumber(int inPlayerNumber) 
    { 
     playerNumber = inPlayerNumber; 
    } 
    //end setters 

} 
} 

任何幫助將不勝感激。謝謝。

+2

你的getters/setters有點奇怪..你通常會使用像這樣的屬性:'公共字符串PropName {get {return _field; } set {_field = value; }}。在提到你的問題時,沒有理由不能像你對其他字段那樣在構造函數中包含'Image' obj。 – DGibbs

+2

嗨! C#有一些稱爲屬性(和自動屬性)的東西。這是你應該谷歌的東西(根本不涉及你的問題,但與你發佈的代碼有關) –

+1

你來自Java,不是嗎?我打電話告訴命名約定。我認爲最好使用屬性而不是getter和setter方法。 –

回答

1

你可能最好使用位圖類而不是圖像類,因爲它可以讓你從一個文件路徑

所以這裏引用圖像是我的解決方案

Image logo = new Bitmap("Path"); 

    public Image getlogo() 
    { 
     return logo; 

    } 

應該沒有需要一個二傳手,因爲它被設定,當它被宣佈

編輯我曾嘗試這種解決方案,它仍然有一個PNG甚至曾因此給它一個嘗試

希望這有助於享受

+0

謝謝你的回答。不幸的是,項目給我們的標識必須是.png,所以我認爲我不能使用它。我之前沒有澄清過這個錯誤。 – zac

+0

@zac:請閱讀文檔:http://msdn.microsoft.com/en-us/library/0cbhe98f.aspx位圖類可以閱讀PNG – leonbloy

+0

對不起,你措辭的方式讓我覺得不一樣哈哈。謝謝你的回答完美。 – zac