Hi All,

I am coming from C++ world and working on some C# code. Currently working on a class with an ArrayList data member and would like to control which methods/behaviours get exposed to the client of this class.

One of the functions in this class takes an index as input, checks to make sure the index is not out of bounds and then returns the element if the index is valid.

I am getting a compiler error message (not all code paths return a value) and would like some hep into figuring how to resolve it.

Please advise if there is a better way of implementing this code in C#, or there is a c# way of implementing this function.

Thanks in advance.

namespace TrackStudents
{  
  class EnrolledClass
  {
    //code somewhere before the getElement method in a class...
    ArrayList myArrayList = new ArrayList(); 

    //more methods here......

    public string getElement( int index )
    {
      if ( ! (index < MIN) || (index >= myArrayList.Count) )
      {
        return myArrayList[index];
      }
    }
  } 
}/*END: NAMESPACE*/

Hey, in your method your returning only if that expression evaluates as true, otherwise there'll be nothing (you don't use return).

This could be a solution:

public string getElement( int index )
    {
      if ( ! (index < MIN) || (index >= myArrayList.Count) )
      {
        return myArrayList[index];
      }
      return null;
    }

I hope it helped :)

@CloneXpert: don't use index >= myArrayList.Count use index > myArrayList.Count Remember if you have a list with a count of 2, the Indexes go like this: 0, 1
@badboy11 ArrayList can hold any object, but this is perhaps not exactly what you want to do. Take a look at the generic collection types instead.

@ddanbe: well i just pasted the code and added the line "return null;" so i wasn't really paying attention to that :)

@CloneXpert: don't use index >= myArrayList.Count use index > myArrayList.Count Remember if you have a list with a count of 2, the Indexes go like this: 0, 1
@badboy11 ArrayList can hold any object, but this is perhaps not exactly what you want to do. Take a look at the generic collection types instead.

Hey ddanbe,

I see what you are referring to but my if statement is correct. The index that my method is taking input is starting from zero(0) so the outer bound checking should be less then ArrayList.Count, otherwise there will be an Out of Bounds Runtime error and the program will crash if there are no exception handling to manage it.

Well badboy11, you are totally right! But why using a NOT (the ! sign) when you just could change the conditions. That was confusing me a bit. :-O

Well badboy11, you are totally right! But why using a NOT (the ! sign) when you just could change the conditions. That was confusing me a bit. :-O

My apologies for the confusing code. I am coming from a C++ world and the style I am using is very normal in that world.

Hey, in your method your returning only if that expression evaluates as true, otherwise there'll be nothing (you don't use return).

This could be a solution:

public string getElement( int index )
    {
      if ( ! (index < MIN) || (index >= myArrayList.Count) )
      {
        return myArrayList[index];
      }
      return null;
    }

I hope it helped :)

Hi CloneXpert, thank you for your feedback.

So the client of the 'getElement()' method would have to do checks to verify if valid data was returned or a NULL, before displaying the output.

This question has already been answered. Start a new discussion instead.