-2

According to the hints in code cleanup, I do not need to define byte[] sqlImage. If that definition is removed, then I get a message that sqlImage has not been defined.

        byte[] sqlImage = new byte[0];
        sqlImage = (byte[])foundRows[0].ItemArray[2];

I would appreciate it if someone would explain what I'm missing. Thanks

Charles
  • 53
  • 7
  • 1
    Besides the duplicate, the official docs are a great place to get started: https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0059 – Camilo Terevinto Nov 18 '20 at 19:56

1 Answers1

3

you are reassigning sqlImage on second line

sqlImage = (byte[])foundRows[0].ItemArray[2];

thus, following line is unnecessary

byte[] sqlImage = new byte[0];

you can simplfy it

var sqlImage = (byte[])foundRows[0].ItemArray[2];
Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
  • In the following lines of code, I get the message about the unnecessary assignment when I define the variable for getPhotoID but not with NewID. I understand I could simplify as you showed, but that is not one of the options given. Shouldn't you define your varialbes before usinging them, which is what I've been doing for years. string getPhotoID; getPhotoID = AncestryID.ToString() + "-" + DocID.ToString(); string NewID; NewID = Convert.ToString(rowIndexOfItemUnderMouseToDrop + 1); – Charles Nov 18 '20 at 21:37
  • Thanks Dervis, that helps. – Charles Dec 03 '20 at 20:31
  • sqlImage is a byte array, which is how I defined it. Defining variables for their specific use has always been the preferred way to define variables. Defining variables at the begging of code makes it easier to understand and read code. – Charles Dec 25 '20 at 01:00
  • @Charles **Defining** and **assigning** an array are two different things. You can **define** a byte array without giving it a value. It does not make sense to **assign** a value and then immediately assign a different value on the next line. – Rainbolt Dec 25 '20 at 01:11