DktPhl2018
DktPhl2018

Reputation: 473

.NET Maui How to Remove Last Item From ObservableCollection

Using ObservableCollection, I learned how to add new input item to List.

Now, I feel puzzled how to remove the last item from list.

Issue: The ObservableCollection does not have a method to remove the last item from list.

I cannot use Remove and RemoveAt b/c these two methods require an index, and the index cannot be the last element.

I get the error: Index was out of range. Must be non-negative and less than the size of the collection.

I have the following code:

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp1.MainPage">

    <ScrollView>
        <VerticalStackLayout 
            Margin="20"
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">

            <Entry x:Name="entry"   
               MaxLength="255"
               MinimumWidthRequest="500"   
               Placeholder="Name"
               FontSize="18"
               HorizontalOptions="Center" />

            <Button
                x:Name="AddToList"
                Text="Add"
                Clicked="AddInputItemToList"
                HorizontalOptions="Center" />

            <Button
                x:Name="RemoveFromList"
                Text="Remove"
                Clicked="RemoveItemFromList"
                HorizontalOptions="Center" />

            <ListView ItemsSource="{Binding MyNames}" >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Label Text="{Binding Name}"></Label>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

MainPage.xaml.cs

using System.Collections.ObjectModel;

namespace MauiApp1
{
    public partial class MainPage : ContentPage
    {

        public ObservableCollection<MyName> MyNames { get; set; } = new ObservableCollection<MyName>();

        public MainPage()
        {
            InitializeComponent();
            BindingContext = this;
        }

        private void AddInputItemToList(object sender, EventArgs e)
        {
            var myName = new MyName
            {
                Name = entry.Text
            };

            MyNames.Add(myName);
        }

        private void RemoveItemFromList(object sender, EventArgs e)
        {
            int N = MyNames.Count;
            if(N > 0)
            {
                MyNames.RemoveAt(N);
            }
        }
    }

    public class MyName
    {
        public string Name { get; set; }
    }
}

Please provide any thoughts.

Thank You

Upvotes: 0

Views: 669

Answers (2)

Alan
Alan

Reputation: 362

The above answers are correct, but to give more detail:

If you have 5 items in the list, their indexes are 0, 1, 2, 3, 4

You were trying to remove the item with index 5, which doesn't exist, hence the index out of range exception.

Upvotes: 2

Jason
Jason

Reputation: 89169

C# indexes are 0 based

if(N > 0)
{
    MyNames.RemoveAt(N-1);
}

Upvotes: 1

Related Questions