Elements will not add to C# list [closed] - 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 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);
}
}
}

Related

C# SQL "Index was outside the bounds of the array." [closed]

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.

Simple name generator in C# [closed]

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.

C# Argument 1: cannot convert from 'void' to 'bool' [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I need to print num after num(num-1) until zero
have to do it in recursion style.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("what is num?");
int num = int.Parse(Console.ReadLine());
downnums(num);
}
public static void downnums(int num)
{
if (num == 0)
Console.WriteLine("that all");
else
{
Console.WriteLine(downnums(num-1));
}
}
}
Thanks guys
Remember: void means your method doesn't return a value, so it cannot be assigned to other variable or passed to method as an argument like WriteLine method:
this is what you're looking for:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("what is num?");
int num = int.Parse(Console.ReadLine());
downnums(num);
}
public static void downnums(int num)
{
if (num == 0)
Console.WriteLine("that all");
else
{
Console.WriteLine(num);
downnums(num-1);
}
}
}

Example Bruteforce Algorithm [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am currently getting into Pentesting and Ethical Hacking to test website security.
I would appreciate an example Bruteforce algorithm that is stored in a string. Not a dictionary algorithm, but a bruteforce algorithm. For example, it tries the letter a. Then it tries the letter b, then it tries c and so on. Thank you in advance :)
Even if i think that you should really come up with an own concept for this problem, i'll like to give you a hint how to do this.
Disclaimer: this is the laziest, slowest and dirtiest approach possible but it gets its job done. NEVER EVER USE THIS FOR A REAL SYSTEM.
Programm.cs
class Program
{
static void Main(string[] args)
{
Brutforce b = new Brutforce()
{
Alphabet = new []{'a', 'b', 'c', 'd'}
};
ICollection<string> vals = b.Calculate(3);
foreach (var elem in vals)
Console.WriteLine(elem);
Console.ReadKey();
}
}
Brutforce.cs
internal class Brutforce
{
public ICollection<char> Alphabet { get; set; }
private ICollection<string> _calculate(int lenght)
{
if (lenght <= 1) return Alphabet.Select(a => a + "").ToList();
ICollection<string> sub = _calculate(lenght - 1);
return (from alpha in Alphabet from prior in sub select alpha + prior).ToList();
}
public ICollection<string> Calculate(int lenght)
{
return Alphabet == null ? null : _calculate(lenght);
}
}

Code unreachable when comparing types [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Question: My else-statement is unreachable, what am i doing wrong?
very VERY new at programming and i'm trying to compare the type so for example people can't enter strings when i'm asking for integers.
My code is probably pretty bad, if i could get a header what to do and why the if-argument skips the else-part i'd be really happy!
Thanks!
class Program
{
static void Main(string[] args)
{
int integer = 0;
start:
Console.WriteLine("How old are you?: ");
int svar = int.Parse(Console.ReadLine());
Utility.CompareTypes(svar, integer);
if (true)
{
Console.WriteLine("Thanks");
}
else
{
Console.WriteLine("You have to enter a number!");
goto start;
}
}
}
class Utility
{
public static bool CompareTypes<T01, T02>(T01 type01, T02 type02)
{
return typeof(T01).Equals (typeof(T02));
}
}
:c
It's not really a question of code, but of logic...
if (true) // <--- this will ALWAYS be true
{
Console.WriteLine("Thanks");
}
else // <--- therefore this will NEVER happen
{
Console.WriteLine("You have to enter a number!");
goto start;
}
Since your else block can never possibly execute under any logical circumstance, the entire block of code can be simplified to:
Console.WriteLine("Thanks");
In order for the else block to execute, the condition checked in the if statement needs to be false. You're currently not checking any actual condition, just a hard-coded true value.
Perhaps you meant to use the result of the previous line of code? Something like this:
var typesAreSame = Utility.CompareTypes(svar, integer);
if (typesAreSame)
{
//...

Categories