CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2015
    Posts
    18

    Creating arrays with a method

    I am just practicing a few things, trying to get the hang of c#. I have very little experience and am teaching myself so some help on here would be much appreciated! I am trying to create a method that would ask the user to input a number and then place that number into an index on the array it is creating. It is set up to use a "For" loop to set the number for each index. After all the indices are filled, it should print out the whole array. I can't seem to figure out how to get the user's input though. Here is my code, hopefully someone can show me what I am doing wrong!

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace C_Sharp_Practice
    {
     
     class Program
      {
         static void MakeArray()
         {
              int[] Array = new int[4];
    
             for (int i = 0; i <= Array.Length; i++)
             {Console.WriteLine("Please enter a number for this index.");
             Array[i] = Convert.ToInt32(Console.ReadLine);
                }
             Console.WriteLine(Array.Length);
    
    
             }
         static void Main(string[] args)
         {
             MakeArray();
         }
                
            }
         }

  2. #2
    Join Date
    Feb 2008
    Posts
    108

    Re: Creating arrays with a method

    I see several problems with your method. In your 'for' loop, you are exceeding the bounds of the array. You are looping until i <= Array.Length; When you start with i = 0, you should run as long as i < the array length. Also, I would declare a temp string variable that you fill with Console.ReadLine(), then convert that string variable to an int.
    When you call Console.WriteLine(Array.Length);, you are just printing out the array length, or '4'.
    Developing using:
    .NET3.5 / VS 2010

  3. #3
    Join Date
    Sep 2015
    Posts
    18

    Re: Creating arrays with a method

    Quote Originally Posted by Jim_Auricman View Post
    I see several problems with your method. In your 'for' loop, you are exceeding the bounds of the array. You are looping until i <= Array.Length; When you start with i = 0, you should run as long as i < the array length. Also, I would declare a temp string variable that you fill with Console.ReadLine(), then convert that string variable to an int.
    When you call Console.WriteLine(Array.Length);, you are just printing out the array length, or '4'.
    Ok so I understand the part about printing out the wrong thing. I didn't think that part through very well. I am a little confused about the loop exceeding the bounds of the array. Should it loop until i>4 so that it gets 5 inputs? And I tried making an "input" variable, then converting it and assigning it to the array, but it was not allowing it.

  4. #4
    Join Date
    Feb 2008
    Posts
    108

    Re: Creating arrays with a method

    Yes, you could pass the array size into the method as a value parameter.
    Loop as long as i < the array length, not i<= array length.
    To display the contents of the array, start with this line, then think of a way to do it in a more generic way.
    Console.WriteLine(Array[0] + ", " + Array[1] + ", " + Array[2] + ", " + Array[3]);
    Developing using:
    .NET3.5 / VS 2010

  5. #5
    Join Date
    Apr 2016
    Posts
    1

    Re: Creating arrays with a method

    I don't mean to take away from the main issue at hand but how do I open up a new post. I have an urgent issue I need help with from all you geniuses.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Creating arrays with a method

    Quote Originally Posted by NMDSPARTAN View Post
    I don't mean to take away from the main issue at hand but how do I open up a new post. I have an urgent issue I need help with from all you geniuses.
    At the top of this thread, you'll see a breadcrumb like: Forum\C# Programming\C-Sharp Programming\Creating arrays with a method

    Click on C-Sharp Programming. In the upper left corner, there is an "Open New Thread" button.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured