An Array initializer of '20' is expected

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 30088 Views - Last Post: 13 August 2011 - 07:19 AM Rate Topic: -----

#1 Goff256   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

An Array initializer of '20' is expected

Posted 12 August 2011 - 08:45 AM

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Unit_10__Michael_Goff__IT_254
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            string[] ArrayString = new string[20]{txtFirst.Text, txtLast.Text,
												txtStreet.Text, txtCity.Text,
												txtState.Text, txtZip.Text};

            for (int myString = 0; myString < 20; myString++)
            {
                nameList.Items.Add(ArrayString);
            }
            foreach (string myString in ArrayString)
            {
                nameList.Items.Add(ArrayString);
            }

            nameList.Items.Add(txtFirst.Text + " " + txtLast.Text);
            nameList.Items.Add(txtStreet.Text);
            nameList.Items.Add(txtCity.Text + " " + txtState.Text + " "
                                       + txtZip.Text + "\r" + "\n");

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (nameList.SelectedIndex > -1)
                nameList.Items.RemoveAt(nameList.SelectedIndex);


        }
    }
}


Alright, this is it. This is the final project of my class. I'll admit that I worked with a friend already, and that's why I got most of it done. we just can't seem to get rid of this last error. To be quite honest, I don't know what it means.

Is This A Good Question/Topic? 0
  • +

Replies To: An Array initializer of '20' is expected

#2 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: An Array initializer of '20' is expected

Posted 12 August 2011 - 08:52 AM

I'm guessing that the problem is in line 20, since you didn't indicate where it was. You declare that the array is of length 20, but then only provide 6 values. If it's going to be length 20, you need to provide 20 values.

You also shouldn't hard code the value of 20 into the for loop, use .Length on the array to set the limit.

This post has been edited by Momerath: 12 August 2011 - 08:53 AM

Was This Post Helpful? 2
  • +
  • -

#3 Goff256   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

Re: An Array initializer of '20' is expected

Posted 12 August 2011 - 08:58 AM

Yes, the problem is in line twenty.

The 20 variables are the twenty sets of information. Those are merely the differing things for the address book.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: An Array initializer of '20' is expected

Posted 12 August 2011 - 09:13 AM

View PostGoff256, on 12 August 2011 - 09:45 AM, said:

This is the final project of my class.


How much did you pay for this course?
The reason I am asking is because if it was more than $50 you got ripped off.
For the same $50 you could have bought 1-2 good self-teaching books that would have gotten you a lot further than this, and probably in less time. The idea that you are still directly accessing the .Text property of GUI controls instead of using properties - at the end of an entire course - is just an appauling statement on the quality of the course curriculum and the teacher. I'm not blaming you. You are doing what you are taught. But any on-line course or half-decent book would have you well past this in the first month.

Now that the course is over, I highly recommend you take it upon yourself to work through some of the tutorials we offer right here on DIC and maybe drop by your local bookstore to pick up one of the recommended 'Teach yourself C# in 21 days' type books. You will be miles ahead of your class mates when the next school year starts!


Resources, references and suggestions for new programmers. - Updated Aug 2011
Spoiler

Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: An Array initializer of '20' is expected

Posted 12 August 2011 - 09:15 AM

Well, I'm not sure what you mean by "The 20 variables are the twenty sets of information. Those are merely the differing things for the address book", but you can't use an array initializer with a different length than the declared size of the array.

So you either add another 14 elements to your initializer, or you don't use one, and manually fill in the first six elements.
Was This Post Helpful? 1
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: An Array initializer of '20' is expected

Posted 12 August 2011 - 09:22 AM

for (int myString = 0; myString < 20; myString++)
            {
                nameList.Items.Add(ArrayString);
            }
            foreach (string myString in ArrayString)
            {
                nameList.Items.Add(ArrayString);
            }


WTF?

The first for loop presumes you have 20 items, and adds the entire ArrayString to nameList.Items.

The second array then goes through all the elements of ArrayString and again adds the entire ArrayString to nameList.Items. You probably mean to add myString not ArrayString.

