mardi 2 mai 2017

how do I convert string to int?

I'm messing around with xamarin, and ran into this problem. I'm making a dice rolling application for phones in C#. But, I can't figure out how to convert an Android.Widget.EditText to a int so I can run the diceRollNorm method.

using Android.App;
using Android.Widget;
using Android.OS;
using System;

namespace TesticleApp
{
    [Activity(Label = "Dice Roller", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            // Add new content
            // Turn the design widgets into a variable that you can manipulate
            EditText diceSize = FindViewById<EditText>(Resource.Id.diceSizeNum);
            EditText numOfRolls = FindViewById<EditText>(Resource.Id.numOfRolls);
            CheckBox averageBox = FindViewById<CheckBox>(Resource.Id.checkBox1);
            CheckBox totalBox = FindViewById<CheckBox>(Resource.Id.checkBox2);
            CheckBox TAABox = FindViewById<CheckBox>(Resource.Id.checkBox3);
            TextView Output = FindViewById<TextView>(Resource.Id.textView1);

            Output = ToString(diceRollNorm(diceSize));
        }

        /// <summary>
        /// This is the function that actual "Rolls the dice", it takes DSize as the how many sides the "Dice" have.
        /// The numRoll is the amount of times you would roll the die
        /// </summary>
        /// <param name="DSize"></param>
        /// <param name="numRoll"></param>
        /// <returns></returns>
        public int diceRollNorm(int DSize, int numRoll)
        {
            Random rnd = new Random();
            Array Total;

            for (int i = 1; i <= numRoll; i++)
            {
                int dice = rnd.Next(1, DSize + 1);
                // Find way to insert output of the dice
                return dice;
            }
            return 0;
        }

        /// <summary>
        /// The overload function for diceRollNorm()
        /// </summary>
        /// <param name="Dsize"></param>
        /// <returns></returns>
        public int diceRollNorm(int Dsize)
        {
            Random rnd = new Random();

            int dice = rnd.Next(1, Dsize + 1);

            return dice;
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire