I need to show the data of a list according to the code entered, the problem is when i want to show the data that corresponds to it is shown system.collections.generic.list
var _respuesta saves the data the problem is only that dont show the number and his binary format only show "system.collections.generic.list"
the valhex and valbin variable should be saved but it is saved "system.collections.generic.list" or "nameofmyprogram.Form1.Tex.Form"
Thi is my code
private void button1_Click(object sender, EventArgs e)
{
bool ValidarEPC = true;
string Respuesta = "";
string caracter;
int LargoEPC = 0;
string varEPC = txtEPC.Text;
List<HexBin> listHexBin = new List<HexBin>()
{
new HexBin() { ValHex="0",ValBin="0000"},
new HexBin() { ValHex="1",ValBin="0001"},
new HexBin() { ValHex="2",ValBin="0010"},
new HexBin() { ValHex="3",ValBin="0011"},
new HexBin() { ValHex="4",ValBin="0100"},
new HexBin() { ValHex="5",ValBin="0101"},
new HexBin() { ValHex="6",ValBin="0110"},
new HexBin() { ValHex="7",ValBin="0111"},
new HexBin() { ValHex="8",ValBin="1000"},
new HexBin() { ValHex="9",ValBin="1001"},
new HexBin() { ValHex="A",ValBin="1010"},
new HexBin() { ValHex="B",ValBin="1011"},
new HexBin() { ValHex="C",ValBin="1100"},
new HexBin() { ValHex="D",ValBin="1101"},
new HexBin() { ValHex="E",ValBin="1110"},
new HexBin() { ValHex="F",ValBin="1111"},
};
LargoEPC = varEPC.Length;
if(LargoEPC<=25)
{
Respuesta = "";
for (int i = 0; i <= LargoEPC; i )
{
caracter = varEPC.Substring(i, 1);
//the data and its binary form are saved
var _respuesta = listHexBin.Where(ValHex => ValHex.ValHex == caracter);
//but now only show the error "system.collections.generic.list"
Respuesta= _respuesta.ToString();
MessageBox.Show(Respuesta);
}
}
else
{
ValidarEPC = false;
Respuesta = "EPC no valido";
}
string aBinario = "EPC no valido";
MessageBox.Show(LargoEPC.ToString());
lblResult.Text = varEPC.ToString();
lblResult.Refresh();
}
class HexBin
{
public string ValHex { get; set; }
public string ValBin { get; set; }
}
}
CodePudding user response:
The Object
class has a ToString()
method. Every other class inherits from Object
, so every class has a ToString()
method.
But what does that method do? Here it is:
public virtual String ToString()
{
return GetType().ToString();
}
Unless a class overrides ToString()
with some other implementation, it's just going to return the name of the type. A lot of classes override ToString()
. For example, Date
or Uri
will return a string representation of the date or URI.
List<T>
doesn't override it. And that's probably good. Imagine if the list had 300,000 items in it. The result would be a massive string you couldn't look at.
So if you want to see a string containing all the values in the list you'll have to write some code. There are a few ways to do that.
string Display(IEnumerable<HexBin> hexBins)
{
return string.Join(Environment.NewLine, hexBins.Select(h => $"Hex: {h.ValHex} Bin: {h.ValBin}"));
}
That's going to take a collection of HexBin
, create a string from each of them, and join them all together with a return in between so that if you showed it in a console, each one would be on a new line. (I'm guessing at what you'd want it to look like.)
If you knew that you always wanted the string representation of HexBin
to look a certain way you could override ToString()
in that class:
class HexBin
{
public string ValHex { get; set; }
public string ValBin { get; set; }
public override string ToString()
{
return $"Hex {ValHex} Bin {ValBin}";
}
}
Now if you called ToString()
on a HexBin
you'd get your nice-looking string representation.
As an example - I wouldn't actually do this - you could create a class like this:
class HexBins : List<HexBin>
{
public override string ToString()
{
return string.Join(Environment.NewLine, this);
}
}
Now if you call ToString()
on this list it will call ToString()
on all the objects in the list and put them all in one big string.
That's just an example to show how we can override ToString()
. In most real scenarios creating another class just for this would be pointless. We would just write methods to show something however we want. We might override ToString()
for HexBin
, but even then we don't know for sure that we'll want a string version of it to look the same everywhere.
CodePudding user response:
The Where will return an enumerated
Try
var _respuesta = listHexBin.FirstOrDefault(ValHex => ValHex.ValHex == caracter);