Get/set properties, list index out of range

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2034 Views - Last Post: 10 April 2012 - 01:24 AM Rate Topic: -----

#1 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

Get/set properties, list index out of range

Posted 09 April 2012 - 03:59 AM

        public class GlobalVars()
     {
        private List<string> TextList = new List<string>(); 

        public string GetTextList(int index)
        {
            return TextList[index]; 
        }

        public void SetTextList(string value)
        {
            TextList.Add(value);
        }
     }



Now, above is the code. What I do, at first, is I run the SetTextList and it adds a single string value to the textlist, I've seen this happen.

However, once it comes to GetTextList, index is = 0, but the list is of the size zero when it should be at one. So I get an ArgumentOutofRangeExceptionUnhandled because the index "0" is out of range. Any idea how I can fix this?

Is This A Good Question/Topic? 0
  • +

Replies To: Get/set properties, list index out of range

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Get/set properties, list index out of range

Posted 09 April 2012 - 05:02 AM

Show the code you are using when you get the error.
Was This Post Helpful? 1
  • +
  • -

#3 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

Re: Get/set properties, list index out of range

Posted 09 April 2012 - 06:42 AM

View Posteclipsed4utoo, on 09 April 2012 - 05:02 AM, said:

Show the code you are using when you get the error.


Righto, here it is:

        public partial class AnotherClass : PhoneApplicationPage
    {
        GlobalVars gVars = new GlobalVars();
        
        public AnotherClass()
        {
            textBlockmyTextBlock.Text = gVars.GetTextList(gVars.GetIndex());
            InitializeComponent();
        }
      }


This post has been edited by Tenderfoot: 09 April 2012 - 06:43 AM

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,189
  • Joined: 02-June 10

Re: Get/set properties, list index out of range

Posted 09 April 2012 - 06:43 AM

The code for the class looks fine.
So I'd believe where you are using the class within your application has the error.
My GUESS would be that you have made a new instance of the class instead of keeping a single instance.
Are you making a new instance in the method that sets the value, and another new instance in the method that gets the value? You can't make the instance inside a method because it will go out of scope as soon as the method ends. You need to make the instance at a class-level of your application and not at a method-level.

Its what we call "scope". A thing exists between the {} it was created in. So if you make a thing with a for{} loop, that's its scope. If you want to access a thing for the entire class, then define it in the class braces

class someClass
{
   int yogi = 5; // Accessible to the class
   
   void someMethod
   {
        int booboo = 3; // Accessible to the method

        while(true)
        {
           string Ranger = "Smith"; // Accessible to the loop
        }
   }
}





Eclipsed is right though, that we need to see the code where you are using this class in order to give you more help.

I would also suggest you use better names for your methods. GetTextList is inaccurate because it doesn't get the list: It gets one entry from the list. So its closer to GetListElement. It might seem like a small distinction right now, but later as the program grows and you realize you do need a method for retrieving the entire list (maybe to iterate through it using a loop for example) that the perfect name is taken. Causing you to eventually have to two methods with very similar sounding names that do very different tasks.
Was This Post Helpful? 1
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

Re: Get/set properties, list index out of range

Posted 09 April 2012 - 07:30 AM

In the code you've shown, you're calling GetTextList without ever calling SetTextList first. So of course it's out of bounds - the list is empty.
Was This Post Helpful? 1
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Get/set properties, list index out of range

Posted 09 April 2012 - 07:42 AM

As sepp2k states, you are never actually adding to the list. So how exactly are you expecting to get an item from a list if you never actually add anything to the list?

Also, your code is going to fail.

    public partial class AnotherClass : PhoneApplicationPage
{
    GlobalVars gVars = new GlobalVars();
    
    public AnotherClass()
    {
        textBlockmyTextBlock.Text = gVars.GetTextList(gVars.GetIndex());
        InitializeComponent();
    }
  }



You are trying to access the TextBlock before it's created. The InitializeComponent method is what actually creates all of the form objects. You should NEVER put code before that method call in the constructor.

The constructor should look like this..

    public AnotherClass()
    {
        InitializeComponent();
        textBlockmyTextBlock.Text = gVars.GetTextList(gVars.GetIndex());
    }


Was This Post Helpful? 1
  • +
  • -

#7 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

Re: Get/set properties, list index out of range

Posted 09 April 2012 - 10:57 AM

View Postsepp2k, on 09 April 2012 - 07:30 AM, said:

In the code you've shown, you're calling GetTextList without ever calling SetTextList first. So of course it's out of bounds - the list is empty.


I set it elsewhere - I was just trying to keep the code as brief as possible, but I see now that I probably should have been more thorough. But still, thanks for all the replies, I'm going to look into all of this and upvote your posts in a second.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,189
  • Joined: 02-June 10

