0

I have a code to read data block inside MIFARE card.

The method rfidM1.ReadDataFromCardM1 will read a block and return value in string.

string memQuery = string.Empty;
int i = 0, j = 0;
sector = 4;
block = 4;

for (i = 0; i < block; i++)
{
    for (j = 0; j < sector; j++)
    {                  
        memQuery += rfidM1.ReadDataFromCardM1(Convert.ToByte(j), Convert.ToByte(i), _Key1) + ",";
    }
}          

My intention is concating memQuery with comma. Example output here:

,0,,,,,True,,C0-12320,0,,,,,,

I concat memQuery with various ways, for example, using +=, StringBuilder or ArrayList but they didn't work because it always has an output like this when I put it in MessageBox.

,0

It looks like string after that 0 cannot concat with other string after it. Why?

  • 1
    Try and use a `List` (which contains real strings) instead of an `ArrayList` (which contains just `object`s) – Hans Kesting May 04 '18 at 08:44
  • I'm getting confused with that `memQuery.Add`. Is that a collection? Try refactoring your code. What are the `variables` and values you want to concat to that string? `K` ? You're near it smells weird near that `.ToString().Replace()` . Try creating different methods for each purpose so it will be easier to debug the problem. – Gonzo345 May 04 '18 at 08:46
  • OT the usual way to declare an index variable of a `for` statement is inside it: `for (int i=0; ...`. And you could also declare it as `byte` if that is what you really need – Hans Kesting May 04 '18 at 08:50
  • @HansKesting I tried it with List and got the same result. – Veerakran Sereerungruangkul May 04 '18 at 08:53
  • @Gonzo345 Sorry I forgot to remove `.ToString().Replace()`. The `k` is for increment the index of `memQuery[k]`. memQuery in the code is ArrayList. When I need to see its value, I `foreach` it with `temptext += foreachData + ","` then `MessageBox.Show(temptext)` after it complete foreach – Veerakran Sereerungruangkul May 04 '18 at 08:58
  • @bommelding `ReadDataFromCardM1` return `string` value. I think I should reformat my question. – Veerakran Sereerungruangkul May 04 '18 at 09:00
  • 1
    if `memQuery[k]` returns null, you will get a NullReferenceException when trying to execute any method on it, such as GetType() or ToString(). Could that `ReadDataFromCardM1` return `null`s or control characters? – Hans Kesting May 04 '18 at 09:00
  • @HansKesting Yes, turned out there are control characters in returned string. I follow this thread https://stackoverflow.com/a/15259355/4535654 to remove them and everything fine now. Thank you. – Veerakran Sereerungruangkul May 04 '18 at 09:13
  • `,0` cannot possible be the output of the code you've shown. The resulting string will always end in a `,` as per the code posted, regardless of the other values you're using. – Flater May 04 '18 at 10:05
  • control characters - was that why you tried to remove backslashes (in a previous version of you question)? Those were not backslashes, but the debugger showing the control chars in a way that you could use in source, such as `\t` instead of a TAB – Hans Kesting May 04 '18 at 11:40
  • @HansKesting Yes, I put .Replace() because I thought it can remove backslash from control chars and reveal them on MessageBox. How silly of me. – Veerakran Sereerungruangkul May 04 '18 at 12:12

1 Answers1

1

My intention is concating memQuery with comma

Wel, first get rid of the ArrayList and replace it with var memQuery = new List<string>();.

Inside your for-loops, decide what to with null or empty results. Add a null or skip the Add or ...

And then when the memQuery is filled correctly, you can do

 string result = string.Join(",", memQuery);

string.Join() can handle nulls in the input sequence.

bommelding
  • 2,969
  • 9
  • 14