none
Using classes to store into arrays isn't working!!!?? RRS feed

  • Question

  •  

    I created a class and I am trying to store the class inside an array. It's an address book and it is suppose to use classes to get the entries' name, address, city, state, and zip code. I created the class AND the array but it just wont work.I have it set for 19, because the address book is supposed to hold 20 entries..Also, this is a Windows App. Here is the code, if anyone can help!

     

    Code Snippet

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.IO;

    using System.Text;

    using System.Windows.Forms;

    namespace MyAddressBook

    {

    public partial class addForm : Form

    {

    public addForm()

    {

    InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)

    {

    }

    public class entryInfo

    {

    public string name,

    address,

    city,

    state,

    zip;

     

    public entryInfo()

    {

    }

    public entryInfo(string entName, string entAddress,

    string entCity, string entState, string entZip)

    {

    name = entName;

    address = entAddress;

    city = entCity;

    state = entState;

    zip = entZip;

    }

    public string getName

    {

    get

    {

    return name;

    }

    }

    public string getAddress

    {

    get

    {

    return address;

    }

    }

    public string getCity

    {

    get

    {

    return city;

    }

    }

    public string getState

    {

    get

    {

    return state;

    }

    }

    public string getZip

    {

    get

    {

    return zip;

    }

    }

    }

    private void btnSaveEntry_Click(object sender, EventArgs e)

    {

    string entName,

    entAddress,

    entCity,

    entState,

    entZip;

    StreamWriter oFil = null;

    try

    {

    entName = txtName.Text;

    entAddress = txtStrAddress.Text;

    entCity = txtCity.Text;

    entState = txtState.Text;

    entZip = txZipCode.Text;

    oFil = new StreamWriter("addBook.txt");

    oFil.WriteLine(entName);

    oFil.WriteLine(entAddress);

    oFil.WriteLine("{0}, {1}", entCity, entState);

    oFil.WriteLine(entZip);

    }

    catch (Exception exc)

    {

    MessageBox.Show(exc.Message);

    }

    finally

    {

    oFil.Close();

    }

    }

    private void menuOpenFile_Click(object sender, EventArgs e)

    {

    openFileDialog1.ShowDialog();

    }

    private void menuSaveFile_Click(object sender, EventArgs e)

    {

    saveFileDialog1.ShowDialog();

    }

    private void menuInfo_Click(object sender, EventArgs e)

    {

    MessageBox.Show("My Address Book\nDesigned and coded by\nRobert Nellums");

    }

    private void menuFile_Click(object sender, EventArgs e)

    {

    }

    }

    }

     

    Friday, February 15, 2008 6:07 PM

All replies

    1. Don't declare a class inside another class unless you really want that class to only be accessible through its parent class.  Make it a separate class, and put it in a separate file in your project.
    2. Don't give your property accessors a name like "getAddress".  Name it "Address".  Common conventions for naming the backing field are to either use lowercase or a prefix, e.g. "_Address".  You're using lowercase here, which is good, but...
    3. Make the fields backing your property accessors private.  If you make them public, there's no need to use property accessors at all, since anything that creates an instance of the class can access its fields.
    4. If you don't actually need entryInfo to be a class (because all you're doing is setting and getting its fields), make it a struct.
    5. You aren't creating an instance of the class.  Look up the new keyword.
    6. You aren't creating an array and storing the instance of the class in it.
    Monday, February 18, 2008 10:19 PM