What is wrong with this?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mick Walker

    #1

    What is wrong with this?

    string eAddress;
    try
    {
    adapter.Fill(my Dataset);
    foreach (DataRow row in myDataset.Table s[0].Rows)
    {


    if (row["EmailAddre ss"] != DBNull.Value)
    eAddress = (string)row["EmailAddre ss"];
    myMLst.Add(eAdd ress );
    }
    }
    .... etc etc

    I get the error message:

    Error 6 Use of unassigned local variable 'eAddress'



  • Mick Walker

    #2
    Re: What is wrong with this?

    Mick Walker wrote:
    string eAddress;
    try
    {
    adapter.Fill(my Dataset);
    foreach (DataRow row in myDataset.Table s[0].Rows)
    {
    >
    >
    if (row["EmailAddre ss"] != DBNull.Value)
    eAddress = (string)row["EmailAddre ss"];
    myMLst.Add(eAdd ress );
    }
    }
    ... etc etc
    >
    I get the error message:
    >
    Error 6 Use of unassigned local variable 'eAddress'
    >
    >
    >
    Oh and MLst as defined as:
    List<stringmyML st = new List<string>();

    Comment

    • Peter Duniho

      #3
      Re: What is wrong with this?

      Mick Walker wrote:
      [...]
      I get the error message:
      >
      Error 6 Use of unassigned local variable 'eAddress'
      Just what the error says. You've used "eAddress" without guaranteeing
      to the compiler that you've assigned something to it first. The
      compiler has no way to know whether the first time through your loop the
      clause in the if() statement will be true or not. Since it might be
      false, you might reach the next line without having assigned something
      to eAddress and that's an error.

      See "definitely assigned" in MSDN for more details.

      Pete

      Comment

      • Niels Ull

        #4
        Re: What is wrong with this?

        Try adding braces:

        if (row["EmailAddre ss"] != DBNull.Value)
        {
        eAddress = (string)row["EmailAddre ss"];
        myMLst.Add(eAdd ress );
        }
        Mick Walker wrote:
        >
        >string eAddress;
        >try
        >{
        >adapter.Fill(m yDataset);
        >foreach (DataRow row in myDataset.Table s[0].Rows)
        >{
        >if (row["EmailAddre ss"] != DBNull.Value)
        >eAddress = (string)row["EmailAddre ss"];
        >myMLst.Add(eAd dress );
        >}
        >}
        >... etc etc
        >I get the error message:
        >>
        >Error 6 Use of unassigned local variable 'eAddress'
        >>
        Oh and MLst as defined as:
        List<stringmyML st = new List<string>();

        Comment

        • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

          #5
          RE: What is wrong with this?

          To simplify:
          string address = String.Empty

          Now "address" is not unassigned.
          Cheers,
          -- Peter
          Recursion: see Recursion
          site: http://www.eggheadcafe.com
          unBlog: http://petesbloggerama.blogspot.com
          BlogMetaFinder: http://www.blogmetafinder.com



          "Mick Walker" wrote:
          string eAddress;
          try
          {
          adapter.Fill(my Dataset);
          foreach (DataRow row in myDataset.Table s[0].Rows)
          {
          >
          >
          if (row["EmailAddre ss"] != DBNull.Value)
          eAddress = (string)row["EmailAddre ss"];
          myMLst.Add(eAdd ress );
          }
          }
          .... etc etc
          >
          I get the error message:
          >
          Error 6 Use of unassigned local variable 'eAddress'
          >
          >
          >
          >

          Comment

          • Ebbe Kristensen

            #6
            Re: What is wrong with this?

            Peter Bromberg [C# MVP] wrote:
            To simplify:
            string address = String.Empty
            >
            Now "address" is not unassigned.
            Correct - and sub-optimal because now 'myMLst.Add()' will have to contain a
            check for an empty address, a check that already has been performed in the
            preceding if-statement.

            Ebbe
            Cheers,
            -- Peter
            Recursion: see Recursion
            site: http://www.eggheadcafe.com
            unBlog: http://petesbloggerama.blogspot.com
            BlogMetaFinder: http://www.blogmetafinder.com
            >
            >
            >
            "Mick Walker" wrote:
            >
            >string eAddress;
            > try
            > {
            > adapter.Fill(my Dataset);
            > foreach (DataRow row in myDataset.Table s[0].Rows)
            > {
            >>
            >>
            > if (row["EmailAddre ss"] != DBNull.Value)
            > eAddress = (string)row["EmailAddre ss"];
            > myMLst.Add(eAdd ress );
            > }
            > }
            >.... etc etc
            >>
            >I get the error message:
            >>
            >Error 6 Use of unassigned local variable 'eAddress'

            Comment

            • Peter Duniho

              #7
              Re: What is wrong with this?

              Ebbe Kristensen wrote:
              Peter Bromberg [C# MVP] wrote:
              >To simplify:
              >string address = String.Empty
              >>
              >Now "address" is not unassigned.
              >
              Correct - and sub-optimal because now 'myMLst.Add()' will have to contain a
              check for an empty address, a check that already has been performed in the
              preceding if-statement.
              Why would you say that? The check in the if() statement doesn't check
              the variable, it checks the database row. The statement using the
              variable executes whether the database row includes that value or not.
              It's a bug to execute that line of code without initialization the
              variable first.

              Now, it may well be a bug to execute the line of code when the database
              row's value is missing as well. But that's a separate issue; the code
              posted has clear semantics, even if they are wrong. You can't just go
              making changes to the basic behavior of the code, not without confirming
              with the OP that that's a reasonable thing to do.

              Pete

              Comment

              Working...