2013-04-17 21 views
1

爲隨後我得到一個錯誤: 可訪問性不一致:屬性類型「AudioDevices.Tracks.track.Time」比財產「AudioDevices.Tracks.track.length」C#的get/set交通不便錯誤

不易進入我不知道它是什麼,或者我該如何解決它。任何人都可以幫助我?

這是所有的代碼我有,[模板=類庫]:

namespace AudioDevices.Tracks 
{ 
public class Track 
{ 
    #region STRUCT   
    private int id; 
    private string name; 
    private string artist; 
    private string albumSource; 
    private Time length; 
    private category style; 
    public enum category{ 
     Ambient, Blues, Country, Disco, Electro, Hardcore, HardRock, HeavyMetal,   Hiphop, Jazz, Jumpstyle, 
     Klassiek, Latin, Other, Pop, Punk, Reggae, Rock, Soul, Trance, Techno 
    }; 
    #endregion 

    #region GET/SET   
    public int Id{ 
     get { return id; } 
     set { id = value; } 
    } 
    public string Name{ 
     get { return name; } 
     set { name = value; } 
    } 
    public string Artist{ 
     get { return artist; } 
     set { artist = value; } 
    } 
    public string AlbumSource{ 
     get { return albumSource; } 
     set { albumSource = value; } 
    } 
    public Time Length{ 
     set { length = value; } 
    } 

    public string DisplayTime 
    { 
     get { return length.ToString(); } 
    } 
    public category Style 
    { 
     get { return style; } 
     set { style = value; } 
    } 
    #endregion 

    #region TIME CONSTRUCTOR 
    struct Time 
    { 
     int seconds; 
     int minutes; 
     int hours; 

     public Time(int seconds) 
     { 
      this.seconds = seconds; 
      this.minutes = 0; 
      this.hours = 0; 
     } 

     public Time(int seconds, int minutes) 
     { 
      this.seconds = seconds; 
      this.minutes = minutes; 
      this.hours = 0; 
     } 

     public Time(int seconds, int minutes, int hours) 
     { 
      this.seconds = seconds; 
      this.minutes = minutes; 
      this.hours = hours; 
     } 

     public override string ToString() 
     { 
      return hours + ":" + minutes + ":" + seconds; 
     } 
    } 
    #endregion 

    #region TRACK CONSTRUCTOR 

    public Track(){  } 

    public Track(int id) 
    { 
     this.id = id; 
    } 

    public Track(int id, string name) 
    { 
     this.id = id; 
     this.name = name; 
    } 

    public Track(int id, string name, string artist) 
    { 
     this.id = id; 
     this.name = name; 
     this.artist = artist; 
    } 
    #endregion 

    #region GetLength 
    public string GetLength() 
    { 
     return length.ToString(); 
    } 

    public int GetLengthInSeconds(int seconds, int minutes, int hours){ 
     int SecondsToSeconds = seconds; 
     int MinutesToSeconds = minutes * 60; 
     int HoursToSeconds = hours * 3600; 

     int TotalSeconds = HoursToSeconds + MinutesToSeconds + SecondsToSeconds; 
     return TotalSeconds; 
    } 
    #endregion 



} 

}

+0

它已被修復!感謝大家的幫助! – user1859829

+0

供將來參考:http://stackoverflow.com/questions/3763612/default-visibility-for-c-sharp-classes-and-members-fields-methods-etc – Steve

+0

作爲供參考,*原因*這是一個錯誤在於其他一些類將能夠看到「長度」(因爲它是公開的)而不是「時間」(因爲它是私有的)。基本上,您的原始代碼表示:*任何人都可以通過傳遞Time對象來設置長度。什麼是時間對象?這是一個祕密! ::伸出舌頭:: * – Brian

回答

3

你已經有了一個公共物業位置:

public Time Length{ 
    set { length = value; } 
} 

。 ..但該屬性的類型是Time,這是一個私人類型:

struct Time { 
    ... 
} 

(它是私有的,因爲它是嵌套類型;如果它被聲明爲頂級類型,它將默認爲內部的,這仍然會有相同的問題。)

公共成員簽名不能引用私有或內部類型中任何地方的參數類型或返回類型。如果調用者在不同的程序集中,該成員根本不會有意義。

所以,解決方法是要麼使Time公共型(和我建議提取它同時頂級型)使Time的私人財產。

+0

好的,我要試一試。感謝您的快速回復 – user1859829

+0

哇,我改變它爲公共結構時間。它的工作。我終於知道問題是什麼。爲什麼有問題。謝謝! – user1859829

0

這可能是因爲您正在使用構造時間。

試圖改變你的代碼,如:

public Time Length{ 
     set { length = new Time(value); } 
    } 
0

MSDN;

類成員和struct成員,包括 嵌套類和結構的訪問級別,默認情況下私人

因此,您的Time結構默認爲private

在這一部分;

public Time Length 
{ 
    set { length = value; } 
} 

您要創建public財產private結構的類型。你不能那樣做。爲了修復它,

  • Length屬性訪問修飾符public更改爲private

  • 設置你的Time結構訪問修飾符來public