Acceder al elemento en la clase personalizada de la lista

Ok, tengo este código:

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

and this is the URL class:

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;
    }
}

Now, how do I access the URL title..?
ie., Property of URLs[0]...? When I print URLs[0].ToString, it just gives me the Namespace.URL.
How do I print the variables inside of the URL class?

preguntado el 30 de junio de 12 a las 14:06

You've not given enough information to be able to answer the question. What sort of object is URLs? -

@freefaller, he said it in the title - a list. -

2 Respuestas

Upgrade your class to expose public properties:

 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; 
        } 
    } 

Then access your properties like so:

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

Respondido el 30 de junio de 12 a las 14:06

A couple of things - by default, all members of a class will be private - meaning that they're not accessible to outside callers. If you wish them to be available, mark them as public:

public string url;

Entonces puedes hacer:

URLs[0].url;

If you wish to simple pipe-out the structure, you can override ToString, by adding a method like follows:

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

then simply call:

URLs[0].ToString();

Respondido el 30 de junio de 12 a las 14:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.