© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
33 replies
Kye

Foreach loop inside List<T> class

Okay so I'm actually quite confused on this. I have a collection of roles that I'd like to store inside a JSON file. My goal is to obtain every role's information within a guild by first obtaining the RoleCollection and then initiating a foreach loop to add it into a class list so later on I can serialize that information into a JSON file.

        public class GuildInformation
        {
            public ulong GuildID { get; set; }
            public string GuildName { get; set; }
            public string GuildDescription { get; set; }
            public string GuildAvatarUrl { get; set; }
            public string GuildBannerUrl { get; set; }
            public string GuildSplashUrl { get; set; }
            public string GuildDiscoveryUrl { get; set; }
            public string GuildVanityUrl { get; set; }
            public string GuildCreationDate { get; set; }
            public ulong GuildOwnerID { get; set; }
            public string GuildOwnerName { get; set; }
            public int GuildMfaLevel { get; set; }
            public int GuildNsfwLevel { get; set; }
            public int GuildVerificationLevel { get; set; }
            public ulong GuildRulesChannel { get; set; }
            public ulong GuildSafetyChannel { get; set; }
            public ulong GuildPublicChannel { get; set; }
            public ulong GuildModRuleChannel { get; set; }
            public GuildPermissions GuildPermission { get; set; }
        }

        public class GuildPermissions
        {
            public List<GuildRolePermissions> GuildRolePermission { get; set; }
            public List<GuildChannelPermissions> GuildPermissionPermission { get; set; }
        }

        public class GuildRolePermissions
        {
            public string GuildRoleName { get; set; }
            public ulong GuildRoleID { get; set; }
            public int GuildRolePermissionLevel { get; set; }
        }

        public GuildInformation CreateGuildInformation( - In here I store the parameters for GuildInformation -, SocketGuild Guild)
        {
            var GuildRoleCollection = new List<GuildRolePermissions>();

                var GuildInformation = new GuildInformation()
            {
                    GuildID = GuildID,
                    GuildName = GuildName,
                    // etc...
            
                    GuildPermissions = new()
                    {
                        foreach (var GuildRole in Guild.Roles
                        {
                                var AddRole = new GuildRolePermission
                                {
                                    GuildRoleName = GuildRole.Name,
                                    GuildRoleID = GuildRole.Id,
                                    GuildRolePermissionLevel = 0
                                }
                        };
                        GuildRoleCollection.Add(AddRole);
                    }
            };
            return GuildInformation;
        }
        public class GuildInformation
        {
            public ulong GuildID { get; set; }
            public string GuildName { get; set; }
            public string GuildDescription { get; set; }
            public string GuildAvatarUrl { get; set; }
            public string GuildBannerUrl { get; set; }
            public string GuildSplashUrl { get; set; }
            public string GuildDiscoveryUrl { get; set; }
            public string GuildVanityUrl { get; set; }
            public string GuildCreationDate { get; set; }
            public ulong GuildOwnerID { get; set; }
            public string GuildOwnerName { get; set; }
            public int GuildMfaLevel { get; set; }
            public int GuildNsfwLevel { get; set; }
            public int GuildVerificationLevel { get; set; }
            public ulong GuildRulesChannel { get; set; }
            public ulong GuildSafetyChannel { get; set; }
            public ulong GuildPublicChannel { get; set; }
            public ulong GuildModRuleChannel { get; set; }
            public GuildPermissions GuildPermission { get; set; }
        }

        public class GuildPermissions
        {
            public List<GuildRolePermissions> GuildRolePermission { get; set; }
            public List<GuildChannelPermissions> GuildPermissionPermission { get; set; }
        }

        public class GuildRolePermissions
        {
            public string GuildRoleName { get; set; }
            public ulong GuildRoleID { get; set; }
            public int GuildRolePermissionLevel { get; set; }
        }

        public GuildInformation CreateGuildInformation( - In here I store the parameters for GuildInformation -, SocketGuild Guild)
        {
            var GuildRoleCollection = new List<GuildRolePermissions>();

                var GuildInformation = new GuildInformation()
            {
                    GuildID = GuildID,
                    GuildName = GuildName,
                    // etc...
            
                    GuildPermissions = new()
                    {
                        foreach (var GuildRole in Guild.Roles
                        {
                                var AddRole = new GuildRolePermission
                                {
                                    GuildRoleName = GuildRole.Name,
                                    GuildRoleID = GuildRole.Id,
                                    GuildRolePermissionLevel = 0
                                }
                        };
                        GuildRoleCollection.Add(AddRole);
                    }
            };
            return GuildInformation;
        }


Is something like this possible? I'd like to do this way so upon the system detecting a new guild, it stores basic guild information and each role (to set up a permission-like system for commands)
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ Foreach loop
C#CC# / help
4y ago
foreach loop index error
C#CC# / help
2y ago
❔ problems with foreach loop
C#CC# / help
3y ago