Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

XML
<!--Pivot Control-->
        <phone:pivot title="Bible In A Year" xmlns:phone="#unknown">
            <!--Pivot item one-->
            <phone:pivotitem header="first">
                <!--Double line list with text wrapping-->
                <grid>

                    <scrollviewer>

                        <textblock x:name="textblock1" textwrapping="Wrap" foreground="Black" xmlns:x="#unknown" />

                    </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" xmlns:x="#unknown" />

                    </scrollviewer>

                </grid>
            </phone:pivotitem>
        </phone:pivot>

C#
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
Posted
Updated 28-Jan-16 11:39am
v2
Comments
Philippe Mori 28-Jan-16 22:49pm    
It look like you start from "working" code with one text block in a single scroll viewer and now you want to have another pair... I guess you want to display distinct text from each source (BibleInYear and BibleInYear2) in each one.

You cannot simply copy existing code like that and hope to have two independent texts. You have to understand what you do.

You surely don't want to display the same text the same way twice and you surely don't want to read 732 daily text to use only the first 366 ones.

In the constructor "MainPage()" you declared the variable "index" twice:
C#
// Constructor
public MainPage()
{
InitializeComponent();

AddBibleInYear();
int index = DateTime.Now.DayOfYear;   // <<< 1st declaration
textblock1.Text = bible[index];

AddBibleInYear2();
int index = DateTime.Now.DayOfYear;   // <<< 2nd declaration
textblock2.Text = bible[index];

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

Remove the type declaration "int" from the second declaration to "just re-use" the already declared variable "index".
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 28-Jan-16 17:47pm    
5ed.
—SA
Sascha Lefèvre 28-Jan-16 18:31pm    
Thank you Sergey. First I wanted to add "easy points :-)" here - then I realized by his comment that I've overseen the second part of his question which I can only partially answer. Maybe you can help out?
Sergey Alexandrovich Kryukov 28-Jan-16 19:25pm    
I found two (identical) ReadTextFile functions in the code sample. One should be removed. However, in this case, the error message should be different, so, if after this fix something else does not work, I would need exact error messages. The offending line should be commented in source code and this comment should be referenced in the text explaining the problem. (In essence, I mean that I don't want to guess where is "line 133" :-)

I just posted a formal answer for related to this bug.

—SA
Member 10627743 28-Jan-16 17:49pm    
Sascha thank you for your reply what about "the call is ambigous between the following properties and methods"
Sascha Lefèvre 28-Jan-16 18:27pm    
Oh - sorry, I totally missed that part. You can't have two methods with the same signature (name and parameters) in a class. In this case, you try to override the method OnNavigatedTo(..) twice. Unfortunately I have no experience with Windows Phone development (if I had seen this part of your question from the start, I wouldn't have answered). So I can't tell you for sure what you could do instead to achieve what you want. Have you tried simply putting the line textblock2.Text = bible[index].ToString(); right below the similar one in the "first" OnNavigatedTo(..) method? (The .ToString() here isn't neccessary BTW, because bible[index] already is a string.)
In addition to Solution 1:

I found two (identical) ReadTextFile methods in source code. One should be removed. See also my comments to Solution 1.

You need to fix the problems explained in Solution 1 and 2 and then look for remaining problems.

—SA
 
Share this answer
 
v2
In addition to solutions 1 and 2, I think that two lists should be used for bible if there should be 2 distincts text each day from each source.

Having said that, there is also a lot of possible improvement regarding code reuse (very similar functions) where a common function with some extra arguments would be preferable.

Cut and paste programming is a very bad habit. It is even worst since the application is intended for device with limited memory.

DRY principle (Don't repeat yourself - Wikipedia, the free encyclopedia[^]) is a well know principle in software industry.

Using a dictionary instead of a list for the daily text could somewhat simplify the handling of leap years.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900