C#C
C#2y ago
Mek

✅ Just curious about something

using Diary.Models;
using Diary.Services;
using ReactiveUI;
using System;
using System.Reactive;
using System.Reactive.Linq;

namespace Diary.ViewModels;

public class LoginViewModel : ViewModelBase
{
    private Database _db;
    private Helpers _hp;
    private string _username;
    private string _password;
    private User _user;

    public LoginViewModel(Database db, Helpers hp)
    {
        _db = db;
        _hp = hp;

        IObservable<bool> usernameOk = this.WhenAnyValue(
            x => x.Username,
            x => !string.IsNullOrEmpty(x) && CheckUsername());

        IObservable<bool> passwordOk = this.WhenAnyValue(
            x => x.Password,
            x => !string.IsNullOrEmpty(x) && CheckPassword());
        IObservable<bool> okEnabled = passwordOk
            .Concat(usernameOk);

        Login = ReactiveCommand.Create(CreateUser, okEnabled);
        Exit = ReactiveCommand.Create(() => { });
    }

    public User CreateUser()
    {
        User currentUser = _db.GetUser(Username);

        return new User
        {
            Id = currentUser.Id,
            FirstName = currentUser.FirstName,
            LastName = currentUser.LastName,
            Email = currentUser.Email,
            Username = currentUser.Username,
            Password = currentUser.Password
        };
    }
in this file, I initialize the private Database _db; and in the main method of the class, LoginViewModel, I bring the database in as a parameter, and set _db = db; for use throughout the class as shown in the above code. This threw the error Member 'Database.GetUser(string)' cannot be accessed with an instance reference; qualify it with a type name instead. So i changed it to Database.GetUser(...) and it seems to like it just fine. So my question is, what's the difference between _db.GetUser() and Database.GetUser()? 🤷‍♂️
Was this page helpful?