Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
namespace IndexOutOfRangeExceptionProject
{
internal class Program
{
private static void Main(string[] args)
{
UtilityHelper utilityHelper = new();

//1) Array - Upper Bound - should be less that the array length
utilityHelper.UpperBoundMethod(false);

//2) Array with custom lower bound
utilityHelper.CustomLowerBoundMethod(2, false);

//3) Incorrect Arguments
Console.WriteLine("Please enter a search argument!!!");
var searchArg = Convert.ToInt32(Console.ReadLine());
utilityHelper.ListIndexOfMethod(searchArg, false);

//4) 2 arrays with different dimensions
utilityHelper.AssignFirstArrayElementToSecondArrayMethod(false);

//5) Getting confused between the index and value at that index
utilityHelper.IndexAndValueMethod(false);

//6 -> DataTable example
utilityHelper.DataTableMethod(false);
}
}
}
using IndexOutOfRangeExceptionProject;

UtilityHelper utilityHelper = new();

//1) Array - Upper Bound - should be less than the array length
utilityHelper.UpperBoundMethod(false);

//2) Array with custom lower bound
utilityHelper.CustomLowerBoundMethod(2, false);

//3) Incorrect Arguments
Console.WriteLine("Please enter a search argument: ");
var searchArg = Convert.ToInt32(Console.ReadLine());
utilityHelper.ListIndexOfMethod(searchArg, false);

//4) 2 arrays with different dimensions
utilityHelper.AssignFirstArrayElementToSecondArrayMethod(false);

//5) Getting confused between the index and value at that index
utilityHelper.IndexAndValueMethod(false);

//6) DataTable example
utilityHelper.DataTableMethod(false);

//7) Index from end, ranges and ElementAtOrDefault
utilityHelper.IndexFromEndMethod(false);
utilityHelper.RangeMethod(false);
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
using System.Data;

namespace IndexOutOfRangeExceptionProject.Repository
namespace IndexOutOfRangeExceptionProject.Repository;

