Home > database >  How to create a table with values of different types with a Parse method?
How to create a table with values of different types with a Parse method?

Time:02-12

    public class Libro
    {
        public string Titolo { get; set; }
        //string Autore da cambiare in Lista
        public string Autore { get; set; }
        public string Editore { get; set; }
        public int ISBN { get; set; }
        public int Pagine { get; set; }
        public decimal Prezzo { get; set; }
        public int Quantità { get; set; }

    }
}

 public class ReaderR
    {
        public static List<Libro> Read(string path)
        {
            List<Libro> libri = new List<Libro>();
            string line = string.Empty;

            try
            {
                StreamReader sr = new StreamReader(path);

                line = sr.ReadLine();
                while (line != null)
                {

                    string[] content = line.Split('*');
                    libri.Add(new Libro { Titolo = content[0], Autore = content[2], Editore = content[3], ISBN = 4, Pagine = 5, Prezzo = 6, Quantità = 7 });
                    line = sr.ReadLine();
                    }
            }
            catch (Exception)
            {
                //catc(Exception e)throw e;
                libri = new List<Libro>();
            }
            return libri;
        }
    }
}

I took the values from a file.txt and created the model class. In practice I should create a table in which you can see the various Books, Authors, Price etc .., but the problem I think is here:

string[] content = line.Split('*');
libri.Add(new Libro { Titolo = content[0], Autore = content[2], Editore = content[3], ISBN = 4, Pagine = 5, Prezzo = 6, Quantità = 7 });

and I don't know how to fix it..

CodePudding user response:

There are many ways to parse information out of a string.

I would sugest using regex. For example with the following regex you could parse three strings separated by a *:

(?'Title'[^*] )[*](?'Author'[^*] )[*](?'ISBN'[^*] )

The Advantage of using regex is, that you check if the string is valid while the string is parsed. To test a regex I would sugest to use something like https://regex101.com

In your code you can use the C# library:

using System.Text.RegularExpressions;

Regex rx = new Regex(@"(?'Title'[^*] )[*](?'Author'[^*] )[*](?'ISBN'[^*] )",
          RegexOptions.Compiled | RegexOptions.IgnoreCase);

// Define a test string.
string text = "Harry Potter*Rowling*12345";

// Find matches.
MatchCollection matches = rx.Matches(text);

CodePudding user response:

You've switched tactic when you got to ISBN

ISBN = 4, Pagine = 5, Prezzo = 6, Quantità = 7

Assigning the editore name like this makes sense:

Editore = content[3]

It means "take the fourth element of the content array, which is a string array, and put it in the editors property, which is a string"

I guess you'll have tried the pattern of:

ISBN = content[4]

But this won't have worked out because content is full of strings and ISBN is an int and even if a string purely only full of numerical chars that doesn't mean it's a number. This will have given some error like "there is no implicit conversion..."

I guess then you removed the content and just left a hard coded 4 assigned as the ISBN which will work in that it will compile syntactically, but it is incorrect logically. That will just fix every ISBN at being 4, literally

Instead you should parse the string to an int. it's easy, and we can do it with like:

ISBN = int.Parse(content[4])

Similarly for the decimal there is a decimal.Parse

This may expose other problems like, if one of the values contains some non numerical chars (e.g. ISBNs that have hyphens in) but we can solve that later...

  • Related