C
C#3y ago
Relevant

❔ List that has a list inside, and an identifier

ok
55 Replies
Relevant
RelevantOP3y ago
is there a question?
richrds
richrds3y ago
I can't seem to accomplish the structure I mentioned
Relevant
RelevantOP3y ago
What's wrong with your current code? Seems like it should work. Not sure if mainList should actually be a list, though, but what you have should be functional
richrds
richrds3y ago
Well, I have no way of identifying each list
Relevant
RelevantOP3y ago
Sounds like maybe you want a new class
public class Party
{
public int Id { get; set; }
public List<BasePlayer> Players {get;set;}
}
public class Party
{
public int Id { get; set; }
public List<BasePlayer> Players {get;set;}
}
richrds
richrds3y ago
How would I add this class inside a list?
Relevant
RelevantOP3y ago
var party = new Party();
party.Id = 123;
party.Players = new List<Party>();
var party = new Party();
party.Id = 123;
party.Players = new List<Party>();
using my variable names, it would be party.Players.Add(player);
richrds
richrds3y ago
I want to have the ability to go through all these player lists, (lets call these parties), so the player can request to join any party with an ID they provide Yes, that would add a new player I want to add this whole playerlist to a list, so I can sort through them easily
Relevant
RelevantOP3y ago
if (parties.Any(p => p.Id == somePartyId)
{
var selectedParty = parties.FirstOrDefault(p => p.Id == somePartyId);
selectedParty.Players.Add(player);
}
if (parties.Any(p => p.Id == somePartyId)
{
var selectedParty = parties.FirstOrDefault(p => p.Id == somePartyId);
selectedParty.Players.Add(player);
}
something like that?
richrds
richrds3y ago
Thank you, this is what I'd use to find the ID, but the question I was trying to ask is, how would I create parties? To hold all the parties
Relevant
RelevantOP3y ago
var parties = new List<Party>();
parties.Add(new Party{
Id = 123,
Players = new List<BasePlayer>()
});
var parties = new List<Party>();
parties.Add(new Party{
Id = 123,
Players = new List<BasePlayer>()
});
richrds
richrds3y ago
Spot on. Thank you so much.
Relevant
RelevantOP3y ago
sure thing! does all that make sense?
richrds
richrds3y ago
Right, var parties has to be inside an running function, but I'd want parties to be global, which means I could access it from all functions (inside single c#), how could I accomplish this?
Relevant
RelevantOP3y ago
Just declare it outside of a method
richrds
richrds3y ago
I can't declare var in a class though, only the functions that are inside of the class
Relevant
RelevantOP3y ago
don't use var, then.
class MyThing
{
private List<Party> _parties;

public MyThing()
{
_parties = new List<Party>();
}
}
class MyThing
{
private List<Party> _parties;

public MyThing()
{
_parties = new List<Party>();
}
}
richrds
richrds3y ago
I'm a bit confused, this would create 1 party inside parties, and I cannot create/Add new parties outside this class
Relevant
RelevantOP3y ago
What do you mean? That snippet doesn't create any parties, it's just an empty list You can create a method to add a new party or to add a player to an existing party
richrds
richrds3y ago
oh, it creates a List that holds PlayerLists
Relevant
RelevantOP3y ago
Could also make the parties public, but wasn't sure how you're using it to know if that's appropriate. You could also leave it private and just be able to access it through public methods like public void AddParty(Party party) and public void AddPlayerToParty(int partyId, BasePlayer player) I renamed my PlayerList type to Party in my examples. Seems like a more appropriate name
richrds
richrds3y ago
Hmm, I can't seem to access the methods I created inside the MyThing class
Relevant
RelevantOP3y ago
are they public? How are you trying to access the methods? createParty has to be inside of some class
richrds
richrds3y ago
Isn't it inside the class "Parties" already? oh, I mean yes
Relevant
RelevantOP3y ago
Oh, yeah, one is, then you have another createParty method outside of the class
richrds
richrds3y ago
the other method I have is inside an other class
Relevant
RelevantOP3y ago
oh okay
richrds
richrds3y ago
All of this code is inside another class
richrds
richrds3y ago
Relevant
RelevantOP3y ago
you can't do Parties.createParty because it's not static You need an instance of the class
Angius
Angius3y ago
You need an instance of Parties Ninja'd lol
Relevant
RelevantOP3y ago
haha
Anton
Anton3y ago
please don't go through it twice
Relevant
RelevantOP3y ago
that should work
ero
ero3y ago
this is just a dictionary
Relevant
RelevantOP3y ago
Right, that would be a better idea haha
richrds
richrds3y ago
// it's match instead of party. Okay, so the match is created successfully with the matchId Upon trying to enter the match by providing the player & matchId, it doesn't find the match and returns false, any idea why this happens?
Relevant
RelevantOP3y ago
looks like you have matches and _matches. Are you sure you're checking the correct object?
richrds
richrds3y ago
Sorry, edit mistake, I have updated it now, that's how it is in my project
Relevant
RelevantOP3y ago
Best bet would be to set a breakpoint and see what is in _matches when in gets to the addPlayerToMatch method
ero
ero3y ago
.Any() isn't what goes through twice. it's the combination of the two calls .Any() and .FirstOrDefault(). both iterate the collection to find a match like mentioned, this should really just be a dictionary though
Relevant
RelevantOP3y ago
In it's current state, I agree, but it wouldn't allow for expansion of a party definition
ero
ero3y ago
right you can still have a cache because it's empty?
richrds
richrds3y ago
There is a match that is created before this
ero
ero3y ago
you're not using that here
richrds
richrds3y ago
It's created here and returns true
ero
ero3y ago
also we have properties, use them
richrds
richrds3y ago
I'm cutting off some code, cause I don't wanna clutter everything
ero
ero3y ago
you're cutting out critical parts don't do that or you'll get questions like these this whole class seems kinda unnecessary... if it's not printing anything that's because there's nothing there to print easy as that
richrds
richrds3y ago
I was considering that I retrieve matches wrongfully, and therefore nothing prints. Anyways then, must be something wrong with createMatch()
ero
ero3y ago
also i would advise you early on to follow c# conventions public members are PascalCase actually, almost all declarations are PascalCase methods, public members (properties, and technically fields, but fields should never be public!), types (classes, structs), namespaces
richrds
richrds3y ago
Okay, so I think I have gotten to the bottom of this issue Whenever I call createMatch(int matchId), I now return _matches. Now, every time a new match is created, the returned _matches only contains the match that is created. Which leaves me to think that, every time the Matches class is used, a new list of _matches is made overwriting/ignoring the old list. How would I go around to fix this issue? Resolved. This was because I create a new instance of Matches every time I call a method. Thank you everybody, for your help & answers.
sibber
sibber3y ago
$close
MODiX
MODiX3y ago
Use the /close command to mark a forum thread as answered
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?