Article C0003 C# .NET
Generic, just an array of a struct

In the old days we used to describe an object using a struct, at present day this is still possible in C#. But the functionality using a struct is very limited in compare with a generic. For the MyRadio utility, which can be found at uTools, an array was required based on the following struct:

 typedef struct station{
 string name;
 string uriurl;
 }
 station stations[100];
The size of this array of stations is fixed and you have to maintain counters for the entries to retrieve values. By changing this array of structs into a class automatically changes its behavior and adds loads of functionality.

// Create a generic containing the station information:
public class station
{
private string _uriurl;
private string _name;

    public station(string name, string uriurl)
    {
        _name = name;
        _uriurl = uriurl;
    }
    public string name
    {
        get { return _name; }
        set { _name = value; }
    }
    public string uriurl
    {
        get { return _uriurl; }
        set { _uriurl = value; }
    }
}
The class exposes the name and uriurl variables, so that station data can be moved into the class object. The default declaration of a generic list will look like this: List myList = new List(); So to assign our class as object type we should declare the class as follows:

// New the class is defined create an Generic array using this new type:
List<station> radio_station = new List<station>();
Now we can use the generic as shown here:

// Access data:
lblStation.Text = radio_station[cbxStation.SelectedIndex].name;
// Add an item:
radio_station.Add(new station(myName, myUriurl));
Get the fully working Internet radio player within the Development area on this site. The player is using this code to keep the radio channel information.

~Edward