Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6859515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:17:35+00:00 2026-05-27T02:17:35+00:00

I’m having a very odd problem in C# and I’m not sure what is

  • 0

I’m having a very odd problem in C# and I’m not sure what is the cause. Take a look at the following code snippet:

foreach(string bed in bayBeds)
{
    string[] bedProperties = bed.Split(new char[] { '^' });
    if (bedProperties.Length > 0)
    {
        string genderCode = bedProperties[1];
        if (genderCode == "M")
        {
            bedCount = bedCount + bayBeds.Count;
            break;
        }
    }
}

In this example, the string array bedProperties is tested to see if its length is greater than 0, and if so, element 1 is retrieved. The problem is that this code always generates an out of bounds exception. I can modify to return bedProperties.Length and it will give me a value such as 3 (which is in fact the number of properties in this object), yet any attempt to get an array element by index (such as bedProperties[1], bedProperties[0], etc.) will always give me an out of bounds exception. Always. I can’t understand why this would be.

Please understand I’m something of a c# hack, so if I’ve made some ridiculously stupid mistake, please don’t be overly harsh.

Thanks – I appreciate all help.

EDIT: I found the issue based on much of the assistance below.

For clarity, this is the entire function:

public int returnMaleBedTotal(string bedCollection) {
      // determine total number of male beds for a bay
      int bedCount = 0;
      if (bedCollection.Length > 0) {
        List<string> theBays = new List<string>(bedCollection.Split(new char[] { '@' }));

        // at this point we have the bays, so iterate them and extract the beds in the bays
        foreach (string bayBedCollection in theBays) {
          List<string> bayBeds = new List<string>(bayBedCollection.Split(new char[] { '|' }));

          // now we have the beds in the bay, so extract the bed properties and determine if the bed is male
          foreach(string bed in bayBeds) {
            string[] bedProperties = bed.Split(new char[] { '^' });
            if (bedProperties.Length > 1) {
              string genderCode = bedProperties[1];
              string bedStatus = bedProperties[2];
              if (genderCode == "M") {
                bedCount = bedCount + bayBeds.Count;
                break;

              }

            }

          }

        }

      } 

      return bedCount;

    }

This takes a collection in the form of a big string that looks like this:

100000^^3|100002^^1|100003^^3|100004^F^2|100005^^2@100006^^1|100007^^2|100008^M^2|100009^^1|100010^^3@100011^M^2|100012^M^2|100013^M^1|100014^M^2|100015^M^1@100016^F^1|100017^^1|100018^F^1|100019^^1|100020^^1

It then chops that up into units that look like this:

100000^^3|100002^^1|100003^^3|100004^F^2|100005^^2

Which it further parses to units like this:

100005^^2 or 100004^F^2

On occasion, during these iterations one of these units would come back malformed and would have a length of 1, so the attempt to get the an indice > 0 would fail.

By the way, this is an extension method inside a transform, and that’s the reason for taking the initial collection as a big string.

Thanks to all who helped – sorry I can’t choose more than one correct answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:17:35+00:00Added an answer on May 27, 2026 at 2:17 am
    if (bedProperties.Length > 0)
    

    Should really be:

    if(bedProperties.Length > 1)
    

    Because any string, when split, will return itself in a single element array. If any splitting actually took place, there would be two or more elements in the array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.