Click here to Skip to main content
15,899,679 members
Home / Discussions / C#
   

C#

 
QuestionString Arrays Pin
Darrall4-Mar-10 5:01
Darrall4-Mar-10 5:01 
AnswerRe: String Arrays Pin
PIEBALDconsult4-Mar-10 5:08
mvePIEBALDconsult4-Mar-10 5:08 
GeneralRe: String Arrays Pin
Darrall4-Mar-10 5:53
Darrall4-Mar-10 5:53 
GeneralRe: String Arrays Pin
PIEBALDconsult4-Mar-10 11:35
mvePIEBALDconsult4-Mar-10 11:35 
GeneralRe: String Arrays Pin
Darrall4-Mar-10 11:56
Darrall4-Mar-10 11:56 
AnswerRe: String Arrays Pin
harold aptroot4-Mar-10 5:17
harold aptroot4-Mar-10 5:17 
GeneralRe: String Arrays Pin
Darrall4-Mar-10 6:01
Darrall4-Mar-10 6:01 
AnswerRe: String Arrays [modified] Pin
OriginalGriff4-Mar-10 5:35
mveOriginalGriff4-Mar-10 5:35 
There are a number of things wrong here, not just that you get an error.
I assume you are new to C#?
Darrall wrote:
// PN is the index. I didn't set it to 0 because I want it to retain it's value
// It is at 0 the first time run.
public int PN;

It will retain it's value anyway. Setting the inital vaule with
public int PN = 0;
makes no difference at all. Each time you construct a Form1 object you will get a new PN, that starts at zero. If you want one PN that is shared by all instances of Form1, then make it static:
public static int PN = 0;

Don't declare fields as public unless you need them accessable outside the class, use private. You can easily create a public property which accesses teh field, which has the advantage that you can provide a getter only, and it becomes readonly to the outside world.

Darrall wrote:
ArrayList Projects = new ArrayList();

This creates Projects as local to the btnNew_Click method - i.e. it is discarded when you exit. Since you keep the PN as part of the class, I would suggest this should be also.

Darrall wrote:
// The error was directed at PN here.
Projects[PN] = textBoxAddNew.ToString();

Projects.Add(Projects[PN]);

textBoxAddNew.ToString() does not give you the data typed into the textbox - textBoxAddNew.Text does.
Projects[PN} does not exist. If it did, then these two statements would try to add the same string twice.
That is why you get the error message - you are trying to access an element that does not exist.

Don't use an ArrayList - use a List<String> instead - it restricts what you can add to the list to just strings (arrayList will take Strings, ints, doubles, anything).

Why do you bother with PN when the ArrayList and List both have a Count which is going to be exactly the same as PN anyway?

Why sort the Projects? Just set the Sorted property of your ListBox.

Why are you adding all the Projects strings to the List box each time?

Better way to do all this:

public partial class Form1 : Form
    {
    public Form1()
        {
        InitializeComponent();
        }
    private List<string> Projects = new List<String>();

    private void btnNew_Click(object sender, EventArgs e)
        {
        Projects.Add(textBoxAddNew.Text);
        listBox1.Items.Add(textBoxAddNew.Text);
        textBoxAddNew.Text = "";
        }
    }


[edit]Forgot the "encode "<" .. option when pasting. Again. Damnit![/edit]
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace

C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

modified on Thursday, March 4, 2010 11:45 AM

GeneralRe: String Arrays Pin
Darrall4-Mar-10 5:58
Darrall4-Mar-10 5:58 
Questioncombobox height? Pin
Pawan Kiran4-Mar-10 3:53
Pawan Kiran4-Mar-10 3:53 
QuestionRe: combobox height? Pin
Eddy Vluggen4-Mar-10 4:16
professionalEddy Vluggen4-Mar-10 4:16 
AnswerRe: combobox height? Pin
Pawan Kiran4-Mar-10 4:26
Pawan Kiran4-Mar-10 4:26 
AnswerRe: combobox height? Pin
Luc Pattyn4-Mar-10 4:19
sitebuilderLuc Pattyn4-Mar-10 4:19 
AnswerRe: combobox height? Pin
Covean4-Mar-10 4:39
Covean4-Mar-10 4:39 
GeneralRe: combobox height? [modified] Pin
Pawan Kiran4-Mar-10 18:49
Pawan Kiran4-Mar-10 18:49 
GeneralRe: combobox height? Pin
Covean4-Mar-10 20:41
Covean4-Mar-10 20:41 
GeneralRe: combobox height? [modified] Pin
Pawan Kiran4-Mar-10 21:50
Pawan Kiran4-Mar-10 21:50 
GeneralRe: combobox height? Pin
Covean4-Mar-10 22:31
Covean4-Mar-10 22:31 
GeneralRe: combobox height? Pin
Pawan Kiran4-Mar-10 22:53
Pawan Kiran4-Mar-10 22:53 
GeneralRe: combobox height? Pin
Covean4-Mar-10 23:14
Covean4-Mar-10 23:14 
GeneralRe: combobox height? Pin
Pawan Kiran4-Mar-10 23:45
Pawan Kiran4-Mar-10 23:45 
GeneralRe: combobox height? Pin
Covean5-Mar-10 1:06
Covean5-Mar-10 1:06 
GeneralRe: combobox height? Pin
Pawan Kiran5-Mar-10 1:43
Pawan Kiran5-Mar-10 1:43 
QuestionClass Not Registered Error (HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)) Pin
Clifford Anup4-Mar-10 3:36
Clifford Anup4-Mar-10 3:36 
AnswerRe: Class Not Registered Error (HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)) Pin
Dave Kreskowiak4-Mar-10 3:52
mveDave Kreskowiak4-Mar-10 3:52 

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.