Check List elements in C# and Python - c#

I have worked on Python and I am new in C#. I am trying to get familier with syntaxes and concepts. I have a problem with list.
For example, I will take a word from user. I will add it to a list and I will check if it is entered before because user can't give the same word second time. If it is not entered before add it to the list. If is is already in the list pass it.
In python:
check_list = list()
if word not in check_list:
check_list.append(word)
else:
pass
How can I do that in C# ???

List<string> list;
if(!list.Contains(word)){
list.Add(word)
}
I was trying to follow your example as closely as possible so I didn't add confusion, however if you new-up the list every time !list.Contains(word) will always be true.

Related

Why can't this read integers from my text file? Sytem.FormatException

Okay, so I am making a game that reads data from a text file to create events. What happens is as each year advances if something exciting happens a popup box with three options appears, clicking them affects the game and so on.
I have created a function which can take various arguments and make the form automatically - so far so good- but writing large event descriptions in the code is messy and disorganized. Instead I decided to create another function which takes values from a text file, organizes them and then calls the second function to create the 'event'.
Now, as I said each event comes with three choices (See below) and each choice has a consequence on one of three factors (approval, prestige, power), I haven't quite worked out the mechanics properly but all in good time, everything runs wonderfully until I need to load this integers from the text file.
It keeps having trouble converting the string to an integer, why is this and how can I fix it?
Line 11 of the text file: 10 (yes I checked and it is the right line to read)
The code:
List<int> affecta = new List<int>();
affecta.Add(Int32.Parse(System.IO.File.ReadLines(filename).Take(11).First()))
I can load the other things such as the picture file's location perfectly, so 'filename' points to the correct .txt
It might be something really obvious to someone with more experience, but I just can't see why.
I don't think Take does what you think - It grabs the first 11 items and returns all of them, so you get an IEnumerable of 11 items. When you do First on that, you get item at position 0, not position 10. I think you want Skip and then First, which will skip the first 10 items and return the next (11th) item:
affecta.Add(Int31.Parse(System.IO.File.ReadLines(filename).Skip(10).First()))
If you use Take(11) this means "get 11 rows from the source". After that you have First(), so you'll get first of them. You're essentially trying to convert the first line into integer.
I assume you want to use Skip(10) since you want the 11th line.
Take(11).First() returns the First element from the IEnumerable containing the 11 elements.
Instead, Skip the first 10 and select the First from the IEnumerable.
affecta.Add(Int32.Parse(System.IO.File.ReadLines(filename).Skip(10).First()))
Alternatively, Take the first 11 and select the Last from the IEnumerable.
affecta.Add(Int32.Parse(System.IO.File.ReadLines(filename).Take(11).Last()))

How can I implement a binary search over a list of file names?

NOTE: Assignment help, No code required.
I have a list of file names in a list-box. As part of the assignment I want to search the file names using a binary search implementation.
Can someone help me understand how to implement a binary search without using the built-in List<T>.BinarySearch(...) method?
You have to begin with a sorted list of values. Then you just search like you would if you were playing a number guessing game (and were a computer). Pick the middle element of your list. If the number you're searching for isn't equal to the value of the middle element, do the same thing again, but this time on a sub-list that's half the size (since the list is sorted, you know what side of the list your target is on). Just keep doing that until you found the value you're looking for.

How to add a text item to a combobox in C#

I am new to C# from Delphi. In Delphi, TCombobox.Add takes a single, string parameter. Thus is it very simple to add a string item to a combobox.
C# confused me because Combobox.Items.Add takes a single, object parameter. There are some circumstances in which I cannot add a string var to the list of items. (I haven't identified a pattern yet; it might be if it's a property).
Googling was fruitless until...
I had the same problem in an WPF application, using VB.Net I got Component.Items.Add("String") to work, my guess is the string will automatically be cast to an suitable object, try to explicitly cast string in your case(not sure to what).
You can also try to assign your string to be the caption of a label and add that label to the control.
I have asked several times for this question to be deleted because it is of no value to anyone. I wrote it when I was confused and bewildered and didn't know what I was doing.
... I found http://www.codeproject.com/Questions/416859/Csharp-Filling-ComboBox-with-an-SQL-table-column, which said
comboBox1.Items.Add((string) problematicVar); //problematicVar is a string
So I have to cast to string.
I've added this self-answered question because I wish it had existed half an hour.

C# Console - Inputting and removing from an array

for part of a small assignment I have, i've been asked to create an array to store names and addresses taken from input that the user gives and to be able to later delete a name and address from the array.
Any help or links to helping me understand how to achieve this would be highly appreaciated, thanks.
EDIT - The array is to be set up like an address book, and when printed to the screen it displays like so: "Bloggs, Joe"
It must be surname then forename. I know how to acquire and store the information the user will give, being their names and addresses, but I am stuck on how to add this into an array. The array doesn't have to be infinite, as I am supposed to allocate the array whatever size I wish.
At the start of the program it will be part of, the user will be given a menu, and they can choose to add a record, delete a record or print the book to the screen. So i am meant to be using methods where suitable.
Well, to start with, an array is the wrong data structure to use here.
Arrays are always a fixed size - whereas you want to be able to add elements and later remove them. Assuming you're using C# 2 or higher, you should probably use a List<T>.
Now, the next thing is to work out what T should be. It sounds like you want to store details of people - so you should create a Person class (or perhaps Contact) to encapsulate the name and address... that way you can have a List<Person>.
The next task is probably to work out how to ask the user for input and convert that input into an instance of Person.
Basically, break the task up into small bits - and then feel free to ask questions about any specific bits which you find hard.
I seem to remember this exact same assignment from my CS classes.
The prof wanted us to use linked lists. As John Skeet points out above, .NET has List<T>, which is basically a linked list (with the added feature of being able to be reference each item by index like an array)
You can use a Serializer for the saving part.
Check out the BinaryFormatter class and XmlSerializer.
XmlSerializer is preferred because the file is human-readable and efficiency is usually less important considering the type and purpose of your app.
Using XmlSerializer is as simple as:
var filename = "c:\....\addressbook.xml";
if (File.Exists(filename))
File.Delete(filename);
using (var sw = new StreamWriter(filename))
{
var xs = new XmlSerializer(typeof(List<Person>));
xs.Serialize(sw, myAddressBook);
}

Fast autocomplete from in-memory collection (.NET)

I have this text input field on a web page. User types in item names for purchase. I'd like to provide a dropdown with possible names, based on letters typed so far.
Question is how to implement the search on the server (ASP.NET MVC). I'll probably load the whole collection of item names (there are over 100 000) in a static variable on app start. How should I implement efficient search for names starting with given one or more characters?
TIA
You can sort the collection by name, then write a modified binary search that returns a range of items.
However, I would recommend first trying a simple sequential search and seeing how it behaves under load.
I'll probably load the whole
collection of item names (there are
over 100 000) in a static variable on
app start. How should I implement
efficient search for names starting
with given one or more characters?
By NOT (!) loading them into a static variable. Hit the db server on every request with a "top 101" clause. Finished.

Categories