variable scope in c#

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

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;
}
 
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
 
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
 
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
http://www.takempis.com
Big things are made up of
lots of Little things.

Christof Nordiek said:
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 said:
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;
}
 
Back
Top