0
votes

I have this code:

    public void ABtn()
    {
        if (ABtnState == "E")
        {
            phrase.Points += -(int)Settings.ABtn;
            ABtnState = "D";
        }
        else
        {
            phrase.Points += (int)Settings.ABtn;
            ABtnState = "E";
        }
        if (BBtnState == "E")
        {
            phrase.Points += -(int)Settings.BBtn;
            BBtnState = "D";
        }
        if (CBtnState == "E") { phrase.Points += -(int)Settings.CBtn; CBtnState = "D"; }
        if (DBtnState == "E") { phrase.Points += -(int)Settings.DBtn; DBtnState = "D"; }
        if (EBtnState == "E") { phrase.Points += -(int)Settings.EBtn; EBtnState = "D"; }
        App.DB.SetPoints(phrase.PhraseId, phrase.Points);
        App.viewablePhrases = App.DB.GetViewablePhrases(Settings.Mode, Settings.Pts, true);
        Cif2aInfo = new String('☆', phrase.Points);
    }

And appcenter is reporting the following crash during ABtn:

DeckTabViewModel.ABtn () System.ArgumentOutOfRangeException: Count cannot be less than zero. Parameter name: count

With this stacktrace

Xamarin Exception Stack: System.ArgumentOutOfRangeException: Count cannot be less than zero. Parameter name: count at System.String.Ctor (System.Char c, System.Int32 count) [0x0000d] in <3ad100fe60d44e1c8a81197ba1997e7e>:0 at System.String.CreateString (System.Char c, System.Int32 count) [0x00000] in <3ad100fe60d44e1c8a81197ba1997e7e>:0 at (wrapper managed-to-managed) System.String..ctor(char,int) at Japanese.DeckTabViewModel.ABtn () [0x00178] in <75f30b75b8d1435e80e8b7703a671cac>:0 at Xamarin.Forms.Command+<>c__DisplayClass4_0.<.ctor>b__0 (System.Object o) [0x00000] in <46a6a42f4aa4494a9a4f56ec3eee8423>:0 at Xamarin.Forms.Command.Execute (System.Object parameter) [0x00000] in <46a6a42f4aa4494a9a4f56ec3eee8423>:0 at Xamarin.Forms.ButtonElement.ElementClicked (Xamarin.Forms.VisualElement visualElement, Xamarin.Forms.Internals.IButtonElement ButtonElementManager) [0x0001a] in <46a6a42f4aa4494a9a4f56ec3eee8423>:0 at Xamarin.Forms.Button.SendClicked () [0x00000] in <46a6a42f4aa4494a9a4f56ec3eee8423>:0 at Xamarin.Forms.Platform.Android.ButtonElementManager.OnClick (Xamarin.Forms.VisualElement element, Xamarin.Forms.IButtonController buttonController, Android.Views.View v) [0x00003] in :0 at Xamarin.Forms.Platform.Android.FastRenderers.ButtonRenderer.Android.Views.View.IOnClickListener.OnClick (Android.Views.View v) [0x0000c] in :0 at Android.Views.View+IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_v) [0x00011] in :0 at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.60(intptr,intptr,intptr)

Does anyone have any ideas what could be causing this or how I could rectify the problem?

1
Cif2aInfo = new String('☆', phrase.Points); What value has phrase.Points here?Silvermind
as error said count at System.String.Ctor. The problem is for new String('☆', phrase.Points);.Hamed Moghadasi

1 Answers

1
votes

From Microsoft Docs:

String(Char c, Int32 count) Initializes the new instance to the value indicated by a specified Unicode character repeated a specified number of times

So if your phrase.Points less than 0, it will trow you that error, try this in the C# Online Compiler:

string Cif2aInfo = new String('☆', -1);
Console.WriteLine(Cif2aInfo);

You need to check the value before that like this:

Cif2aInfo = phrase.Points != null && phrase.Points > 0 ? new String('☆', phrase.Points) : string.Empty;