Jump to content

[C#] Finding the next available position in an array

Drown
Go to solution Solved by Ciccioo,

those values must come from somewhere

if they're hardcoded in the source, just count them and hardcode their number too in the counter that fizzlesticks suggested

if they're loaded when the program is launched, just keep the counter up to date as you load up your elements from a file or database or something

Hello everyone,

 

I have a two-dimensions array of a fixed size of 50 elements. I need to ask the user for some values and insert them into the array. The problem is "How do I make sure I'm not overwriting anything that's in there?"

There will already be some content in the array when I start the program, but I don't know how much. How can I find the next available ID in the array, to insert my content there without overwriting anything that could be already in there? I tried using array.GetUpperBound and array.GetLength, however they return fixed values no matter how many elements are already in the array.

have to use an array, I can't use lists or anything like that.

What can I do to find out the next "free" position in my array?

Thank you very much for helping.

Link to comment
Share on other sites

Link to post
Share on other sites

Are you always adding to the end of the array? If so, you can just keep a counter.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Are you always adding to the end of the array? If so, you can just keep a counter.

 

Even if I use a counter, when I insert my first value in the array, won't it replace what it's already there if there are already values in the array when the program starts?

Link to comment
Share on other sites

Link to post
Share on other sites

those values must come from somewhere

if they're hardcoded in the source, just count them and hardcode their number too in the counter that fizzlesticks suggested

if they're loaded when the program is launched, just keep the counter up to date as you load up your elements from a file or database or something

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×