0

I have a string variable st and i have assign this string variable to output coming form data table by using this statement

string st;

if(dt!=null)
{
    if(dt.rows.count> 0)
    {
        st = dt.Rows[3]["timeslot_StartTime"].ToString();
    }
}

now i want to convert this string variable to date time property and i have done this one by using the below statement

DateTime pt1 = DateTime.Parse(st);

but it shows error at st saying that use of unassigned local varaible "st".

Wessel Kranenborg
  • 1,400
  • 15
  • 38
user682417
  • 1,478
  • 4
  • 25
  • 46

7 Answers7

3

Initialize st to null or string.Empty

string st = null;

and to be on the safer side, check st for null before parsing.

Bala R
  • 107,317
  • 23
  • 199
  • 210
1

Give st an initial value such as

string st = String.Empty;
Ryan
  • 26,884
  • 9
  • 56
  • 83
1

try definning st in this way

string st = "" 
JAiro
  • 5,914
  • 2
  • 22
  • 21
1

try to do that

string st = null;

check the st if it's not null before parsing

Tomasz Jaskuλa
  • 15,723
  • 5
  • 46
  • 73
0

Probably you are using the variable in a different scope from the one where it's assigned, like

string st;
if (condition) {
    st = dt.Rows[3]["timeslot_StartTime"].ToString();
}
DateTime pt1 = DateTime.Parse(st);

So, st is not always initialized (it is only if the if condition is verified). Try instead

string st;
if (condition) {
    st = dt.Rows[3]["timeslot_StartTime"].ToString();
    DateTime pt1 = DateTime.Parse(st);
}
Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193
0

You only assign st inside your if logic. If you are attempting to use st outside of these blocks, you will run into the "unassignment" error.

Either

  • Give the variable a default value when you initialize it
    • something that is parse-able, or remember to include a check before you attempt to parse
  • or attempt to parse it to a DateTime at the point where you retrieve the value from the data table

Of course, given that you are pulling the value from a DataTable, if the value is already stored in the table as a date, forget the ToString() and parsing altogether.

DateTime date = (DateTime)dt.Rows[x]["ColumnName"];
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
  • hi how to check the present time is in between two given times that is in datetime not in string – user682417 Jun 21 '11 at 15:33
  • @user, please do not ask questions in comments. If you have something that is related to the original, add it into the original. However, your question here appears to be a different piece of logic, so it should be its own *new* question. But for the sake of getting you started, compare that a given date is greater than a start date and less than an end date. Such as `bool isBetween = (startDate <= yourDate) && (yourDate <= endDate);` – Anthony Pegram Jun 21 '11 at 15:48
0

You need to initialize the string. Right now any initialization or assignment is being performed within an if block. The compiler is detecting this and considering it possibly never initialized.

string st = string.Empty;

As a side note, it is much safer to use the sister methods TryParse() for conversations to ensure that you won't have any unexpected exceptions thrown due to a invalid formatting issue. The method will return true if it is converted successfully, which makes a clean look as such:

        if (dt!=null)
        {
           if(dt.rows.count> 0)
           {
              st = dt.Rows[3]["timeslot_StartTime"].ToString();
           }
        }

        DateTime dt = DateTime.MinValue;
        if (DateTime.TryParse(st, out dt))
        {
            //was successful and do something here
        }
Matthew Cox
  • 13,566
  • 9
  • 54
  • 72