user88
user88

Reputation: 1164

How do I get the index of string variable value into ObservableCollection?

In my windows phone application I am using observablecollection like below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.UserData;
using System.Windows.Media;
using Microsoft.Phone.Maps.Controls;
using System.Collections.ObjectModel;
using System.Collections;

namespace GetContacts
{
    public partial class createGroups : PhoneApplicationPage
    {
        string buttonName = "";
        public static ObservableCollection<Group> groupbtn;

        public createGroups()
        {
            InitializeComponent();
            groupbtn = new ObservableCollection<Group>();        
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            string buttonText = "abc"
            string index = groupbtn.IndexOf(buttonText);                      
        }

    }
}

And below is the Group class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using GetContacts.Resources;
using Microsoft.Phone.UserData;

namespace GetContacts
{
    public class Group
    {
        public string Name { get; set; }
        /*public string content { get; set; }
        private string[] numberofbtns = new string[5];
        public string[] NumberOfBtns
        {
            get { return numberofbtns; }
            set { numberofbtns = value; }
        }*/
        //object[] array1 = new object[5];


        public Group()
        { 

        }
        public Group(Button btn)
        {
            Name = btn.Name;

        }
    }
}

But its getting me two errors below:

Error 1: at this line groupbtn.IndexOf(buttonText):

The best overloaded method match for 'System.Collections.ObjectModel.Collection<GetContacts.Group>.IndexOf(GetContacts.Group)' has some invalid arguments

Error 2 at this line groupbtn.IndexOf(buttonText) on it buttonText:

Argument 1: cannot convert from 'string' to 'GetContacts.Group'

How do I resolve it or kindly suggest me how do I get the index of string variable value into observablecollection. Waiting for your reply. Thanks.

Upvotes: 0

Views: 551

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460158

You cannot use IndexOf with a string on an ObservableCollection<Group>. You need a Group. You also have to override Equals in class Group (also override GetHashCode):

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

    public override bool Equals(object obj)
    {
        Group g2 = obj as Group;
        return g2 != null && g2.Name == this.Name;
    }

    public override int GetHashCode()
    {
        return Name == null 0 : Name.GetHashCode();
    }
}

Now you're able to use IndexOf:

Group searchGroup = new Group { Name = buttonText };
int index = groupbtn.IndexOf(searchGroup);

or use a different approach, for example using LINQ:

int index = groupbtn
    .Select((g, i) => new { Group = g, Index = i })
    .Where(x => x.Group.Name == buttonText)
    .Select(x => x.Index)
    .DefaultIfEmpty(-1)
    .First();

Upvotes: 1

Carlos Rodriguez
Carlos Rodriguez

Reputation: 2220

The IndexOf method is expecting a Group object, but you're providing a string. ObservableCollections implement IEnumerable, so you may be able to use a Linq expression. Such as...

Group btnGroup = (from g in groupbtn where g.Name == "name" select g).First();
int indexOfGroupButton = groupbtn.IndexOf(btnGroup);

Or something like that. Basically, you first need to find the actual "Group" object, and then you can find the index of that object.

Upvotes: 0

Coda17
Coda17

Reputation: 599

Those are the same error. The problem is that you have a collection of type Group and then you are trying to find a matching Group with a string. You have to pass a Group to .IndexOf to find the index of the Group you are trying to find.

This type of error is something you need to be able to figure out yourself from the compiler error.

The best overloaded method match for 'System.Collections.ObjectModel.Collection<GetContacts.Group>.IndexOf(GetContacts.Group)' has some invalid arguments

This clearly means it doesn't like the argument you are passing to this function. It shows the function definition, so you can tell it wants to be passed a Group.

Argument 1: cannot convert from 'string' to 'GetContacts.Group'

You are passing a string to a function that we know accepts a Group. Since it only accepts a Group, it tries to implicitly convert the string to a Group, but there isn't a way for it automatically do that.

Upvotes: 0

Related Questions