Reputation: 25
I'm trying to display a set of characters as one string in an array, but every time I run the program it either displays a number or zero. https://i.sstatic.net/wByxd.jpg The set of values and numbers on the right is a class which I use to get information from multiple forms, and the set of codes and values on the left is for the Result form. https://i.sstatic.net/o3UkO.jpg The characters come from multiple forms all stored into the chr(~) value.
If you require any more information, please feel free to ask.
public static string result = (chrExt + chrAgr + chrCon + chrNeu + chrOpen).ToString();
public static string[] names = new string[20];
public static string[] results = new string[20];
and
string name = Data.name;
string result = Data.result;
private void label2_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
Main frm = new Main();
this.Hide();
frm.Show();
}
private void btnDisplay_Click(object sender, EventArgs e)
{
Data.names[Data.i] = name;
Data.results[Data.i] = result;
Data.i = Data.i + 1;
lstNames.Items.Clear();
lstResults.Items.Clear();
for (int i = 0; i < Data.index; i++)
{
lstNames.Items.Add(Data.names[i]);
lstResults.Items.Add(Data.results[i]);
}
}
EDIT
I changed the code
public static int chrExt, chrAgr, chrCon, chrNeu, chrOpen;
to
public static char chrExt, chrAgr, chrCon, chrNeu, chrOpen;
and then I changed
public static string result = (chrExt + chrAgr + chrCon + chrNeu + chrOpen).ToString();
to
public static string result
{
get { return string.Concat(chrExt, chrAgr, chrCon, chrNeu, chrOpen); }
}
Thank you all!
Upvotes: 0
Views: 111
Reputation: 186668
Character is a number (Int16
to be exact), so adding up numbers you have a number. Use String.Concat
then:
String test1 = ('a' + 'b').ToString(); // 195 == 97 + 98
String test2 = String.Concat('a', 'b'); // "ab" == "a" + "b"
In your case
String result = String.Concat(chrExt, chrAgr, chrCon, chrNeu, chrOpen);
Edit: As I can see form the screenshot you declare
public static int chrExt, chrAgr, chrCon, chrNeu, chrOpen;
a better choice is declare char
as char
, not int
:
public static char chrExt, chrAgr, chrCon, chrNeu, chrOpen;
If you insist on int
, then convert to char
within Concat
:
String result = String.Concat(
(char)chrExt, (char)chrAgr, (char)chrCon, (char)chrNeu, (char)chrOpen);
Upvotes: 1
Reputation: 43876
Your chr*
variables are of type int
. So this line
public static string result = (chrExt + chrAgr + chrCon + chrNeu + chrOpen).ToString();
first sums up the integers and converts the result to a string
. You can change this to
public static string result = string.Empty + chrExt + chrAgr + chrCon + chrNeu + chrOpen;
or use string.Concat
:
public static string result = string.Concat(chrExt, chrAgr, chrCon, chrNeu, chrOpen);
But this of course does not yet do what you want. You declare a static
member and initialize it only once. At this time, all your chr*
variables are still 0
.
From your code, I can't see when you set those variables to the desired values. But your result
has to be computed afterwards.
One way is to declare it as a property:
public static string Result
{
get { return string.Concat(chrExt, chrAgr, chrCon, chrNeu, chrOpen); }
}
or with C# 6 as an expression bodied member:
public static string Result => string.Concat(chrExt, chrAgr, chrCon, chrNeu, chrOpen);
Upvotes: 1