Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 02-08-2009, 09:43 PM   #1 (permalink)
office politics's Avatar
 
It's all just 1s and 0s

Join Date: Jan 2004

Location: in the lab

Posts: 4,425

office politics will become famous soon enough

Default c# array.resize

i'm getting index out of bounds when trying to assign ok the the a array.

Code:
static void Main(string[] args)
        {
            string[] a = new string[0];

            Console.WriteLine(a.GetUpperBound(0) + 1);

            Array.Resize(ref a, a.GetUpperBound(0) + 1);

            a[0] = "ok";

            Console.WriteLine(a[0]);
        }

office politics is offline  
Old 02-09-2009, 04:38 AM   #2 (permalink)
 
Junior Techie

Join Date: Feb 2009

Posts: 69

NeoMagic33 is on a distinguished road

Default Re: c# array.resize

Quote:
Originally Posted by office politics View Post
i'm getting index out of bounds when trying to assign ok the the a array.

Code:
static void Main(string[] args)
        {
            string[] a = new string[0];

            Console.WriteLine(a.GetUpperBound(0) + 1);

            Array.Resize(ref a, a.GetUpperBound(0) + 1);

            a[0] = "ok";

            Console.WriteLine(a[0]);
        }
Hi friend

This is my first post and feel that i am useful at C#.NET...

How familar are you with C#?

Index out of bounds means your trying to access a position within the array that is unavailable or not assigned anything..

The size of an array is ALWAYS "length - 1", as indexing starts at 0 within array, so what you are doing with the + 1 is incorrect. Additionally your array doesnt seem to have a size as you have assigned it 0, although as i said the indexing starts at 0, you still must assign a vaule <=1.

Let me know how you get on

All The Best

NeoMagic33
NeoMagic33 is offline  
Old 02-09-2009, 06:37 AM   #3 (permalink)
Baez's Avatar
 

Join Date: Sep 2005

Location: Toronto, Canada

Posts: 5,484

Baez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of light

Default Re: c# array.resize

Neo is somewhat right. Except that you can set the array to 0 to start with. You're just resizing the array wrong. It isn't adding 1 to the size the way you were doing it.

Code:
static void Main(string[] args)
        {
            string[] a = new string[0];

            Console.WriteLine(a.GetUpperBound(0) + 1);

            Array.Resize(ref a, a.Length + 1);

            a[0] = "ok";

            Console.WriteLine(a[0]);
         }
That's what you need to use.
__________________

Baez is offline  
Old 02-09-2009, 09:09 AM   #4 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default Re: c# array.resize

From a practical standpoint, if you find yourself needing to manually resize an array, use a List<T> instead. You can always call the 'ToArray()' method on the list if you need to pass the list as an array to some method.
jaeusm is offline  
Old 02-10-2009, 07:29 PM   #5 (permalink)
office politics's Avatar
 
It's all just 1s and 0s

Join Date: Jan 2004

Location: in the lab

Posts: 4,425

office politics will become famous soon enough

Default Re: c# array.resize

I'm going to switch over to a list and use the add function. much easier.

jaeusm repped - edit, if i could - javascript error
office politics is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On