Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Index of List<> element

Discussion in 'Scripting' started by benjaminver89, Aug 26, 2015.

  1. benjaminver89

    benjaminver89

    Joined:
    May 14, 2015
    Posts:
    30
    I load a :
    Liste<login> Logins;

    I d'like to match username Gui.textfield and password Gui.passwordfield saved in this list.

    Login class contain :
    _username and _pasword.

    I try Indexof, but i get an :
    Assets/Script/Login.cs(60,50): error CS1660: Cannot convert `lambda expression' to non-delegate type `User'
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Mind posting the relevant lines of your script?
     
  3. benjaminver89

    benjaminver89

    Joined:
    May 14, 2015
    Posts:
    30
    using UnityEngine;
    using UnityEngine.UI;
    using System;
    using System.IO;
    using System.Collections;
    using System.Collections.Generic;

    public class Login : MonoBehaviour {

    public List<User> _users;

    void Start(){
    _users = new List<User> ();
    LoadUsersLog();
    }

    void LoginWindow(int id){
    GUI.Label (new Rect (150,100, 100, 20), "User Name");
    currentuser = GUI.TextField (new Rect (150, 140, 100, 20),currentuser);

    GUI.Label (new Rect (150,140, 100, 20), "Password");
    currentpwd = GUI.PasswordField (new Rect (250,140, 150, 20),currentpwd,"*"[0]);

    if (Wpwd==false)
    GUI.Label (new Rect (310,180, 100, 20), "");
    else
    GUI.Label (new Rect (310,180, 100, 20), "Wrong password");
    if (GUI.Button (new Rect (300, 220, 100, 40), "OK")) {
    PwdIndex=_users.IndexOf( d=>d == currentuser);
    if (currentpwd == _users[PwdIndex]._UserPwd){
    IsLogedIn = true;
    UserLogged= _users[PwdIndex]._UserName;
    }else{
    Wpwd=true;
    }
    }
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Why are you using a lambda expression here? It should read

    Code (csharp):
    1. PwdIndex=_users.IndexOf(currentuser);
     
  5. benjaminver89

    benjaminver89

    Joined:
    May 14, 2015
    Posts:
    30
    lambda expression have solved a previous problem when selecting user in a list (that give me index when clicking on item).
    But now, for personal data security, i need to avoid using selecting in a list displayed on screen.
    So i need to find index corresponding to username and then compare the current password with password registered

    Assets/Script/Login.cs(60,41): error CS1502: The best overloaded method match for `System.Collections.Generic.List<User>.IndexOf(User)' has some invalid arguments