public class StaticDataRepository
{
public class StaticDataRepository
public static DataTable GetGroceries()
{
public static DataTable GetGroceries()
var dataTable = new DataTable
{
var dataTable = new DataTable
{
TableName = "Groceries"
};
dataTable.Columns.Add("Id", typeof(int));
dataTable.Columns.Add("Name", typeof(string));
dataTable.Columns.Add("Description", typeof(string));
TableName = "Groceries"
};
dataTable.Columns.Add("Id", typeof(int));
dataTable.Columns.Add("Name", typeof(string));
dataTable.Columns.Add("Description", typeof(string));

dataTable.Rows.Add(1, "Macaroni", "Tasty Pasta");
dataTable.Rows.Add(2, "Ramen", "Tasty Noodles");
dataTable.Rows.Add(3, "Figaro Oil", "Olive Oil");
dataTable.Rows.Add(4, "XYZ Lentils", "Nutritious Pulses");
dataTable.Rows.Add(1, "Macaroni", "Tasty Pasta");
dataTable.Rows.Add(2, "Ramen", "Tasty Noodles");
dataTable.Rows.Add(3, "Figaro Oil", "Olive Oil");
dataTable.Rows.Add(4, "XYZ Lentils", "Nutritious Pulses");

return dataTable;
}
return dataTable;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,154 +1,173 @@
using IndexOutOfRangeExceptionProject.Repository;
using System.Data;

namespace IndexOutOfRangeExceptionProject
namespace IndexOutOfRangeExceptionProject;

public class UtilityHelper
{
public class UtilityHelper
public int[] NumbersArray { get; set; }
public Array CustomLowerBoundArray { get; set; }
public List<int> MultiplesOfFive { get; set; }
public int[] Array1 { get; set; }
public int[] Array2 { get; set; }
public int[] OddNumbersArray { get; set; }
public DataTable Groceries { get; set; }

public UtilityHelper()
{
public int[] NumbersArray { get; set; }
public Array CustomLowerBoundArray { get; set; }
public List<int> MultiplesOfFive { get; set; }
public int[] Array1 { get; set; }
public int[] Array2 { get; set; }
public int[] OddNumbersArray { get; set; }
public DataTable Groceries { get; set; }
NumbersArray = new int[] { 1, 2, 3, 4, 5 };
CustomLowerBoundArray = Array.CreateInstance(typeof(int), new int[] { 5 }, new int[] { 1 });
MultiplesOfFive = new List<int> { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 };
Array1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
OddNumbersArray = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
Groceries = StaticDataRepository.GetGroceries();
}

public UtilityHelper()
public void UpperBoundMethod(bool forcefullyRaiseException)
{
if (forcefullyRaiseException)
{
NumbersArray = new int[] { 1, 2, 3, 4, 5 };
CustomLowerBoundArray = Array.CreateInstance(typeof(int), new int[] { 5 }, new int[] { 1 });
MultiplesOfFive = new List<int> { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 };
Array1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
OddNumbersArray = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
Groceries = StaticDataRepository.GetGroceries();
for (var i = 0; i <= NumbersArray.Length; i++)
{
Console.WriteLine(NumbersArray[i]);
}
}

public void UpperBoundMethod(bool forcefullyRaiseException)
else
{
if (forcefullyRaiseException)
for (var i = 0; i < NumbersArray.Length; i++)
{
for (var i = 0; i <= NumbersArray.Length; i++)
{
Console.WriteLine(NumbersArray[i]);
}
Console.WriteLine(NumbersArray[i]);
}
else
}
}

public void CustomLowerBoundMethod(int input, bool forcefullyRaiseException)
{
if (forcefullyRaiseException)
{
for (var i = 0; i < CustomLowerBoundArray.Length; i++)
{
for (var i = 0; i < NumbersArray.Length; i++)
{
Console.WriteLine(NumbersArray[i]);
}
CustomLowerBoundArray.SetValue(input, i);
input *= 5;
}
}

public void CustomLowerBoundMethod(int input, bool forcefullyRaiseException)
else
{
if (forcefullyRaiseException)
for (var i = CustomLowerBoundArray.GetLowerBound(0); i <= CustomLowerBoundArray.GetUpperBound(0); i++)
{
for (var i = 0; i < CustomLowerBoundArray.Length; i++)
{
CustomLowerBoundArray.SetValue(input, i);
input *= 5;
}
CustomLowerBoundArray.SetValue(input, i);
input *= 5;
}
else
for (var i = CustomLowerBoundArray.GetLowerBound(0); i <= CustomLowerBoundArray.GetUpperBound(0); i++)
{
for (var i = CustomLowerBoundArray.GetLowerBound(0); i <= CustomLowerBoundArray.GetUpperBound(0); i++)
{
CustomLowerBoundArray.SetValue(input, i);
input *= 5;
}
for (var i = CustomLowerBoundArray.GetLowerBound(0); i <= CustomLowerBoundArray.GetUpperBound(0); i++)
{
Console.WriteLine(CustomLowerBoundArray.GetValue(i));
}
Console.WriteLine(CustomLowerBoundArray.GetValue(i));
}
}
}

public void ListIndexOfMethod(int searchArgument, bool forcefullyRaiseException)
{
var startIndex = MultiplesOfFive.IndexOf(searchArgument);
public void ListIndexOfMethod(int searchArgument, bool forcefullyRaiseException)
{
var startIndex = MultiplesOfFive.IndexOf(searchArgument);

if (forcefullyRaiseException)
if (forcefullyRaiseException)
{
Console.WriteLine($"Let's display the multiples of 5 starting at {searchArgument}");
for (var i = startIndex; i < MultiplesOfFive.Count; i++)
{
Console.WriteLine($"Let's display the even numbers greater than {searchArgument}");
for (var i = startIndex; i < MultiplesOfFive.Count; i++)
{
Console.WriteLine(MultiplesOfFive[i]);
}
Console.WriteLine(MultiplesOfFive[i]);
}
}
else
{
if (startIndex < 0)
{
Console.WriteLine($"The number {searchArgument} does not exist in the list.");
}
else
{
if (startIndex < 0)
{
Console.WriteLine($"The number {searchArgument} does not exist in the list.");
}
else
Console.WriteLine($"Let's display the multiples of 5 starting at {searchArgument}");
for (var i = startIndex; i < MultiplesOfFive.Count; i++)
{
Console.WriteLine($"Let's display the even numbers greater than {searchArgument}");
for (var i = startIndex; i < MultiplesOfFive.Count; i++)
{
Console.WriteLine(MultiplesOfFive[i]);
}
Console.WriteLine(MultiplesOfFive[i]);
}
}
}
}

public void AssignFirstArrayElementToSecondArrayMethod(bool forcefullyRaiseException)
public void AssignFirstArrayElementToSecondArrayMethod(bool forcefullyRaiseException)
{
if (forcefullyRaiseException)
{
if (forcefullyRaiseException)
{
Array2 = new int[7];
Array2[Array1.Length - 1] = Array1[Array1.Length - 1];
Console.WriteLine($"The last element of array2 is: {Array2[Array2.Length - 1]}");
}
else
{
Array2 = new int[Array1.Length];
Array2[Array1.Length - 1] = Array1[Array1.Length - 1];
Console.WriteLine($"The last element of array2 is: {Array2[Array2.Length - 1]}");
}
Array2 = new int[7];
Array2[Array1.Length - 1] = Array1[Array1.Length - 1];
Console.WriteLine($"The last element of array2 is: {Array2[Array2.Length - 1]}");
}
else
{
Array2 = new int[Array1.Length];
Array2[Array1.Length - 1] = Array1[Array1.Length - 1];
Console.WriteLine($"The last element of array2 is: {Array2[Array2.Length - 1]}");
}
}

public void IndexAndValueMethod(bool forcefullyRaiseException)
public void IndexAndValueMethod(bool forcefullyRaiseException)
{
if (forcefullyRaiseException)
{
if (forcefullyRaiseException)
foreach (var number in OddNumbersArray)
{
foreach (var number in OddNumbersArray)
{
Console.WriteLine($"The odd number is: {OddNumbersArray[number]}");
}
Console.WriteLine($"The odd number is: {OddNumbersArray[number]}");
}
else
}
else
{
foreach (var number in OddNumbersArray)
{
foreach (var number in OddNumbersArray)
{
Console.WriteLine($"The odd number is: {number}");
}
Console.WriteLine($"The odd number is: {number}");
}
}
}

public void DataTableMethod(bool forcefullyRaiseException)
public void DataTableMethod(bool forcefullyRaiseException)
{
if (forcefullyRaiseException)
{
if (forcefullyRaiseException)
for (var i = 0; i < Groceries.Rows.Count; i++)
{
for (var i = 0; i < Groceries.Rows.Count; i++)
{
var row = Groceries.Rows[i];
Console.WriteLine($"{row[0]}\t {row[1]}\t {row[2]}\t {row[3]}");
}
var row = Groceries.Rows[i];
Console.WriteLine($"{row[0]}\t {row[1]}\t {row[2]}\t {row[3]}");
}
else
}
else
{
foreach (DataRow grocery in Groceries.Rows)
{
foreach (DataRow grocery in Groceries.Rows)
foreach (var item in grocery.ItemArray)
{
foreach (var item in grocery.ItemArray)
{
Console.WriteLine($"{item}");
}
Console.WriteLine("********************");
Console.WriteLine($"{item}");
}
Console.WriteLine("********************");
}
}
}
}

public void IndexFromEndMethod(bool forcefullyRaiseException)
{
if (forcefullyRaiseException)
{
Console.WriteLine(NumbersArray[^0]);
}
else
{
Console.WriteLine($"The last element is: {NumbersArray[^1]}");
Console.WriteLine($"The last three elements are: {string.Join(", ", NumbersArray[^3..])}");
Console.WriteLine($"The tenth element or the default: {NumbersArray.ElementAtOrDefault(9)}");
}
}

public void RangeMethod(bool forcefullyRaiseException)
{
var range = forcefullyRaiseException ? 3..7 : 3..5;
Console.WriteLine(string.Join(", ", NumbersArray[range]));
}
}
Loading
Loading