Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I need some help with completing this simple name "generator"
We've just started programming in high school, so I would like a simpler solution. This is all I've got for now, but the output in the textbox is System.String[]
private void Btn_gen_Click(object sender, EventArgs e)
{
textbox.Text = textbox.Text + name((int) count.Value) + Environment.NewLine;
}
private string name(int count)
{
string heslo = "";
string[] names = new string[] {"Abby", "Uther", "Thomas", "Michelle", "Abraham", "Bendy"};
string[] surnames = {"The Mighty", "The Clumsy", "The Strong", "The Lightbringer", "The Pyromancer", "The Necromancer"};
Random gnč = new Random();
for (int i = 1; i <= count; i++)
{
int nč = gnč.Next(0, names.Length);
heslo = heslo + names;
}
return heslo;
}
The problem is that you aren't appending a random name, but rather the type name System.String[], which is the formal type name of your names variable, to your string.
Change heslo = heslo + names to
heslo = heslo + names[nč];
This will take a random element out of the names array.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
I just finished a C# totorial and thought it would be a cool idea to get the most popular type of computers using lists. I was able to get it to work exept the only the most recent element will be added to the list.
namespace computerDatabase
{
class program
{
static void Main()
{
while (true)
{
List<string> computerName = new List<string>();
Console.Write("Who is your computer manufacturer: ");
string cName = Console.ReadLine();
computerName.Add(cName);
if (cName == "list")
{
foreach(string s in computerName)
{
Console.WriteLine(s);
}
}
else
{
computerName.Add(cName.ToLower());
}
}
}
}
}
As pointed out by Jon, you re-initializing your list for every iteration, nullifying any input added to the list. You also do not need to specify a capacity for the list in the constructor as it will grow automatically. I've corrected the relevant parts for you:
List<string> computerName = new List<string>();
while (true)
{
Console.Write("Who is your computer manufacturer: ");
string cName = Console.ReadLine();
if (cName != "list")
computerName.Add(cName.ToLower());
else
{
foreach(string s in computerName)
{
Console.WriteLine(s);
}
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I've been learning how to create a SQL base with C# and I'm stuck here with a question.
Here I am testing how to return the value of a text row to a string
Why does the compiler make an error here? I read the method list and my database has 3 more values for the 'FirstName' column. Why can I only see an index?
I appreciate your help in advance
{
static void Main(string[] args)
{
string dbName = "URI=file:testeDB.db";
using (var connection = new SqliteConnection(dbName))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = "SELECT FirstName FROM Persons";
SqliteDataReader reader = command.ExecuteReader();
Console.WriteLine(reader);
if ( reader.Read() )
{
Console.WriteLine("Reading...");
Thread.Sleep(1000);
string myVar = reader.GetString(2); //Index was outside the bounds of the array."
Console.WriteLine(myVar);
}
else
{
Console.WriteLine("Dont Read");
}
}
}
}
}
This happens because the method GetString(int) as the documentation says "Gets the value of the specified column as a String." and according to the code you showed us you are just retrieving one column from the table Persons, that's why you are getting the exception, only GetString(0) will work in this case.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Hi I am working on a simple code wherein I am trying to push integer into a integer array from a string.
Below is my code:
/*
Input:3[a2[b]] Output: abbabbabb
*/
Stack<int> intChar = new Stack<int>();
Stack<string> strChar = new Stack<string>();
string output = "";
Console.WriteLine("Enter the string of chars");
string input= Console.ReadLine();
for (int i = 0; i < input.Length; i++)
{
char c = input[i];
if(Char.IsDigit(c))
{
intChar.Push(input[i]);
}
that if condition is taking up wrong values and I think it is taking up the ascii value of the character and pushing in the ascii value of the same. below us the screenshot of the debug window.
I want to check the entire string and push the integer and char values to their respective Stacks. Please suggest me where I am doing it wrong.
Apparently type char is converted to ascii code. You have to use string to get the real number. Try this
if (Char.IsDigit(c)) intChar.Push( Convert.ToInt32(c.ToString()) );
another way is to use GetNumericValue function. Since it returns double, it needs to be cast to int.
if (Char.IsDigit(c)) intChar.Push( (int)Char.GetNumericValue(c) );
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
private void button1_Click(object sender, EventArgs e)
{
Random random = new Random();
int getal1 = random.Next(0, 100);
Random random2 = new Random();
int getal2 = random2.Next(0, 100);
int Antwoord = getal1 + getal2;
if (Antwoord == textBox1.Text);
...
}
it says
Operator '==' cannot be applied to operands of type 'string' and 'int'
can someone help me?
if (Antwoord.ToString() == textBox1.Text);
Write it like this. You want to check int to string, this can't happen. You should convert the int value to string or the string value to int. I advice you to convert int to string in other case you can have an exception.
If you need an integer value, entered in the TextBox, you should try to parse its Text:
int textBox1Value;
if (int.TryParse(textBox1.Text, out textBox1Value))
{
// Here the text was successfully parsed to the textBox1Value variable
if (Antwoord == textBox1Value)
{
... // do your stuff
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Cannot convert lambda expression to type bool because it is not a delegate type.
protected void button1_Click(object sender, EventArgs e)
{
int totalpoints;
Int32 realpoints;
lelpoints = 0;
totalpoints = Convert.ToInt32(dPts.Text);
totalpoints = totalpoints + 1;
totalpoints = realpoints;
dPts.Text = totalpoints.ToString();
}
protected void buyBG_Click(object sender, EventArgs e)
{
string[] rbg = new string[] { "red", "green", "blue" };
Random random = new Random();
int randomNumber = random.Next(0, 3);
string currentbg = rbg[randomNumber];
if (realpoints => 10 ){}
This is the issue I'm experiencing with the code I've provided above. The issue is showed at the If statement at the bottom.
Edit: Changing => to >= definitely resolved that issue but now it reports an error "the name realpoints does not exist in the current context". Thank you
Not =>, but >=.
This should resolve your problem.
=> is a lambda operator is not the same as the greater than or equal to operator >=
you can read => (lambda sign) as "goes to". Means you are passing a parameter to a delegate body. In your case realpoints => 10 mean realpoints is parameter and 10 is a body and this body is not bool type (10 is not bool).
For example you have a list of integer like:
List intList = new List {10, 14,5,17};
you want to find the first integer in the list which is greater than 10 then you can use lambda expression something like
int numberFound = intList.Find(x => x > 10 );
Thanks,
Sukh