Re: Get/set properties, list index out of range

Posted 09 April 2012 - 11:04 AM

View PostTenderfoot, on 09 April 2012 - 11:57 AM, said:

I set it elsewhere


Can we please see the entire method where you do the setting?
Was This Post Helpful? 1
  • +
  • -

#9 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Get/set properties, list index out of range

Posted 09 April 2012 - 11:15 AM

View PostTenderfoot, on 09 April 2012 - 01:57 PM, said:

View Postsepp2k, on 09 April 2012 - 07:30 AM, said:

In the code you've shown, you're calling GetTextList without ever calling SetTextList first. So of course it's out of bounds - the list is empty.


I set it elsewhere - I was just trying to keep the code as brief as possible, but I see now that I probably should have been more thorough. But still, thanks for all the replies, I'm going to look into all of this and upvote your posts in a second.


I don't know where "elsewhere" would be, and you create a new instance as part of the form, and you try to access it in the constructor. There aren't many places you could set it before the constructor is expecting the value to be there.
Was This Post Helpful? 0
  • +
  • -

#10 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

Re: Get/set properties, list index out of range

Posted 09 April 2012 - 11:32 AM

View PosttlhIn`toq, on 09 April 2012 - 11:04 AM, said:

View PostTenderfoot, on 09 April 2012 - 11:57 AM, said:

I set it elsewhere


Can we please see the entire method where you do the setting?


Sure, here it is.

        public partial class AddTextToList: PhoneApplicationPage
    {
        GlobalVars gVars = new GlobalVars(); 

        public AddTextToList()
        {
            InitializeComponent();
        }

        private void buttonNext_Click(object sender, RoutedEventArgs e)
        {
            gVars.SetTextList(textBoxTextToInsert.Text); 
            NavigationService.Navigate(new Uri("/AddTextToList.xaml", UriKind.Relative));
        }

        private void buttonDone_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }



I was attempting to simplify this by changing the name of the list to textList and the names of the pages and whatnot, for you guys, but I believe I've just made it more complex this way.

However, I do do a lot of navigating, and I have this line:

GlobalVars gVars = new GlobalVars(); 



On several of the pages, so maybe I'm resetting all the global vars over and over again by accident.
Was This Post Helpful? 0
  • +
  • -

#11 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

Re: Get/set properties, list index out of range

Posted 09 April 2012 - 11:38 AM

And to add to that:

GlobalVars gVars = new GlobalVars();



Is always set at the beginning of the public partial class that inherits from the PhoneApplicationPage.
Was This Post Helpful? 0
  • +
  • -

#12 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

Re: Get/set properties, list index out of range

Posted 09 April 2012 - 11:46 AM

Okay the new code you posted, doesn't call GetTextList at all? So are you saying you have one class that calls SetTextList and another unrelated class that calls GetTextList? Both on their own instance variable? Because of course that won't work.

If you call SetTextList on one instance of GlobalVars and then GetTextList on another instance of GlobalVars, you will get an index out of range error. Calling a method on one instance, only affects that one instance (unless you're accessing static variables, which you're not). The other instance's list is still empty.
Was This Post Helpful? 1
  • +
  • -

#13 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

Re: Get/set properties, list index out of range

Posted 09 April 2012 - 11:50 AM

View Postsepp2k, on 09 April 2012 - 11:46 AM, said:

Okay the new code you posted, doesn't call GetTextList at all? So are you saying you have one class that calls SetTextList and another unrelated class that calls GetTextList? Both on their own instance variable? Because of course that won't work.

If you call SetTextList on one instance of GlobalVars and then GetTextList on another instance of GlobalVars, you will get an index out of range error. Calling a method on one instance, only affects that one instance (unless you're accessing static variables, which you're not). The other instance's list is still empty.


Thanks a lot - that's what I was looking for. Changing them to static variables seems to have done the trick.

Would also like to thank eclipsed4utoo and tlhIn`toq, you guys are both incredible and I appreciate you taking your time to reply.
Was This Post Helpful? 1
  • +
  • -

#14 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Get/set properties, list index out of range

Posted 09 April 2012 - 11:58 AM

If you are looking to keep data around(in a class structure) in the app between pages, you could use a Singleton class.

http://dotnet.dzone....tern-vs-static-
Was This Post Helpful? 3
  • +
  • -

#15 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

Re: Get/set properties, list index out of range

Posted 09 April 2012 - 12:07 PM

View PostTenderfoot, on 09 April 2012 - 08:50 PM, said:

Changing them to static variables seems to have done the trick.


I only added the bit about static variables, to make my statement correct. It wasn't meant as a recommendation. Using mutable static variables like this is a design smell.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2