Appropriate solution on reading a list of Strings from a file
-
Hallo, there is a List which has to be filled with Strings when the program starts up. I am missing the common practice in such cases, if there is any at all. I am thinking of putting all strings names in a text file from where the programme should read and fill up the List. That way, I will always be able to change it, by adding more names, changing or deleting them, avoiding the risk to mess up with the code. Are there any better suggestions on that? Would that be too slow if the List came at thousands of names? Should it be better to have a binary list of text strings, if something like that would be possible (through serialization maybe?) Those are some of the questions coming up, when I am thinking of the appropriate solution to the problem. Thanks.
-
Hallo, there is a List which has to be filled with Strings when the program starts up. I am missing the common practice in such cases, if there is any at all. I am thinking of putting all strings names in a text file from where the programme should read and fill up the List. That way, I will always be able to change it, by adding more names, changing or deleting them, avoiding the risk to mess up with the code. Are there any better suggestions on that? Would that be too slow if the List came at thousands of names? Should it be better to have a binary list of text strings, if something like that would be possible (through serialization maybe?) Those are some of the questions coming up, when I am thinking of the appropriate solution to the problem. Thanks.
I would go with strings in a file, then just
List<string> myList = new List<string>();
myList.AddRange(File.ReadAllLines(path));The memory space shouldn't be a problem, even with thousands of strings, and it won't take long to load anyway. You could hold them as XML data, but then it is a bit harder to edit: you can cause errors in the file. At least with straight text, you don't need any special tools (or intelligence) to change them, so that job can be hived off to the office idiot junior. A quick test said mine read 5000 lines in 7mSec (on the first try, so caching was not involved) - and my PC is nowhere near SOTA! Always go with an option that means you don't have to re-compile!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
Hallo, there is a List which has to be filled with Strings when the program starts up. I am missing the common practice in such cases, if there is any at all. I am thinking of putting all strings names in a text file from where the programme should read and fill up the List. That way, I will always be able to change it, by adding more names, changing or deleting them, avoiding the risk to mess up with the code. Are there any better suggestions on that? Would that be too slow if the List came at thousands of names? Should it be better to have a binary list of text strings, if something like that would be possible (through serialization maybe?) Those are some of the questions coming up, when I am thinking of the appropriate solution to the problem. Thanks.
Griff gave a good answer, but I wonder at the wisdom of using a List rather than a database. Could you give more information on what you are trying to do?
-
I would go with strings in a file, then just
List<string> myList = new List<string>();
myList.AddRange(File.ReadAllLines(path));The memory space shouldn't be a problem, even with thousands of strings, and it won't take long to load anyway. You could hold them as XML data, but then it is a bit harder to edit: you can cause errors in the file. At least with straight text, you don't need any special tools (or intelligence) to change them, so that job can be hived off to the office idiot junior. A quick test said mine read 5000 lines in 7mSec (on the first try, so caching was not involved) - and my PC is nowhere near SOTA! Always go with an option that means you don't have to re-compile!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
Griff gave a good answer, but I wonder at the wisdom of using a List rather than a database. Could you give more information on what you are trying to do?
I want it to be a simple metric conversion programme, such as foot to meter and so on. My purpose is mainly an exercise for myself, but also to be able to share it with friends on a simple download. I cannot ask them to have a database already installed into their system, they wouldn't know how to do that.
modified on Thursday, April 7, 2011 11:26 AM
-
I want it to be a simple metric conversion programme, such as foot to meter and so on. My purpose is mainly an exercise for myself, but also to be able to share it with friends on a simple download. I cannot ask them to have a database already installed into their system, they wouldn't know how to do that.
modified on Thursday, April 7, 2011 11:26 AM
Asnwering your comment to PIEBALDconsult and to me at teh same time: Not all databases need any installation. For example, there is SqlCE which was meant for Windows Mobile devices, but can be used by desktop machines. The code is built into .NET, so if they can run your program, it can use a database. It's pretty full SQL with only a few limitations: single user, no stored procedures, no triggers, but for 90% of simple apps it works like a charm - Outlook stores it's data in modified SqlCE databases. You can serialize it, yes:
// Read from file string path = @"F:\\Temp\\roman.txt"; List myList = new List(); myList.AddRange(File.ReadAllLines(path)); // Write to serialization file. using (Stream stream = File.Open(@"F:\\Temp\\data.bin", FileMode.Create)) { BinaryFormatter bin = new BinaryFormatter(); bin.Serialize(stream, myList); } // Read from serialization file List myList2; using (Stream stream = File.Open(@"F:\\Temp\\data.bin", FileMode.Open)) { BinaryFormatter bin = new BinaryFormatter(); myList2 = (List) bin.Deserialize(stream); }
But this will be slower than reading text! (Simple tests say about twice as slow!) Additionally, you will need some utility or method to modify values.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
Asnwering your comment to PIEBALDconsult and to me at teh same time: Not all databases need any installation. For example, there is SqlCE which was meant for Windows Mobile devices, but can be used by desktop machines. The code is built into .NET, so if they can run your program, it can use a database. It's pretty full SQL with only a few limitations: single user, no stored procedures, no triggers, but for 90% of simple apps it works like a charm - Outlook stores it's data in modified SqlCE databases. You can serialize it, yes:
// Read from file string path = @"F:\\Temp\\roman.txt"; List myList = new List(); myList.AddRange(File.ReadAllLines(path)); // Write to serialization file. using (Stream stream = File.Open(@"F:\\Temp\\data.bin", FileMode.Create)) { BinaryFormatter bin = new BinaryFormatter(); bin.Serialize(stream, myList); } // Read from serialization file List myList2; using (Stream stream = File.Open(@"F:\\Temp\\data.bin", FileMode.Open)) { BinaryFormatter bin = new BinaryFormatter(); myList2 = (List) bin.Deserialize(stream); }
But this will be slower than reading text! (Simple tests say about twice as slow!) Additionally, you will need some utility or method to modify values.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
I want it to be a simple metric conversion programme, such as foot to meter and so on. My purpose is mainly an exercise for myself, but also to be able to share it with friends on a simple download. I cannot ask them to have a database already installed into their system, they wouldn't know how to do that.
modified on Thursday, April 7, 2011 11:26 AM
Oh, one of those -- an excellent exercise. According to the file date, I wrote the following last October (probably in response to a post here):
namespace PIEBALD.Lib.LibExt.Convert
{
public interface IConvert
{
double Convert ( double Value ) ;
}public enum Language { CSharp , VisualBasic } \[System.AttributeUsageAttribute(System.AttributeTargets.Field , AllowMultiple=false , Inherited=false)\] public sealed class ConverterAttribute : System.Attribute { public ConverterAttribute ( string Function , Language Language ) { this.Function = Function ; this.Language = Language ; return ; } public string Function { get ; private set ; } public Language Language { get ; private set ; } } public enum Conversion { \[ConverterAttribute("Value \* 2.54",Language.VisualBasic)\] FromInchToCentimeter , \[ConverterAttribute("Value / 2.54",Language.VisualBasic)\] FromCentimeterToInch , \[ConverterAttribute("( Value - 32.0 ) \* 5.0 / 9.0",Language.CSharp)\] FromFahrenheitToCelsius , \[ConverterAttribute("Value \* 9.0 / 5.0 + 32.0",Language.CSharp)\] FromCelsiusToFahrenheit } public static class LibExt { private static readonly System.Collections.Generic.Dictionary<Conversion,IConvert> conversion ; static LibExt ( ) { conversion = new System.Collections.Generic.Dictionary<Conversion,IConvert>() ; return ; } private static void Add ( Conversion Conversion ) { System.Reflection.FieldInfo fi = typeof(Conversion).GetField ( Conversion.ToString() , System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static ) ; foreach ( ConverterAttribute att in fi.GetCustomAttributes ( typeof(ConverterAttribute) , false ) ) { string code = null ; switch ( att.Language ) { case Language.CSharp : {