variable scope in c#

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default variable scope in c#

    I'm calling this procedure from a custom validator OnServerValidate
    event. I keep getting the error :

    Use of unassigned local variable 'DateFrom'

    I'm new to c# so I'm not really sure about the syntax I'm using. But it
    seems the problem is that the DateFrom and DateTo values get lost
    somewhere between being initialised and the line if (DateFrom > DateTo).
    Can anybody help me out?

    public void ValidateDates(object sender,
    System.Web.UI.WebControls.ServerValidateEventArgs value)
    {
    bool blnValid = false;
    DateTime DateFrom, DateTo;
    int intErrorCount = 0;

    if (SelectLog.Items[0].Selected)
    {

    blnValid = true;
    }
    else
    {
    try
    {
    DateFrom = Convert.ToDateTime(DayFrom.SelectedItem.Value
    + "/" + MonthFrom.SelectedItem.Value + "/" +
    YearFrom.SelectedItem.Value);
    }
    catch
    {
    intErrorCount += 1;
    }

    try
    {
    DateTo = Convert.ToDateTime(DayTo.SelectedItem.Value +
    "/" + MonthTo.SelectedItem.Value + "/" + YearTo.SelectedItem.Value);
    }
    catch
    {
    intErrorCount += 2;
    }

    if (intErrorCount == 1)
    {
    valCustom.ErrorMessage = "Please enter a valid Date
    From";
    blnValid = false;
    value.IsValid = blnValid;
    return;
    }
    else if (intErrorCount == 2)
    {
    valCustom.ErrorMessage = "Please enter a valid Date To";
    blnValid = false;
    value.IsValid = blnValid;
    return;
    }
    else if (intErrorCount == 3)
    {
    valCustom.ErrorMessage = "Please enter a valid Date From
    and Date To";
    blnValid = false;
    value.IsValid = blnValid;
    return;
    }
    else
    {
    if (DateFrom > DateTo)
    {
    valCustom.ErrorMessage = "The date in the Date To
    field must be the same or later than the date in the Date From field";
    blnValid = false;
    }
    else
    {


    blnValid = true;
    }
    }
    }
    value.IsValid = blnValid;
    return;
    }



    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    Mike P Guest

  2. Similar Questions and Discussions

    1. PHP variable scope bug in CT
      I have a page where I declare PHP variables which are then used inside an include file. When I try to edit the page in Contribute, I get a notice...
    2. Variable scope
      What type of variable can you use that lasts between page refreshes is it possible or do i have to use session or a hidden field thanks
    3. [PHP-DEV][1] Variable Scope
      Thank you for the first decent response. I concede. On Sunday, Aug 31, 2003, at 07:09 America/New_York, Zeev Suraski wrote: -- PHP Internals...
    4. [PHP-DEV] Variable Scope
      Variable scope is mediocre at best. For instances: $array = array(1, 2, 3, 4, 5); for ($i = 0; $i < 5; $i++) { $num = $array; echo $num;...
    5. Re[2]: [PHP-DEV] Variable Scope
      Hello LingWitt, - PHP is typeless - for doesn't span any declaration level and hence does not have its own symbol table - PHP is not c, not C++...
  3. #2

    Default Re: variable scope in c#

    Hi Mike,

    Your problem is with definite assignment; that means, it's a compiletime
    error in C# if you read a variable that could be not assigned at that time.
    The Problem in your code is, that the combile doesn't consider possible
    variable values in flow control, so it doesn't know that the statement
    if(DateFrom > DateTo) is unreachable if the assignment DateFrom =
    Convert.DateTime... does fail.
    A solution would be to initally assign the varibles:

    DateTime DateFrom = new DateTime(), DateTo = new DateTime();

    with that your Code should succeed.

    cn

    "Mike P" <mrp@telcoelectronics.co.uk> schrieb im Newsbeitrag
    news:O33LM$9PDHA.3088@TK2MSFTNGP10.phx.gbl...
    > I'm calling this procedure from a custom validator OnServerValidate
    > event. I keep getting the error :
    >
    > Use of unassigned local variable 'DateFrom'
    >
    > I'm new to c# so I'm not really sure about the syntax I'm using. But it
    > seems the problem is that the DateFrom and DateTo values get lost
    > somewhere between being initialised and the line if (DateFrom > DateTo).
    > Can anybody help me out?
    >
    > public void ValidateDates(object sender,
    > System.Web.UI.WebControls.ServerValidateEventArgs value)
    > {
    > bool blnValid = false;
    > DateTime DateFrom, DateTo;
    > int intErrorCount = 0;
    >
    > if (SelectLog.Items[0].Selected)
    > {
    >
    > blnValid = true;
    > }
    > else
    > {
    > try
    > {
    > DateFrom = Convert.ToDateTime(DayFrom.SelectedItem.Value
    > + "/" + MonthFrom.SelectedItem.Value + "/" +
    > YearFrom.SelectedItem.Value);
    > }
    > catch
    > {
    > intErrorCount += 1;
    > }
    >
    > try
    > {
    > DateTo = Convert.ToDateTime(DayTo.SelectedItem.Value +
    > "/" + MonthTo.SelectedItem.Value + "/" + YearTo.SelectedItem.Value);
    > }
    > catch
    > {
    > intErrorCount += 2;
    > }
    >
    > if (intErrorCount == 1)
    > {
    > valCustom.ErrorMessage = "Please enter a valid Date
    > From";
    > blnValid = false;
    > value.IsValid = blnValid;
    > return;
    > }
    > else if (intErrorCount == 2)
    > {
    > valCustom.ErrorMessage = "Please enter a valid Date To";
    > blnValid = false;
    > value.IsValid = blnValid;
    > return;
    > }
    > else if (intErrorCount == 3)
    > {
    > valCustom.ErrorMessage = "Please enter a valid Date From
    > and Date To";
    > blnValid = false;
    > value.IsValid = blnValid;
    > return;
    > }
    > else
    > {
    > if (DateFrom > DateTo)
    > {
    > valCustom.ErrorMessage = "The date in the Date To
    > field must be the same or later than the date in the Date From field";
    > blnValid = false;
    > }
    > else
    > {
    >
    >
    > blnValid = true;
    > }
    > }
    > }
    > value.IsValid = blnValid;
    > return;
    > }
    >
    >
    >
    > *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    > Don't just participate in USENET...get rewarded for it!

    Christof Nordiek Guest

  4. #3

    Default Re: variable scope in c#

    With C# if you assign something during a try block, it isn't guaranteed the
    operation will perform, so the compiler thinks the DateFrom and DateTo
    fields are unassigned.

    I would suggest having a ConvertDate function that

    private bool ConvertDate( string date, ref DateTime dt )
    {
    try
    {
    dt = Convert.ToDateTime( date );
    }
    catch
    {
    return false;
    }
    return true;
    }

    And then in you event handler:

    if ( !ConvertDate( DayFrom.SelectedItem.Value + "/" +
    MonthFrom.SelectedItem.Value + "/" + YearFrom.SelectedItem.Value,
    DateFrom ) )
    intErrorCount += 1;


    and so on.

    HTH,

    bill






    "Mike P" <mrp@telcoelectronics.co.uk> wrote in message
    news:O33LM$9PDHA.3088@TK2MSFTNGP10.phx.gbl...
    > I'm calling this procedure from a custom validator OnServerValidate
    > event. I keep getting the error :
    >
    > Use of unassigned local variable 'DateFrom'
    >
    > I'm new to c# so I'm not really sure about the syntax I'm using. But it
    > seems the problem is that the DateFrom and DateTo values get lost
    > somewhere between being initialised and the line if (DateFrom > DateTo).
    > Can anybody help me out?
    >
    > public void ValidateDates(object sender,
    > System.Web.UI.WebControls.ServerValidateEventArgs value)
    > {
    > bool blnValid = false;
    > DateTime DateFrom, DateTo;
    > int intErrorCount = 0;
    >
    > if (SelectLog.Items[0].Selected)
    > {
    >
    > blnValid = true;
    > }
    > else
    > {
    > try
    > {
    > DateFrom = Convert.ToDateTime(DayFrom.SelectedItem.Value
    > + "/" + MonthFrom.SelectedItem.Value + "/" +
    > YearFrom.SelectedItem.Value);
    > }
    > catch
    > {
    > intErrorCount += 1;
    > }
    >
    > try
    > {
    > DateTo = Convert.ToDateTime(DayTo.SelectedItem.Value +
    > "/" + MonthTo.SelectedItem.Value + "/" + YearTo.SelectedItem.Value);
    > }
    > catch
    > {
    > intErrorCount += 2;
    > }
    >
    > if (intErrorCount == 1)
    > {
    > valCustom.ErrorMessage = "Please enter a valid Date
    > From";
    > blnValid = false;
    > value.IsValid = blnValid;
    > return;
    > }
    > else if (intErrorCount == 2)
    > {
    > valCustom.ErrorMessage = "Please enter a valid Date To";
    > blnValid = false;
    > value.IsValid = blnValid;
    > return;
    > }
    > else if (intErrorCount == 3)
    > {
    > valCustom.ErrorMessage = "Please enter a valid Date From
    > and Date To";
    > blnValid = false;
    > value.IsValid = blnValid;
    > return;
    > }
    > else
    > {
    > if (DateFrom > DateTo)
    > {
    > valCustom.ErrorMessage = "The date in the Date To
    > field must be the same or later than the date in the Date From field";
    > blnValid = false;
    > }
    > else
    > {
    >
    >
    > blnValid = true;
    > }
    > }
    > }
    > value.IsValid = blnValid;
    > return;
    > }
    >
    >
    >
    > *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    > Don't just participate in USENET...get rewarded for it!

    William F. Robertson, Jr. Guest

  5. #4

    Default Re: variable scope in c#

    I agree. However, I would assign a value of null to the variables, as they
    will have values assigned to them, and initializing an instance of a class
    would therefore waste processor cycles.

    HTH,

    Kevin Spencer
    Microsoft FrontPage MVP
    Internet Developer
    [url]http://www.takempis.com[/url]
    Big things are made up of
    lots of Little things.

    "Christof Nordiek" <cnordiek@amexus.com> wrote in message
    news:bdsbjg$sgl$01$1@news.t-online.com...
    > Hi Mike,
    >
    > Your problem is with definite assignment; that means, it's a compiletime
    > error in C# if you read a variable that could be not assigned at that
    time.
    > The Problem in your code is, that the combile doesn't consider possible
    > variable values in flow control, so it doesn't know that the statement
    > if(DateFrom > DateTo) is unreachable if the assignment DateFrom =
    > Convert.DateTime... does fail.
    > A solution would be to initally assign the varibles:
    >
    > DateTime DateFrom = new DateTime(), DateTo = new DateTime();
    >
    > with that your Code should succeed.
    >
    > cn
    >
    > "Mike P" <mrp@telcoelectronics.co.uk> schrieb im Newsbeitrag
    > news:O33LM$9PDHA.3088@TK2MSFTNGP10.phx.gbl...
    > > I'm calling this procedure from a custom validator OnServerValidate
    > > event. I keep getting the error :
    > >
    > > Use of unassigned local variable 'DateFrom'
    > >
    > > I'm new to c# so I'm not really sure about the syntax I'm using. But it
    > > seems the problem is that the DateFrom and DateTo values get lost
    > > somewhere between being initialised and the line if (DateFrom > DateTo).
    > > Can anybody help me out?
    > >
    > > public void ValidateDates(object sender,
    > > System.Web.UI.WebControls.ServerValidateEventArgs value)
    > > {
    > > bool blnValid = false;
    > > DateTime DateFrom, DateTo;
    > > int intErrorCount = 0;
    > >
    > > if (SelectLog.Items[0].Selected)
    > > {
    > >
    > > blnValid = true;
    > > }
    > > else
    > > {
    > > try
    > > {
    > > DateFrom = Convert.ToDateTime(DayFrom.SelectedItem.Value
    > > + "/" + MonthFrom.SelectedItem.Value + "/" +
    > > YearFrom.SelectedItem.Value);
    > > }
    > > catch
    > > {
    > > intErrorCount += 1;
    > > }
    > >
    > > try
    > > {
    > > DateTo = Convert.ToDateTime(DayTo.SelectedItem.Value +
    > > "/" + MonthTo.SelectedItem.Value + "/" + YearTo.SelectedItem.Value);
    > > }
    > > catch
    > > {
    > > intErrorCount += 2;
    > > }
    > >
    > > if (intErrorCount == 1)
    > > {
    > > valCustom.ErrorMessage = "Please enter a valid Date
    > > From";
    > > blnValid = false;
    > > value.IsValid = blnValid;
    > > return;
    > > }
    > > else if (intErrorCount == 2)
    > > {
    > > valCustom.ErrorMessage = "Please enter a valid Date To";
    > > blnValid = false;
    > > value.IsValid = blnValid;
    > > return;
    > > }
    > > else if (intErrorCount == 3)
    > > {
    > > valCustom.ErrorMessage = "Please enter a valid Date From
    > > and Date To";
    > > blnValid = false;
    > > value.IsValid = blnValid;
    > > return;
    > > }
    > > else
    > > {
    > > if (DateFrom > DateTo)
    > > {
    > > valCustom.ErrorMessage = "The date in the Date To
    > > field must be the same or later than the date in the Date From field";
    > > blnValid = false;
    > > }
    > > else
    > > {
    > >
    > >
    > > blnValid = true;
    > > }
    > > }
    > > }
    > > value.IsValid = blnValid;
    > > return;
    > > }
    > >
    > >
    > >
    > > *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    > > Don't just participate in USENET...get rewarded for it!
    >
    >

    Kevin Spencer Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139