9 Replies - 732 Views - Last Post: 07 May 2017 - 04:09 AM

#1 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

System.FormatException: Index (zero based) must be greater than or equ

Posted 04 May 2017 - 05:14 PM

EDIT: Fixed it left out 2 things.

This post has been edited by Dialupp: 04 May 2017 - 05:22 PM

Is This A Good Question/Topic? 0
  • +

Replies To: System.FormatException: Index (zero based) must be greater than or equ

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13482
  • View blog
  • Posts: 53,820
  • Joined: 12-June 08

Re: System.FormatException: Index (zero based) must be greater than or equ

Posted 04 May 2017 - 06:12 PM

FYI - even if you fix it, keep the original post and then explain what you fixed. This helps others learn as well.
Was This Post Helpful? 0
  • +
  • -

#3 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: System.FormatException: Index (zero based) must be greater than or equ

Posted 04 May 2017 - 11:09 PM

Ah ok thanks how do I bring it back? Or should I just type it up?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13482
  • View blog
  • Posts: 53,820
  • Joined: 12-June 08

Re: System.FormatException: Index (zero based) must be greater than or equ

Posted 05 May 2017 - 06:43 AM

Just keep it in mind.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,372
  • Joined: 02-June 10

Re: System.FormatException: Index (zero based) must be greater than or equ

Posted 05 May 2017 - 03:51 PM

Email said:

Hey guys so I am reading the book creating mobile apps with xamarin, and getting this error:

System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace YoutubeApp
{
    class FontSizesPage : ContentPage
    {
        public FontSizesPage()
        {
            BackgroundColor = Color.White;
            StackLayout stackLayout = new StackLayout
            {
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions = LayoutOptions.Center
            };
            // Do the NamedSize values.
            NamedSize  ptSizes = { 4, 6, 8, 10, 12 };
            foreach (double ptSize in ptSizes)
            {
                double fontSize = resolution * ptSize / 72;
                stackLayout.Children.Add(
                new Label
                {
                    Text = String.Format("Point Size = {0} ({1:F2})", ptSize, fontSize),
                    FontSize = fontSize,
                    TextColor = Color.Black
                });
            }
            Content = stackLayout;
        }
    }
}




This line has the error:

stackLayout.Children.Add(
                new Label
                {
                    Text = String.Format("Named Size = {0} ({1:F2})"),
                    FontSize = fontSize,
                    TextColor = Color.Black
                });


So...
The original error was in your String.Format but you've cleared that up.

1:
"YouTubeApp" - Really? Are you writing an app with the intent of impersonating the official app? If not then you might consider better naming.

2:
No software development team in the grown-up world builds WPF/Xamarin UI in C#. Break this habit fast. Build your UI in XAML.

3:
You seem to be building UI with no objects to back it up. Building the UI before you have data. That's backwards but common with beginners. Everyone want to see something right away, even if it isn't good architecture and have don't have a plan on what to do next. I say this because... You're building a bunch of Labels then adding them to the stack. Ok. Then what? I mean really, wants the point? You can't do much with them. You can't use them to select a preference for the user.

UI is the last thing you build. First you build your Models and ViewModels: The logic and data. Then you can build your UI binding to that so you have some way to interact with the data. But that's all UI is: A reflection of logic and data. Its not meant to be the driving force of the logic.

If you had first created an ObservableCollection<Double> for your sizes... OR better still an ObservableCollection<FontObjects> you could have binded a ListView to the collection and used an ItemTemplate to automatically display items for all the elements of the collection. Then you'd have data... A UI that reflects that data... And a way to Select and element from the collection and do something with it; like make that the size all the rest of the UI used for display.

If it helps... http://www.RedPillXamarin.com
The series starts with the basic "Welcome to Xamarin" app and works up through bindings, commands, custom controls etc.

This post has been edited by tlhIn`toq: 05 May 2017 - 03:54 PM

Was This Post Helpful? 0
  • +
  • -

#6 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: System.FormatException: Index (zero based) must be greater than or equ

Posted 05 May 2017 - 05:28 PM

No its just a random project I'm not making a youtube app just a random name I gave it. Also you do realize that I am reading through a book and this is the code they provide? I did not write this. Also I think I'd prefer xaml instead of c# for the ui and stuff it looks better and not as confusing. but the book is using code atm.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,372
  • Joined: 02-June 10

Re: System.FormatException: Index (zero based) must be greater than or equ

Posted 06 May 2017 - 11:14 AM

> Also you do realize that I am reading through a book and this is the code they provide?
No. Why would any one here realize assume that?
Was This Post Helpful? 0
  • +
  • -

#8 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: System.FormatException: Index (zero based) must be greater than or equ

Posted 06 May 2017 - 08:44 PM

Well you recovered my previous post and look at the first line what does it say?
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,372
  • Joined: 02-June 10

Re: System.FormatException: Index (zero based) must be greater than or equ

Posted 07 May 2017 - 04:02 AM

I'm going to say this - then I'm done here.

You said you're reading a book. That tends to mean, for most people, you eead the book - learn some concepts, syntax and new commands or features. Then you go write your own code to make use of what you learned. "I'm reading a book about coding" does not mean "I'm copy/pasting my way through life." I try to give credit to someone and presume the best - that they are trying to learn, not just copy/pasting. I guess that's where I keep going wrong with this generation.

<unsubscribing from this thread now - I'm done with ya>
Was This Post Helpful? 0
  • +
  • -

#10 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: System.FormatException: Index (zero based) must be greater than or equ

Posted 07 May 2017 - 04:09 AM

Lol someone is mad for no reason.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1