1
Reply

A local variable index is already defined in this scope

JOHN JOHNNNY

JOHN JOHNNNY

Jan 28 2016 5:25 PM
495

Hi

I am using pivot template in my app to develop an app and in my xaml.cs i will like to
initialize more than one index but am getting error is there any way i can do this without getting 
the following errors "a local variable index is already defined in this scope" and
"the call is ambigous between the following properties and methods"
I am doing this because my text is getting truncated at the bottom of the textblock. 
i want the overflow text to continue in pivot template. see my code below

xaml

<!--Pivot Control-->
        <phone:Pivot Title="Bible In A Year">
            <!--Pivot item one-->
            <phone:PivotItem Header="first">
                <!--Double line list with text wrapping-->
                <Grid >

                    <ScrollViewer >

                        <TextBlock x:Name="textblock1" TextWrapping="Wrap" Foreground="Black" />

                    </ScrollViewer>

                </Grid>
            </phone:PivotItem>

            <!--Pivot item two-->
            <phone:PivotItem Header="second">
                <!--Double line list no text wrapping-->
                <Grid >

                    <ScrollViewer >

                        <TextBlock x:Name="textblock2" TextWrapping="Wrap" Foreground="Black" />

                    </ScrollViewer>

                </Grid>
            </phone:PivotItem>
        </phone:Pivot>

xaml.cs

namespace My_SolnForTruncation
{
    public partial class MainPage : PhoneApplicationPage
    {
        //Bible In A Year text strings code begins
        List<String> bible = new List<string>();

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            AddBibleInYear();
            int index = DateTime.Now.DayOfYear;
            textblock1.Text = bible[index];

            AddBibleInYear2();
            int index = DateTime.Now.DayOfYear;
            textblock2.Text = bible[index];

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        // Bible In A Year code continues
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            DateTime dt = DateTime.Now;
            int month = dt.Month;
            int year = dt.Year;
            int index;

            if (DateTime.IsLeapYear(year) || (month <= 2))
            {
                index = dt.DayOfYear - 1; // list is indexed from 0
            }
            else
            {
                index = dt.DayOfYear; // add a day
            }

            textblock1.Text = bible[index].ToString(); // or some other property
        }


        private void AddBibleInYear()
        {
            for (int i = 1; i <= 366; i++)
            {
                string filePath = Path.GetFullPath("BibleInYear/Bible" + i.ToString() + ".txt");

                if (File.Exists(filePath))
                {
                    bible.Add(ReadTextFile(filePath));
                }
                else
                {
                    bible.Add(string.Format("BibleInYear file '{0}' was not found.", i));
                }
            }
        }

        public string ReadTextFile(string textFilePath)
        {
            string text = null;

            using (StreamReader r = new StreamReader(textFilePath))
            {
                text = r.ReadToEnd();
            }

            return text;
        }

        // Bible In A Year Part 2 code continues
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            DateTime dt = DateTime.Now;
            int month = dt.Month;
            int year = dt.Year;
            int index;

            if (DateTime.IsLeapYear(year) || (month <= 2))
            {
                index = dt.DayOfYear - 1; // list is indexed from 0
            }
            else
            {
                index = dt.DayOfYear; // add a day
            }

            textblock2.Text = bible[index].ToString(); // or some other property
        }


        private void AddBibleInYear2()
        {
            for (int i = 1; i <= 366; i++)
            {
                string filePath = Path.GetFullPath("BibleInYear2/Bible" + i.ToString() + ".txt");

                if (File.Exists(filePath))
                {
                    bible.Add(ReadTextFile(filePath));
                }
                else
                {
                    bible.Add(string.Format("BibleInYear2 file '{0}' was not found.", i));
                }
            }
        }

        public string ReadTextFile(string textFilePath)
        {
            string text = null;

            using (StreamReader r = new StreamReader(textFilePath))
            {
                text = r.ReadToEnd();
            }

            return text;
        }


Kindly help


Answers (1)