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

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.

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();

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.

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.

Related

Unable to use ICollection.ToList()

I am trying to call ToList() here:
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
namespace CSVStuff
{
public static class CSVContentGenerator
{
public static string GetContent(IOrderedDictionary headingsPropertiesMapping)
{
var propertyNames = headingsPropertiesMapping.Keys.ToList(); //ICollection does not contain a definition for ToList()
//var propertyNames = new List<object>(headingsPropertiesMapping.Keys); //Cannot convert from ICollection to int
return "";
}
}
}
Why are these not working?
Try this:
var propertyNames = headingsPropertiesMapping.Keys.Cast<T>().ToList();
and type T is the type of dictionary keys.

does not contain a public instance definition for 'getenumerator' for each error thrown

I am trying to loop through a list that contains student data using foreach method however I get the error QA does not contain a public instance definition for 'getenumerator' for each.
My QA class is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Test
{
class QA
{
private List<Student> students;
public QA()
{
students = new List<Student>();
string line;
using (StreamReader reader = new StreamReader("/Users/jvb/Desktop/Students.txt"))
{
line = reader.ReadLine();
var s = line.Split(',');
int id = int.Parse(s[0]);
int houseNo = int.Parse(s[3]);
var status = int.Parse(s[7]);
Student sData = new Student(id, s[1], s[2], houseNo, s[4], s[5], s[6], (StudentStatus)status);
AddStudent(sData);
}
}
public List<Student> GetStudents()
{
return students;
}
public void AddStudent(Student student)
{
students.Add(student);
}
}
}
This simply loops through a text file with various bits of data and adds each student to a students list. Inside my program.cs file I created an instance of the QA class and tried to loop through it like so:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test
{
class Program
{
static void Main(string[] args)
{
QA students = new QA();
foreach (var s in students)
{
Console.WriteLine(s.GetStudents());
}
}
}
}
I am quite new to c#, could anyone care to explain what I am misunderstanding/doing wrong?
You are direct use object that is not enuerable, you have to access it member which is implemented IList and that is enumerable.
you are doing it all wrong.
You are iterating class object which is not iterable. you don't need foreach.
static void Main(string[] args)
{
QA students = new QA();
var studentList= s.GetStudents(); //you get all the students not you can iterate on this lidt
foreach(var student in studentList)
{
//here you can access student property like
Console.WriteLine(student.Name); //I assume Name is a property of Student class
}
}

Importing CSV File in C# Convert the string list to specific classtype list

First i have to mention that iam not that good in programming but i try my best.
Following situation: I imported several csv files into string lists. Now i want to cast those lists as the classdatatype i need. For example class Student, in the studentlistmanager class iam improting the list and trying to convert it to List but it seems like it wont be that easy, i tried to create an object of that list and add the object to the student list but that wont work either. Instead of the values i get System.String[] values into my list.
internal void ImportStudentList(CsvImportService csv)
{
List<string> newList = new List<string>();
csv = new CsvImportService("Klassenlisten_20160906.csv");
for(int i = 0; i <= csv.ClassList.Count;i++)
{
for(int x = 0; x <= csv.ClassList.Count;x++)
{
string line = csv.ClassList[i];
// Student st = new Student(line);
// ListOfStudents.Add(st);
newList.Add(line);
ListOfStudents = newList.Cast<Student>().ToList();
}
}
}
I would really appreciate any help. Thanks in advance!
Is that what you are looking for ? Saving the Data of the csv File in the Student
List.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Stackoverflow_Konsole_Test
{
class Program
{
static void Main(string[] args)
{
string CSV_FILE_PATH = #"filepath.csv";
List<string> FULL_CSV_STRING = new List<string>();
Students Student1 = new Students();
FULL_CSV_STRING.Add(File.ReadAllText(CSV_FILE_PATH));
foreach (string line in FULL_CSV_STRING)
{
Student1.add(line);
}
foreach (string line in Student1.getlist())
{
Console.WriteLine(line);
}
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Stackoverflow_Konsole_Test
{
class Students
{
private List<string> List_of_students = new List<string>();
public Students()
{
//constructor
}
public void add(string line)
{
this.List_of_students.Add(line);
}
public List<string> getlist()
{
return this.List_of_students;
}
}
}

Parse.Com and getting the relational data

I have the following code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using OpenFridge.Portable.Data.Interfaces.Entities;
using OpenFridge.Portable.Data.Parse.Entities;
using Parse;
using AutoMapper;
namespace OpenFridge.Portable.Data.Parse.Entities
{
[ParseClassName("_User")]
public class ParseUserEntity : ParseUserEntityBase, IUserEntity
{
private string _password;
[ParseFieldName("password")]
public new string Password
{
get { return _password; }
set
{
_password = value;
base.Password = value;
}
}
Lazy<IEnumerable<IBankAccountEntity>> _bankAccounts;
[ParseFieldName("bankAccounts")]
public Lazy<IEnumerable<IBankAccountEntity>> BankAccounts
{
get
{
var relation = GetRelation<ParseBankAccountEntity>("BankAccount");
if (relation == null) return null;
var result = relation.Query.FindAsync().Result;
var _bankAccounts = new Lazy<IEnumerable<IBankAccountEntity>>(() => relation.Query.FindAsync().Result);
return _bankAccounts;
}
set
{
_bankAccounts = value;
}
}
}
}
And it all seems to work quite fine, however.. once I use the .BankAccounts property I get the following exception:
Must specify a ParseObject class name when creating a ParseQuery.\r\nParameter name: className
Which I find strange since there is no way for me to define a classname with in that line of code:
(That line of code beeing:)
relation.Query.FindAsync().Result
So.. is this a bug?.. Am I doing something wrong or using it wrong?
Any ideas?
Br,
Inx

Adding objects to an array in the constructor

I am making a simple c# web service that takes in a name and returns the corresponding phone number.
So I created a contact class with a name field and a number field
Here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PhoneService
{
class Contact
{
String name;
int number;
//constructor to make new contact
public Contact(String name, int number)
{
this.name = name;
this.number = number;
}
}
}
And here is my web service class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PhoneService
{
class PhoneBook : IPhoneBook
{
Contact[] contactList = new Contact[4];
//constructor
public PhoneBook ()
{
contactList[0] = new Contact("Mary Jones", 1800252525);
contactList[1] = new Contact("Bob Smith", 1800343434);
contactList[2] = new Contact("Martin Dunne", 1800797979);
contactList[3] = new Contact("Sarah Mitchel", 1800898989);
}
//method to look up name
public string lookUpNumberByName(string name)
{
//variable to hold name entered
String nameEntered = name; ;
/**
code to compare the name entered with the
the names of the contact objects.
If the name is a match with a contact name then return
the associated number
**/
//return number corresponding to the name
return null;
}
}
}
So I am trying to make 4 simple contact objects stored in an array then when I enter a name I can check if it matches one of them and return the relevant phone number.
The problem is with the constructor
My assignment says-
"Have the service maintain a list of names and phone numbers in a collection in memory which is initialised in the constructor for the service class."
So I don't know what parts I should have in the constructor and what should be outside in the main class.Hopefully someone can let me know the best way I can go ahead with this.
Thanks in advance
UPDATE
I have redone my code and made it a bit more simple by removing the contact class.
Here it is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PhoneService
{
class PhoneBook : IPhoneBook
{
static String[] nameList = new String[4];
static String[] numberList = new String[4];
//constructor
public PhoneBook()
{
nameList[0] = ("Mary Jones");
nameList[1] = ("Bob Smith");
nameList[2] = ("Martin Dunne");
nameList[3] = ("Martin Dunne");
numberList[0] = ("1800252525");
numberList[1] = ("1800343434");
numberList[2] = ("1800797979");
numberList[3] = ("1800898989");
}
//method to look up name
public String lookUpNumberByName(String name)
{
try
{
if (string.Equals(name, nameList[0], StringComparison.CurrentCultureIgnoreCase))
{
return numberList[0];
}
if (string.Equals(name, nameList[1], StringComparison.CurrentCultureIgnoreCase))
{
return numberList[1];
}
if (string.Equals(name, nameList[2], StringComparison.CurrentCultureIgnoreCase))
{
return numberList[2];
}
if (string.Equals(name, nameList[3], StringComparison.CurrentCultureIgnoreCase))
{
return numberList[3];
}
}
catch (System.Exception)
{
return ("That name is not found");
}
}//end of lookUpNumberByName method
}//end of class phonebook
}// end of phoneservice
My lookUpNumberByName method is getting an error saying not all code paths return a value
You're basically asking about encapsulation. Generally, you want to think of what parts of the class you want to expose kind of like an engineer thinks about what controls of a car to expose to a driver. Basically, if no one on the outside will need to use this property of this method, don't expose it, just leave it be.
What your assignment is effectively asking you to do is not to expose a property but to set a contact list via a service constructor. So if you need to set your contact list, shouldn't you just pass a contact list into the constructor for your phone book? And remember that you can have multiple constructors and if you don't specify one that takes no arguments, you won't be able to simply declare an instance of that class without passing arguments.
You mean something like this?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PhoneService
{
class PhoneBook : IPhoneBook
{
private static List<Contact> _contactList = new List<Contact>();
//constructor
public PhoneBook (List<Contact> contactList)
{
_contactList = contactList;
}
public PhoneBooks()
{
}
}
}
you can use it like this
using System;
class Program
{
static void Main()
{
var contactList = new List<Contact>();
contactList.Add(new Contact("Mary Jones", 1800252525));
contactList.Add(new Contact("Bob Smith", 1800343434));
contactList.Add(new Contact("Martin Dunne", 1800797979));
contactList.Add(new Contact("Sarah Mitchel", 1800898989));
var phoneBook = new PhoneBook(contactList);
}
}

Categories