Click here to Skip to main content
16,014,392 members
Home / Discussions / C#
   

C#

 
AnswerRe: Beginners help with classes Pin
musefan2-Dec-10 3:13
musefan2-Dec-10 3:13 
GeneralRe: Beginners help with classes Pin
kruegs352-Dec-10 3:28
kruegs352-Dec-10 3:28 
GeneralRe: Beginners help with classes Pin
musefan2-Dec-10 3:38
musefan2-Dec-10 3:38 
GeneralRe: Beginners help with classes Pin
PIEBALDconsult2-Dec-10 8:25
mvePIEBALDconsult2-Dec-10 8:25 
AnswerRe: Beginners help with classes Pin
#realJSOP2-Dec-10 3:21
professional#realJSOP2-Dec-10 3:21 
GeneralRe: Beginners help with classes Pin
kruegs352-Dec-10 3:45
kruegs352-Dec-10 3:45 
GeneralRe: Beginners help with classes Pin
Jeff Connelly2-Dec-10 4:53
Jeff Connelly2-Dec-10 4:53 
GeneralRe: Beginners help with classes Pin
DaveyM692-Dec-10 9:30
professionalDaveyM692-Dec-10 9:30 
kruegs35 wrote:
When I think of organizing a list of people that have first names, last names, phone numbers, I think of a database table. I can store all the data in a table and query it to display what I need in a datagridview or whatever.


Of course - and that is fine! The DataGridView (a class itself) will hold the data for you in memory. However, to be able to reuse that data it can be far easier to have a proper representation of the data by using classes.
C#
public enum PhoneNumberType
{
    Home,
    Mobile,
    Work
}

public class PhoneNumber
{
    private string number;
    private PhoneNumberType type;

    public PhoneNumber(string number, PhoneNumberType type)
    {
        // validate here
        this.number = number;
        this.type = type;
    }

    public string Number
    {
        get{ return number; }
    }
    public PhoneNumberType Type
    {
        get{ return type; }
    }
}

public class Person
{
    private string forename;
    private string surname;
    private List<PhoneNumber> numbers;

    public Person(string forename, string surname, IEnumerable<PhoneNumber> numbers)
    {
        // validate here
        this.forename = forename;
        this.surname = surname;
        if(numbers != null)
            this.numbers = new List<PhoneNumber>(numbers);
        else
            this.numbers = new List<PhoneNumber>();
    }

    public string Forename
    {
        get{ return forename; }
    }
    public string Surname
    {
        get{ return surname; }
    }
    public List<PhoneNumber> Numbers
    {
        get{ return numbers; }
    }
}

public class People : List<Person>
{
    // ToDo
}

Now by using this structure in memory it's easy to access any part of any person in your one collection - People - without having to requery or try and extract from the DataGridView. Any of these classes can have Load, Save, Update, Delete etc methods which can access your database, or better still an interface that represents a database so you can have different implementations of the interface for dfferent database types if ever required... but you should get to grips with the reasoning for classes before exploring interfaces!

BTW, the above code was just typed here and not copied from VS so is untested and may contain errors!
Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



AnswerRe: Beginners help with classes Pin
V.2-Dec-10 23:31
professionalV.2-Dec-10 23:31 
QuestionExecuteNonQuery Problem Pin
Erdinc272-Dec-10 1:40
Erdinc272-Dec-10 1:40 
AnswerRe: ExecuteNonQuery Problem PinPopular
Hiren solanki2-Dec-10 1:46
Hiren solanki2-Dec-10 1:46 
GeneralRe: ExecuteNonQuery Problem Pin
Eddy Vluggen2-Dec-10 1:53
professionalEddy Vluggen2-Dec-10 1:53 
GeneralRe: ExecuteNonQuery Problem Pin
Erdinc272-Dec-10 2:05
Erdinc272-Dec-10 2:05 
AnswerRe: ExecuteNonQuery Problem Pin
musefan2-Dec-10 1:47
musefan2-Dec-10 1:47 
GeneralRe: ExecuteNonQuery Problem Pin
musefan2-Dec-10 6:25
musefan2-Dec-10 6:25 
QuestionError regarding crystal report? Pin
Tridip Bhattacharjee2-Dec-10 1:31
professionalTridip Bhattacharjee2-Dec-10 1:31 
AnswerRe: Error regarding crystal report? Pin
Hiren solanki2-Dec-10 1:50
Hiren solanki2-Dec-10 1:50 
QuestionWinForms VS. WebForms Pin
treuveni2-Dec-10 1:24
treuveni2-Dec-10 1:24 
AnswerRe: WinForms VS. WebForms Pin
Hiren solanki2-Dec-10 1:32
Hiren solanki2-Dec-10 1:32 
AnswerRe: WinForms VS. WebForms Pin
Sathesh Sakthivel2-Dec-10 1:50
Sathesh Sakthivel2-Dec-10 1:50 
GeneralRe: WinForms VS. WebForms Pin
Hiren solanki2-Dec-10 1:57
Hiren solanki2-Dec-10 1:57 
GeneralRe: WinForms VS. WebForms Pin
Ankur\m/2-Dec-10 2:44
professionalAnkur\m/2-Dec-10 2:44 
GeneralRe: WinForms VS. WebForms Pin
Chris Maunder2-Dec-10 12:24
cofounderChris Maunder2-Dec-10 12:24 
QuestionC# window app to listen multiple port simultaneously.. Pin
ayandelhi1-Dec-10 23:41
ayandelhi1-Dec-10 23:41 
AnswerRe: C# window app to listen multiple port simultaneously.. Pin
Luc Pattyn2-Dec-10 0:38
sitebuilderLuc Pattyn2-Dec-10 0:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.