?
Solved

c# error message:The name 'TotalDays3' does not exist in the current context

Posted on 2007-08-12
2
Medium Priority
?
2,070 Views
Last Modified: 2013-11-05
Hi experts, I am getting the following error in c# with visual web developer 2005 express edition.
My code is at the end, I do realize I have not added any namespace for type class libraries such as:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
Compiler Error Message: CS0103: The name 'TotalDays3' does not exist in the current context
Source Error:
Line 114:            Response.Write("TotalDays:");
Line 115:            Response.Write("#");
Line 116:            Response.Write(TotalDays3);
Line 117:            Response.Write("#");
Line 118:        }
 The complete code is as follows, it works if line 116 is deleted.
<script runat="server">
    void Page_Load(Object Src, EventArgs E)
    {
        if (!Page.IsPostBack)
        {
            ArrayList YAL = new ArrayList();
            ArrayList MAL = new ArrayList();
            ArrayList DAL = new ArrayList();
            int i = 0;
            int a = 1900;
            int j = 0;
            int b = 1;
            int k = 0;
            int c = 1;

            ArrayList YALE = new ArrayList();
            ArrayList MALE = new ArrayList();
            ArrayList DALE = new ArrayList();
            int l = 0;
            int d = 1900;
            int m = 0;
            int e = 1;
            int n = 0;
            int f = 1;
           
            while (i < 110)
            {
                YAL.Add(a++);
                i++;
            }
            Dropdownlist1.Items.Add("");
            Dropdownlist1.DataSource = YAL;
            Dropdownlist1.DataBind();
            while (l < 110)
            {
                YALE.Add(d++);
                l++;
            }
            Dropdownlist4.Items.Add("");
            Dropdownlist4.DataSource = YALE;
            Dropdownlist4.DataBind();
           
            while (j < 12)
            {
                MAL.Add(b++);
                j++;
            }
            Dropdownlist2.Items.Add("");
            Dropdownlist2.DataSource = MAL;
            Dropdownlist2.DataBind();
            while (m < 12)
            {
                MALE.Add(e++);
                m++;
            }
            Dropdownlist5.Items.Add("");
            Dropdownlist5.DataSource = MALE;
            Dropdownlist5.DataBind();
           
            while (k < 30)
            {
                DAL.Add(c++);
                k++;
            }
            Dropdownlist3.Items.Add("");
            Dropdownlist3.DataSource = DAL;
            Dropdownlist3.DataBind();
            while (n < 30)
            {
                DALE.Add(f++);
                n++;
            }
            Dropdownlist6.Items.Add("");
            Dropdownlist6.DataSource = DALE;
            Dropdownlist6.DataBind();

            long years = 0;
            years = Convert.ToInt64(Dropdownlist1.SelectedItem.Value);
            years = (years - 1900) * 365;
            ///Response.Write(years + "days + ");
            long months = 0;
            months = Convert.ToInt64(Dropdownlist2.SelectedItem.Value);
            months = months * 30;
            ///Response.Write(months + "days + ");
            long days = 0;
            days = Convert.ToInt64(Dropdownlist3.SelectedItem.Value);
            ///Response.Write(days + "days = ");
            long TotalDays = 0;
            TotalDays = years + months + days;
            ///Response.Write(TotalDays + "Total days");
            ///Response.Write("            ");
            long years2 = 0;
            years2 = Convert.ToInt64(Dropdownlist4.SelectedItem.Value);
            years2 = (years2 - 1900) * 365;
            ///Response.Write(years2 + "days + ");
            long months2 = 0;
            months2 = Convert.ToInt64(Dropdownlist5.SelectedItem.Value);
            months2 = months2 * 30;
            ///Response.Write(months2 + "days + ");
            long days2 = 0;
            days2 = Convert.ToInt64(Dropdownlist6.SelectedItem.Value);
            ///Response.Write(days2 + "days = ");
            long TotalDays2 = 0;
            TotalDays2 = years2 + months2 + days2;
           
            long TotalDays3 = 0;
            TotalDays3 = TotalDays2 - TotalDays;
            ///output3.Text = "TotalDays:" + TotalDays2.ToString();
            ///Response.Write("<br/><br/><br/><br/><br/><br/><br/>" + "TotalDays:" + TotalDays3);
        }
        else
        {
            Response.Write("TotalDays:");
            Response.Write("#");
            Response.Write(TotalDays3);
            Response.Write("#");
        }
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        output.Text = "Day:" + Dropdownlist3.SelectedItem.Value + " month:" + Dropdownlist2.SelectedItem.Value + " year:" + Dropdownlist1.SelectedItem.Value + " ";
    }
    protected void Button2_Click2(object sender, EventArgs e)
    {
        output2.Text = "Day:" + Dropdownlist6.SelectedItem.Value + " month:" + Dropdownlist5.SelectedItem.Value + " year:" + Dropdownlist4.SelectedItem.Value;
    }
    </script>
<html>
<head>
<title>Date Test</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ValidationSummary id="validSummary" runat="server"
headerText="*** Errors On Your Form ***"
showSummary="True"
displayMode="List" />
    <br />
<asp:DropDownList runat="server" id="Dropdownlist1" />
<asp:DropDownList runat="server" id="Dropdownlist2" />
<asp:DropDownList runat="server" id="Dropdownlist3" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />
<asp:Label runat="server" id="output" />

<asp:DropDownList runat="server" id="Dropdownlist4" />
<asp:DropDownList runat="server" id="Dropdownlist5" />
<asp:DropDownList runat="server" id="Dropdownlist6" />
<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click2" />
<asp:Label runat="server" id="output2" />
    <br />
    <br />