But either way you are trying to add the same things to the nameList.Items a second time

And then you try to add the same information a third time.
            nameList.Items.Add(txtFirst.Text + " " + txtLast.Text);
            nameList.Items.Add(txtStreet.Text);
            nameList.Items.Add(txtCity.Text + " " + txtState.Text + " "


You've already added this earlier in the method when you added these textboxes to the ArrayString array.

Are you guys just throwing darts in the dark hoping to make something stick?
Was This Post Helpful? 1
  • +
  • -

#7 Goff256   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

Re: An Array initializer of '20' is expected

Posted 12 August 2011 - 09:49 AM

View PostCurtis Rutland, on 12 August 2011 - 09:15 AM, said:

Well, I'm not sure what you mean by "The 20 variables are the twenty sets of information. Those are merely the differing things for the address book", but you can't use an array initializer with a different length than the declared size of the array.

So you either add another 14 elements to your initializer, or you don't use one, and manually fill in the first six elements.


What I meant is this:

Each address is the address book is supposed to have that information (name, address, state, etc), and it is supposed to hold twenty addresses.

View PosttlhIn`toq, on 12 August 2011 - 09:22 AM, said:

for (int myString = 0; myString < 20; myString++)
            {
                nameList.Items.Add(ArrayString);
            }
            foreach (string myString in ArrayString)
            {
                nameList.Items.Add(ArrayString);
            }


WTF?

The first for loop presumes you have 20 items, and adds the entire ArrayString to nameList.Items.

The second array then goes through all the elements of ArrayString and again adds the entire ArrayString to nameList.Items. You probably mean to add myString not ArrayString.

But either way you are trying to add the same things to the nameList.Items a second time

And then you try to add the same information a third time.
            nameList.Items.Add(txtFirst.Text + " " + txtLast.Text);
            nameList.Items.Add(txtStreet.Text);
            nameList.Items.Add(txtCity.Text + " " + txtState.Text + " "


You've already added this earlier in the method when you added these textboxes to the ArrayString array.

Are you guys just throwing darts in the dark hoping to make something stick?


No, we're simply trying to put what we have learned into context in a final project. We both took our individual problems, which were not the ones that the other one had, and then tried to make one working set of code. This was, unfortunately, what ended up happening.

Alright, so the second and third are redundant? I should know this better, I know, and I'm sorry for coming across as such an idiot. I will follow your advice on the books, but I'm hoping you can help me fix this particular problem.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: An Array initializer of '20' is expected

Posted 12 August 2011 - 09:56 AM

View PostGoff256, on 12 August 2011 - 10:49 AM, said:

but I'm hoping you can help me fix this particular problem.


Happy to help. That's what we have been doing: Helping. By pointing out some problems.
But we won't do your homework for you. We won't write corrected code for you to turn in as your work/knowledge.

With that understanding, what can we do to help you?

This post has been edited by tlhIn`toq: 12 August 2011 - 09:56 AM

Was This Post Helpful? 1
  • +
  • -

#9 Goff256   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

Re: An Array initializer of '20' is expected

Posted 12 August 2011 - 10:00 AM

I am happy that you do not write the corrected code.
If I want the array to hold twenty sets of something, how would I go about setting up the array? That seems to be my main problem, the address book needs to hold twenty sets of information (first name, last name, street address, state, zip code) to be able to be called upon at a moment's notice.

So far, twenty in the array seems wrong and I don't exactly know what it is that would enable me to put several pieces of data in each array entry.
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: An Array initializer of '20' is expected

Posted 12 August 2011 - 10:11 AM

View PostGoff256, on 12 August 2011 - 11:00 AM, said:

If I want the array to hold twenty sets of something, how would I go about setting up the array?


Momerath already answered this for you. You are initializing it with 6 pieces of data when you define it as 20. You need to provide 14 more contacts.

View PostMomerath, on 12 August 2011 - 09:52 AM, said:

I'm guessing that the problem is in line 20, since you didn't indicate where it was. You declare that the array is of length 20, but then only provide 6 values. If it's going to be length 20, you need to provide 20 values.

You also shouldn't hard code the value of 20 into the for loop, use .Length on the array to set the limit.


View PostGoff256, on 12 August 2011 - 11:00 AM, said:

If I want the array to hold twenty sets of something, how would I go about setting up the array?


I'm sorry if this sounds condicending but...
Open your text book. Find the section on arrays.
Tell me what it is telling you to do to set up the array. Really. Type the textbook description here.

Then tell me what part of that you don't understand and we will try to get you 'unlost' about what you learned.

This post has been edited by tlhIn`toq: 12 August 2011 - 10:10 AM

Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: An Array initializer of '20' is expected

Posted 12 August 2011 - 10:17 AM


I hate to say it, but it seems like you are very lost for this being the last project in the course. I don't know how much we can teach you in one thread that you should have learned in class.

This is why I always highly recommend to students to swallow their pride and tell the teacher they are lost very early into the course. And it is something that comes up a LOT: That students for some reason won't admit to being lost and just bluff for the rest of the course. It comes up so often that it has its own FAQ #13 below.

This is not to say we are going to stop helping you. We will do whatever we can short of giving you the code. Just that I have grave concerns about how much comprehension we can impart on you in a short time.


FAQ (Frequently Asked Questions - Updated Aug 2011

Spoiler

This post has been edited by tlhIn`toq: 12 August 2011 - 10:19 AM

Was This Post Helpful? 0
  • +
  • -

#12 Goff256   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

Re: An Array initializer of '20' is expected

Posted 12 August 2011 - 10:23 AM

I'm afraid I can't exactly type up everything it says on how to type up arrays, there is an entire paragraph on it.

The thing I'm saying, and perhaps being ignorant on how to say, is this. Each of those different variables are all part of each and every single entry into my program. Each of those makes up a single contact, which there are supposed to be twenty of.
Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: An Array initializer of '20' is expected

Posted 12 August 2011 - 10:36 AM

View PostGoff256, on 12 August 2011 - 11:23 AM, said:

I'm afraid I can't exactly type up everything it says on how to type up arrays, there is an entire paragraph on it.


Ok. If getting this problem resolved isn't worth your time to type a <sarcasm>whole entire paragraph (oh my!)</sarcasm>, then I'm not seeing a way to help you understand what your textbook is telling you. That is the one and only goal here: To teach you and help you understand.

Quote

The thing I'm saying, and perhaps being ignorant on how to say, is this. Each of those different variables are all part of each and every single entry into my program. Each of those makes up a single contact, which there are supposed to be twenty of.


Yeah. We all got that right off. 20 contacts, each of which have a phone number, address etc. We've all been there. Most of us would have built a 'Contact' class with properties for this. But that may not be where your course is up to yet. And that's fine. We can help you with doing this via string arrays for learning purposes.

But really, you are starting seem less interested in help with understanding, and more interested in someone giving you corrected code. I mean, really, one paragraph is too much typing? Then coding is definitely not the industry you should be considering.

This post has been edited by tlhIn`toq: 12 August 2011 - 10:37 AM

Was This Post Helpful? 0
  • +
  • -

#14 Goff256   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

Re: An Array initializer of '20' is expected

Posted 12 August 2011 - 10:44 AM

Okay, I just want to correct what I said.

I meant that it isn't just a paragraph and IS a whole chapter. I thought I had posted that, but apparently.... something went wrong and I didn't.

If it seems like I'm not interested in understanding, then I apologize. I'm trying to take this as I can, trying to understand. I don't know what I'd do if you simple gave me the code, probably nothing good.

I refuse to use code that people give me.

So this thing is a big misunderstanding when I put paragraph instead of chapter.
My apologies.
Was This Post Helpful? 0
  • +
  • -

#15 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: An Array initializer of '20' is expected

Posted 12 August 2011 - 11:00 AM

No problem. So read the chapter on arrays. Find the part about setting up an array for use. Read it carefully. If it doesn't make sense, let us know what part of setting up the array doesn't make sense.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2