Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9064005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:08:22+00:00 2026-06-16T16:08:22+00:00

I have a class with a List and I am accessing it in another

  • 0

I have a class with a List and I am accessing it in another classes method. That works fine but when I attempt to use the List in a do-loop I always get a “ArgumentOutOfRangeExcetpion”. I’m really confused why using the List inside a do-loop caused it to be empty or at least that’s what I think is happening. If some one could explain to me why this is happening it would be very enlightening and maybe I could figure out how I’m gonna get this to work.

Here is my Method:

private void ListToAttributes()
    {
        int count = -1;

        //Finds Attribute starting Line.
        do
        {
            count = count + 1;

        } while (Player.PlayerList[count] != "Attributes;");

        //Adds all Attributes to a list
        List<string> TypeAndAmount = new List<string>();

        do
        {
            TypeAndAmount.Add(Player.PlayerList[count]);
            count = count + 1;
        } while (Player.PlayerList[count] != ".");

        //Sorts by type and adds amount to Player Class


        foreach (string Line in TypeAndAmount)
        {
            string[] Type = Line.Split(' ');

            if (Type[0] == "Mind") { Player.Mind = int.Parse(Type[1]); }
            if (Type[0] == "Body") { Player.Body = int.Parse(Type[1]); }
            if (Type[0] == "Soul") { Player.Soul = int.Parse(Type[1]); }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:08:26+00:00Added an answer on June 16, 2026 at 4:08 pm

    You first loop will cause an ArgumentOutOfRangeException if no item in the list matches the string. In this case you will happily walk past the end of the list. Your second loop has the same issue.

    Further I think you have to increment count one more time between the two loops – currently you are adding the item "Attributes;" to the list TypeAndAmount. This will also cause an ArgumentOutOfRangeException because splitting the string "Attributes;" at spaces yields only one string and you are accessing Type[1] in your last loop.

    I suggest rewriting your code using some LINQ (including the step to skip the item "Attributes;").

    // Skip everything until we find 'Attributes;'.
    var stepOne = Player.PlayerList.SkipWhile(entry => entry != "Attributes;");
    
    // Skip over 'Attributes;'.
    var stepTwo = stepOne.Skip(1);
    
    // Take everything until we find '.'.
    var stepThree = stepTwo.TakeWhile(entry => entry != ".");
    
    // Stuff everything we selected into a new list.
    var typeAndAmount = stepThree.ToList();
    

    You can also put everything into one statement – less verbose but harder to debug.

    var typeAndAmount = Player.PlayerList
                              .SkipWhile(entry => entry != "Attributes;")
                              .Skip(1)
                              .TakeWhile(entry => entry != ".")
                              .ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that wraps List<> I have GetValue by index method: public
I'm using protobuf-net v2 and have a class that inherits List that I wan't
What is the preferred method of accessing ivars from different classes? Application Delegate Class
I have a Python App Engine web app class that I am accessing with
I want to implement custom JSP list tag, but have problem with accessing properties
I have a table in C# and ASP.net that references a list of another
I have class A and list of A objects. A has a function f
I have a class like this: public class Contest { List<ContestTeam> Teams { get;
For instance, if I have an class: public class StuffHolder { List<Stuff> myList; public
I have class A with a public std::list<int> object list_ . Class B ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.