variable scope in c#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike P

    variable scope in c#

    I'm calling this procedure from a custom validator OnServerValidat e
    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(o bject sender,
    System.Web.UI.W ebControls.Serv erValidateEvent Args value)
    {
    bool blnValid = false;
    DateTime DateFrom, DateTo;
    int intErrorCount = 0;

    if (SelectLog.Item s[0].Selected)
    {

    blnValid = true;
    }
    else
    {
    try
    {
    DateFrom = Convert.ToDateT ime(DayFrom.Sel ectedItem.Value
    + "/" + MonthFrom.Selec tedItem.Value + "/" +
    YearFrom.Select edItem.Value);
    }
    catch
    {
    intErrorCount += 1;
    }

    try
    {
    DateTo = Convert.ToDateT ime(DayTo.Selec tedItem.Value +
    "/" + MonthTo.Selecte dItem.Value + "/" + YearTo.Selected Item.Value);
    }
    catch
    {
    intErrorCount += 2;
    }

    if (intErrorCount == 1)
    {
    valCustom.Error Message = "Please enter a valid Date
    From";
    blnValid = false;
    value.IsValid = blnValid;
    return;
    }
    else if (intErrorCount == 2)
    {
    valCustom.Error Message = "Please enter a valid Date To";
    blnValid = false;
    value.IsValid = blnValid;
    return;
    }
    else if (intErrorCount == 3)
    {
    valCustom.Error Message = "Please enter a valid Date From
    and Date To";
    blnValid = false;
    value.IsValid = blnValid;
    return;
    }
    else
    {
    if (DateFrom > DateTo)
    {
    valCustom.Error Message = "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 http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Christof Nordiek

    #2
    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.DateTim e... 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@telcoelect ronics.co.uk> schrieb im Newsbeitrag
    news:O33LM$9PDH A.3088@TK2MSFTN GP10.phx.gbl...[color=blue]
    > I'm calling this procedure from a custom validator OnServerValidat e
    > 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(o bject sender,
    > System.Web.UI.W ebControls.Serv erValidateEvent Args value)
    > {
    > bool blnValid = false;
    > DateTime DateFrom, DateTo;
    > int intErrorCount = 0;
    >
    > if (SelectLog.Item s[0].Selected)
    > {
    >
    > blnValid = true;
    > }
    > else
    > {
    > try
    > {
    > DateFrom = Convert.ToDateT ime(DayFrom.Sel ectedItem.Value
    > + "/" + MonthFrom.Selec tedItem.Value + "/" +
    > YearFrom.Select edItem.Value);
    > }
    > catch
    > {
    > intErrorCount += 1;
    > }
    >
    > try
    > {
    > DateTo = Convert.ToDateT ime(DayTo.Selec tedItem.Value +
    > "/" + MonthTo.Selecte dItem.Value + "/" + YearTo.Selected Item.Value);
    > }
    > catch
    > {
    > intErrorCount += 2;
    > }
    >
    > if (intErrorCount == 1)
    > {
    > valCustom.Error Message = "Please enter a valid Date
    > From";
    > blnValid = false;
    > value.IsValid = blnValid;
    > return;
    > }
    > else if (intErrorCount == 2)
    > {
    > valCustom.Error Message = "Please enter a valid Date To";
    > blnValid = false;
    > value.IsValid = blnValid;
    > return;
    > }
    > else if (intErrorCount == 3)
    > {
    > valCustom.Error Message = "Please enter a valid Date From
    > and Date To";
    > blnValid = false;
    > value.IsValid = blnValid;
    > return;
    > }
    > else
    > {
    > if (DateFrom > DateTo)
    > {
    > valCustom.Error Message = "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 http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it![/color]


    Comment

    • William F. Robertson, Jr.

      #3
      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.ToDateT ime( date );
      }
      catch
      {
      return false;
      }
      return true;
      }

      And then in you event handler:

      if ( !ConvertDate( DayFrom.Selecte dItem.Value + "/" +
      MonthFrom.Selec tedItem.Value + "/" + YearFrom.Select edItem.Value,
      DateFrom ) )
      intErrorCount += 1;


      and so on.

      HTH,

      bill






      "Mike P" <mrp@telcoelect ronics.co.uk> wrote in message
      news:O33LM$9PDH A.3088@TK2MSFTN GP10.phx.gbl...[color=blue]
      > I'm calling this procedure from a custom validator OnServerValidat e
      > 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(o bject sender,
      > System.Web.UI.W ebControls.Serv erValidateEvent Args value)
      > {
      > bool blnValid = false;
      > DateTime DateFrom, DateTo;
      > int intErrorCount = 0;
      >
      > if (SelectLog.Item s[0].Selected)
      > {
      >
      > blnValid = true;
      > }
      > else
      > {
      > try
      > {
      > DateFrom = Convert.ToDateT ime(DayFrom.Sel ectedItem.Value
      > + "/" + MonthFrom.Selec tedItem.Value + "/" +
      > YearFrom.Select edItem.Value);
      > }
      > catch
      > {
      > intErrorCount += 1;
      > }
      >
      > try
      > {
      > DateTo = Convert.ToDateT ime(DayTo.Selec tedItem.Value +
      > "/" + MonthTo.Selecte dItem.Value + "/" + YearTo.Selected Item.Value);
      > }
      > catch
      > {
      > intErrorCount += 2;
      > }
      >
      > if (intErrorCount == 1)
      > {
      > valCustom.Error Message = "Please enter a valid Date
      > From";
      > blnValid = false;
      > value.IsValid = blnValid;
      > return;
      > }
      > else if (intErrorCount == 2)
      > {
      > valCustom.Error Message = "Please enter a valid Date To";
      > blnValid = false;
      > value.IsValid = blnValid;
      > return;
      > }
      > else if (intErrorCount == 3)
      > {
      > valCustom.Error Message = "Please enter a valid Date From
      > and Date To";
      > blnValid = false;
      > value.IsValid = blnValid;
      > return;
      > }
      > else
      > {
      > if (DateFrom > DateTo)
      > {
      > valCustom.Error Message = "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 http://www.developersdex.com ***
      > Don't just participate in USENET...get rewarded for it![/color]


      Comment

      • Kevin Spencer

        #4
        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
        All hotels in Benidorm. The best selection of Benidorm hotels with reviews and maps. Book in advance and save.

        Big things are made up of
        lots of Little things.

        "Christof Nordiek" <cnordiek@amexu s.com> wrote in message
        news:bdsbjg$sgl $01$1@news.t-online.com...[color=blue]
        > 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[/color]
        time.[color=blue]
        > 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.DateTim e... 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@telcoelect ronics.co.uk> schrieb im Newsbeitrag
        > news:O33LM$9PDH A.3088@TK2MSFTN GP10.phx.gbl...[color=green]
        > > I'm calling this procedure from a custom validator OnServerValidat e
        > > 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(o bject sender,
        > > System.Web.UI.W ebControls.Serv erValidateEvent Args value)
        > > {
        > > bool blnValid = false;
        > > DateTime DateFrom, DateTo;
        > > int intErrorCount = 0;
        > >
        > > if (SelectLog.Item s[0].Selected)
        > > {
        > >
        > > blnValid = true;
        > > }
        > > else
        > > {
        > > try
        > > {
        > > DateFrom = Convert.ToDateT ime(DayFrom.Sel ectedItem.Value
        > > + "/" + MonthFrom.Selec tedItem.Value + "/" +
        > > YearFrom.Select edItem.Value);
        > > }
        > > catch
        > > {
        > > intErrorCount += 1;
        > > }
        > >
        > > try
        > > {
        > > DateTo = Convert.ToDateT ime(DayTo.Selec tedItem.Value +
        > > "/" + MonthTo.Selecte dItem.Value + "/" + YearTo.Selected Item.Value);
        > > }
        > > catch
        > > {
        > > intErrorCount += 2;
        > > }
        > >
        > > if (intErrorCount == 1)
        > > {
        > > valCustom.Error Message = "Please enter a valid Date
        > > From";
        > > blnValid = false;
        > > value.IsValid = blnValid;
        > > return;
        > > }
        > > else if (intErrorCount == 2)
        > > {
        > > valCustom.Error Message = "Please enter a valid Date To";
        > > blnValid = false;
        > > value.IsValid = blnValid;
        > > return;
        > > }
        > > else if (intErrorCount == 3)
        > > {
        > > valCustom.Error Message = "Please enter a valid Date From
        > > and Date To";
        > > blnValid = false;
        > > value.IsValid = blnValid;
        > > return;
        > > }
        > > else
        > > {
        > > if (DateFrom > DateTo)
        > > {
        > > valCustom.Error Message = "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 http://www.developersdex.com ***
        > > Don't just participate in USENET...get rewarded for it![/color]
        >
        >[/color]


        Comment

        Working...