2012-06-30 185 views
1

好吧,我有這樣的代碼:訪問項目

URLs.Add(new URL(str.URL, str.Title, browser)); 

,這是URL類:

public class URL 
{ 
    string url; 
    string title; 
    string browser; 
    public URL(string url, string title, string browser) 
    { 
     this.url = url; 
     this.title = title; 
     this.browser = browser; 
    } 
} 

現在,我該如何訪問URL標題..?
即,URL的屬性[0] ...?當我打印URLs [0] .ToString時,它只給了我Namespace.URL。
如何打印URL類中的變量?

+1

你沒有給予足夠的信息,以便能夠回答這個問題。什麼樣的對象是「URL」? – freefaller

+0

@freefaller,他在標題中表示 - 列表。 – walther

回答

2

升級你的類公開公共屬性:

public class URL 
    { 
     public string Url { get; set; } 
     public string Title { get; set; } 
     public string Browser { get; set; } 
     public URL(string url, string title, string browser) 
     { 
      this.Url = url; 
      this.Title = title; 
      this.Browser = browser; 
     } 
    } 

然後訪問像這樣的屬性:

foreach(var url in URLs) 
{ 
    Console.WriteLine(url.Title); 
} 
2

有幾件事情 - 默認情況下,一個班級的所有成員都是私人的 - 這意味着他們不能被外部來電者訪問。如果你想他們是可用的,它們標記爲市民:

public string url; 

然後,你可以這樣做:

URLs[0].url; 

如果你想簡單的管時的結構,你可以重寫的ToString,加入像的方法如下:

public override string ToString() 
{ 
    return string.format("{0} {1} {2}", url, title, browser); 
} 

然後簡單地調用:

URLs[0].ToString();