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 7791647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:57:06+00:00 2026-06-01T21:57:06+00:00

I want to write a check for some conditions without having to use try/catch

  • 0

I want to write a check for some conditions without having to use try/catch and I want to avoid the possibilities of getting Index Out of Range errors

if (array.Element[0].Object.Length > 0 || array.Element[1].Object.Length > 0) //making sure there's at least one Object array that has values
 {                
    if (array.Element[0].Object[0].Item.Length != 0 || array.Element[1].Object[0].Item.Length != 0) //this is where I check that at least one of the Items (strings) is not empty
       {
           // execute code here
       }
 }

So the problem I am facing is that in the second check I need to see whether I have one Item that is not empty. However, If I don’t have Element[1], I get the Index Out of Range exception. The problem is that there could be 2 Elements and one(or both) of them may have empty Object arrays. The code will have to be executed only if one of thos Item strings is not empty.

Hopefully, I explained it well. How do I go about avoiding getting that exception under any condition?

  • 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-06-01T21:57:09+00:00Added an answer on June 1, 2026 at 9:57 pm

    Alright, you need some better null checking and some more cautious code here.

    if (array.Element[0].Object.Length > 0 || array.Element[1].Object.Length > 0) //making sure there's at least one Object array that has values
    {                
       if (array.Element[0].Object[0].Item.Length != 0 || array.Element[1].Object[0].Item.Length != 0) //this is where I check that at least one of the Items (strings) is not empty
       {
           // execute code here
       }
    }
    

    is just unacceptable.

    First, let’s null check

    if (array != null)
    {
        if (array.Element != null)
    

    for simplicity, you could use &&

    if (array != null && array.Element != null)
    

    then, inside that if, we use a for loop (since you’re stuck on arrays) and null check it

    for (int i = 0; i < array.Element; ++i)
    {
        if (array.Element[i] != null && array.Element[i].Object != null)
        {
    

    then, since you have nested arrays, we loop again. This is called a nested loop, and it’s generally bad practice, I’ll show you why it works in a second.

    for (int o = 0; o < array.Element[i].Object.length; ++o)
    {
        if (array.Element[i].Object[o] != null && !string.IsNullOrEmpty(array.Element[i].Object[o].Item))
        {
    

    Now, with all of that ugly nested loopyness, we’ve found out that your Item is not null.
    On top of that, you have access to ALL of the potential values here, and can group them as you like. Here’s how I would put the whole thing together for simplification.

    List<string> arrayValues = new List<string>();
    if (array != null && array.Element != null)
    {
        for (int i = 0; i < array.Element.length; ++i)
        {
            //bool found = false;
            if (array.Element[i] != null && array.Element[i].Object != null)
            {
                for (int o = 0; o < array.Element[i].Object.length; ++o)
                {
                    if (array.Element[i].Object[o] != null && !string.IsNullOrEmpty(array.Element[i].Object[o].Item))
                    {
                        arrayValues.Add(array.Element[i].Object[o].Item);
                        //if you want to drop out here, you put a boolean in the bottom loop and break and then break out of the bottom loop if true
                        //found = true;
                         //break;
                    }
                }
            }
            //if (found)
            //  break;
        }
    }
    if (arrayValues.Count > 0)
    {
        //do stuff with arrayValues
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write some autocmd's that check if the filename is this or
I want to write a method in Java that verifies that some conditions hold
I want to write some large amount of data to a file. the data
I want to be able to write something as void Start(some condition that might
I want to write some code that deletes all DIRECTORIES older than 7 days.
I want to write a simple Ruby DSL to translate some statements and expressions
I want to write a Linux 2.6 netfilter module , which can check the
I want to check with some of the more experienced Oracle developers here on
As per my application I want to write some Lines code in ThisWorkbook of
I want to write some C code such that gcc using the -msse4.1 flag

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.