✅ need help with class

Heres my code, Im kinda confused.
39 Replies
monkeyoohlala
monkeyoohlalaOP4w ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Media3D;

namespace PassApp
{

class Accounts
{
private string name = "";
private string website = "";
private string login = "";
private string password = "";
List<Tuple<string, string, string, string>> accountsList = new List<Tuple<string, string, string, string>>();

public Accounts(string name, string website, string login, string password)
{
this.name = name;
this.website = website;
this.login = login;
this.password = password;
}

public void addToList(Accounts accounts)
{
accountsList.Add(Tuple.Create(name, website, login, password));

var mainWindow = Application.Current.MainWindow as MainWindow;
if (mainWindow != null)
{
mainWindow.updateStatus($"Test Added {name}: {website}, login: {login} password: {password}");
}

}



public void subtractFromList(Accounts accounts)
{

}

public bool check(Accounts accountCheck)
{
var mainWindow = Application.Current.MainWindow as MainWindow;
foreach (var account in accountsList)
{
if (account.Item1 == accountCheck.name &&
account.Item2 == accountCheck.website &&
account.Item3 == accountCheck.login &&
account.Item4 == accountCheck.password)
{


return true;
}
}
if (mainWindow != null)
{
mainWindow.updateStatus($"False");
}
return false;
}



}





}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Media3D;

namespace PassApp
{

class Accounts
{
private string name = "";
private string website = "";
private string login = "";
private string password = "";
List<Tuple<string, string, string, string>> accountsList = new List<Tuple<string, string, string, string>>();

public Accounts(string name, string website, string login, string password)
{
this.name = name;
this.website = website;
this.login = login;
this.password = password;
}

public void addToList(Accounts accounts)
{
accountsList.Add(Tuple.Create(name, website, login, password));

var mainWindow = Application.Current.MainWindow as MainWindow;
if (mainWindow != null)
{
mainWindow.updateStatus($"Test Added {name}: {website}, login: {login} password: {password}");
}

}



public void subtractFromList(Accounts accounts)
{

}

public bool check(Accounts accountCheck)
{
var mainWindow = Application.Current.MainWindow as MainWindow;
foreach (var account in accountsList)
{
if (account.Item1 == accountCheck.name &&
account.Item2 == accountCheck.website &&
account.Item3 == accountCheck.login &&
account.Item4 == accountCheck.password)
{


return true;
}
}
if (mainWindow != null)
{
mainWindow.updateStatus($"False");
}
return false;
}



}





}
mg
mg4w ago
you're not going to get much help if you don't ask a question
monkeyoohlala
monkeyoohlalaOP4w ago
ok I am trying to create a list that contains 4 strings per list I dont know why my check function isnt working
mg
mg4w ago
were you the one asking about lists of tuples yesterday too
monkeyoohlala
monkeyoohlalaOP4w ago
yes you advised me to put it in a class
mg
mg4w ago
ok so you're on the right track to take those 4 strings and encapsulate them into a class but you didn't do it quite right
monkeyoohlala
monkeyoohlalaOP4w ago
yah Im kind of new to C#
mg
mg4w ago
the strings represent an account's name, website, login, and password, which you have as fields of that class
monkeyoohlala
monkeyoohlalaOP4w ago
been using java more but transitioned
mg
mg4w ago
but then you also have a list of a 4-tuple of strings in there too the Accounts class should really be the Account class and then you would have a List<Account> instead of the list of tuples and that list would be outside the class
monkeyoohlala
monkeyoohlalaOP4w ago
it should be called the Account class because I am creating one account at a time?
mg
mg4w ago
yes exactly
monkeyoohlala
monkeyoohlalaOP4w ago
oh okay
mg
mg4w ago
the class only represents one account if you want to hold multiple, you put them in a list, which you are doing, but the list shouldn't be part of the class a class to its members is a "has a" relationship. e.g. an account has a name, website, login, and password but why would an account "have a" list of accounts?
monkeyoohlala
monkeyoohlalaOP4w ago
it wouldnt
mg
mg4w ago
yup
monkeyoohlala
monkeyoohlalaOP4w ago
Im trying to make this app with by only studying udemy courses and I took some java and C#, C, C++ in school
mg
mg4w ago
are you restricting yourself to just udemy courses or are you saying that's mainly what you're following?
monkeyoohlala
monkeyoohlalaOP4w ago
im on winter break so I been studying udemy
mg
mg4w ago
here we recommend $helloworld
mg
mg4w ago
it's from microsoft who created and maintain C# and .NET, so it's your best bet
Buddy
Buddy4w ago
I've said it a lot of times before, paid courses are a scam. Youtube has the same quality as udemy courses Learn by doing while simultaneously read docs and/or forums
monkeyoohlala
monkeyoohlalaOP4w ago
I do like udemy though better than other sites and youtube videos
mg
mg4w ago
not better than microsoft learn
monkeyoohlala
monkeyoohlalaOP4w ago
ok I will save those 2 links
Buddy
Buddy4w ago
Remember, C# uses PascalCase for methods so check becomes Check and subtractFromList becomes SubtractFromList
mg
mg4w ago
and you should be using properties instead of fields for the name, website, etc.
monkeyoohlala
monkeyoohlalaOP4w ago
yeah Im kinda new to hands on experience this is my 3rd or so project
mg
mg4w ago
plenty to learn
monkeyoohlala
monkeyoohlalaOP4w ago
whats the difference between field and properties? i looked it up
mg
mg4w ago
under the hood, a property is a field and a method to get and set that field so you can, for instance, have a property with a public getter but a private setter so the property can only be changed from within the class, but can be read from outside the class
Angius
Angius4w ago
$getsetdevolve
MODiX
MODiX4w ago
class Foo
{
private int _bar;

public int GetBar()
{
return _bar;
}

public void SetBar(int bar)
{
_bar = bar;
}
}
class Foo
{
private int _bar;

public int GetBar()
{
return _bar;
}

public void SetBar(int bar)
{
_bar = bar;
}
}
can be shortened to
class Foo
{
private int _bar;

public int GetBar() => _bar;

public void SetBar(int bar) => _bar = bar;
}
class Foo
{
private int _bar;

public int GetBar() => _bar;

public void SetBar(int bar) => _bar = bar;
}
can be shortened to
class Foo
{
private int _bar;
public int Bar {
get { return _bar; }
set { _bar = value; }
}
}
class Foo
{
private int _bar;
public int Bar {
get { return _bar; }
set { _bar = value; }
}
}
can be shortened to
class Foo
{
private int _bar;
public int Bar {
get => _bar;
set => _bar = value;
}
}
class Foo
{
private int _bar;
public int Bar {
get => _bar;
set => _bar = value;
}
}
can be shortened to
class Foo
{
public int Bar { get; set; }
}
class Foo
{
public int Bar { get; set; }
}
monkeyoohlala
monkeyoohlalaOP4w ago
where would I be without you all if a property is private, how do I retrieve the value for it? private string name { get; } = name; besides making it public
mg
mg4w ago
you don't, outside of the class
monkeyoohlala
monkeyoohlalaOP4w ago
do I have to create a GetName function or is there an easier way?
mg
mg4w ago
the getter and setter are how the property is accessed if you want it to be accessible outside the class, make them public
monkeyoohlala
monkeyoohlalaOP4w ago
okay

Did you find this page helpful?