<asp:Label runat="server" id="output3" />
</form>
</body>
</html>

0
Comment
Question by:homeshopper
2 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1000 total points
ID: 19679220
C# has very strict variable scoping rules.  You declared the variable inside the if block, and tried to access it in the else--no can do.

You would have to move the declaration outside of the if block.

      long TotalDays3 = 0;

Bob


0
 

Author Comment

by:homeshopper
ID: 19679297
Thanks Bob,
I have moved alot of the code around as you suggested.
It now works displaying two dates and the number of days in between.
I have listed the revised code below:
<%@ Page Language="C#" Debug="true" Trace="False"%>
<script runat="server">
    void Page_Load(Object Src, EventArgs E)
    {
        long years = 0;
        long years2 = 0;
        long months2 = 0;
        long months = 0;
        long days2 = 0;
        long days = 0;
        long TotalDays = 0;
        long TotalDays2 = 0;
        long TotalDays3 = 0;
        if (!Page.IsPostBack)
        {
            ArrayList YAL = new ArrayList();
            ArrayList MAL = new ArrayList();
            ArrayList DAL = new ArrayList();
            int i = 0;
            int a = 1900;
            int j = 0;
            int b = 1;
            int k = 0;
            int c = 1;

            ArrayList YALE = new ArrayList();
            ArrayList MALE = new ArrayList();
            ArrayList DALE = new ArrayList();
            int l = 0;
            int d = 1900;
            int m = 0;
            int e = 1;
            int n = 0;
            int f = 1;
           
            while (i < 110)
            {
                YAL.Add(a++);
                i++;
            }
            Dropdownlist1.Items.Add("");
            Dropdownlist1.DataSource = YAL;
            Dropdownlist1.DataBind();
            while (l < 110)
            {
                YALE.Add(d++);
                l++;
            }
            Dropdownlist4.Items.Add("");
            Dropdownlist4.DataSource = YALE;
            Dropdownlist4.DataBind();
           
            while (j < 12)
            {
                MAL.Add(b++);
                j++;
            }
            Dropdownlist2.Items.Add("");
            Dropdownlist2.DataSource = MAL;
            Dropdownlist2.DataBind();
            while (m < 12)
            {
                MALE.Add(e++);
                m++;
            }
            Dropdownlist5.Items.Add("");
            Dropdownlist5.DataSource = MALE;
            Dropdownlist5.DataBind();
           
            while (k < 30)
            {
                DAL.Add(c++);
                k++;
            }
            Dropdownlist3.Items.Add("");
            Dropdownlist3.DataSource = DAL;
            Dropdownlist3.DataBind();
            while (n < 30)
            {
                DALE.Add(f++);
                n++;
            }
            Dropdownlist6.Items.Add("");
            Dropdownlist6.DataSource = DALE;
            Dropdownlist6.DataBind();

            years = Convert.ToInt64(Dropdownlist1.SelectedItem.Value);
           
            months = Convert.ToInt64(Dropdownlist2.SelectedItem.Value);
           
            days = Convert.ToInt64(Dropdownlist3.SelectedItem.Value);
           
            years2 = Convert.ToInt64(Dropdownlist4.SelectedItem.Value);
           
            months2 = Convert.ToInt64(Dropdownlist5.SelectedItem.Value);
           
            days2 = Convert.ToInt64(Dropdownlist6.SelectedItem.Value);
        }
        else
        {
            years = Convert.ToInt64(Dropdownlist1.SelectedItem.Value);
            years = (years - 1900) * 365;
            months = Convert.ToInt64(Dropdownlist2.SelectedItem.Value);
            months = months * 30;
            days = Convert.ToInt64(Dropdownlist3.SelectedItem.Value);
            years2 = Convert.ToInt64(Dropdownlist4.SelectedItem.Value);
            years2 = (years2 - 1900) * 365;
            months2 = Convert.ToInt64(Dropdownlist5.SelectedItem.Value);
            months2 = months2 * 30;
            days2 = Convert.ToInt64(Dropdownlist6.SelectedItem.Value);
            TotalDays = years + months + days;
            TotalDays2 = years2 + months2 + days2;
            TotalDays3 = TotalDays2 - TotalDays;
            Response.Write("TotalDays:");
            Response.Write("#");
            Response.Write(TotalDays3);
            Response.Write("#");
        }
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        output.Text = "Day:" + Dropdownlist3.SelectedItem.Value + " month:" + Dropdownlist2.SelectedItem.Value + " year:" + Dropdownlist1.SelectedItem.Value + " ";
    }
    protected void Button2_Click2(object sender, EventArgs e)
    {
        output2.Text = "Day:" + Dropdownlist6.SelectedItem.Value + " month:" + Dropdownlist5.SelectedItem.Value + " year:" + Dropdownlist4.SelectedItem.Value;
    }
    </script>
<html>
<head>
<title>Date Test</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ValidationSummary id="validSummary" runat="server"
headerText="*** Errors On Your Form ***"
showSummary="True"
displayMode="List" />
    <br />
<asp:DropDownList runat="server" id="Dropdownlist1" />
<asp:DropDownList runat="server" id="Dropdownlist2" />
<asp:DropDownList runat="server" id="Dropdownlist3" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />
<asp:Label runat="server" id="output" />

<asp:DropDownList runat="server" id="Dropdownlist4" />
<asp:DropDownList runat="server" id="Dropdownlist5" />
<asp:DropDownList runat="server" id="Dropdownlist6" />
<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click2" />
<asp:Label runat="server" id="output2" />
    <br />
    <br />
<asp:Label runat="server" id="output3" />
</form>
</body>
</html>
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question