C
C#4mo ago
Mekasu0124

✅ 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
};
}
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()? 🤷‍♂️
5 Replies
canton7
canton74mo ago
What is Database? Is that a type which you've made?
Mekasu0124
Mekasu01244mo ago
I have a database class if that's what you're asking
Jimmacle
Jimmacle4mo ago
that error means it's a static method you call static methods by qualifying them with a type name instead of an instance
Pobiega
Pobiega4mo ago
Static members don't belong to the instance of the class, they belong to the class itself. Because of that, you must call them via the class instead of via an instance of the class So you should probably take a look at the Database class and decide if it should be static or not
Mekasu0124
Mekasu01244mo ago
oh ok. I got you. thanks!