Aldas Aldas - 2 months ago 6
C# Question

c# Returning array values from methods

so this probably is going to get a lot of hate, because this is a really newbie mistake, but I have an assignment where i have a list of books in a library with their ISBN numbers, authors and etc. I'm having trouble returning values of each book with their info. I need the program to create two csv files with the info i put in AND write the text I've written in my Console.WriteLine. You can expect many errors here since I've just started programming. Here's the code:

using System;
using System.IO;

namespace ConsoleApplication2
{
class Program
{

static void Main(string[] args)
{


foreach (string line in File.ReadAllLines(@"Duomenys.txt"))
{
string[] a = line.Split(',');
long ISBN = Convert.ToInt64(a[0]);
string BookName = a[1];
string Author = a[2];
string Genre = a[3];
string Publisher = a[4];
int PublishYear = Convert.ToInt32(a[5]);
int PageNumber = Convert.ToInt32(a[6]);
Console.WriteLine(PublishYear);
Console.WriteLine();

}
}

public void BookWithTheMostPages(int[] a)
{
int maxPages = 0;
string[] lines = File.ReadAllText(@"Duomenys.txt").Split('\n');
foreach (string line in lines)
{
{
Console.ReadLine();
if (a[6] > maxPages)
{
maxPages = a[6];

Console.WriteLine("Storiausios knygos pavadinimas: {0} , jos autorius(-ė): {1}", a[1], a[2]);
}
}
}
}

public void Publish(string[] a)
{
if (!File.Exists(@"Technologija.csv"))
File.Create(@"Technologija.csv").Dispose();
using (StreamWriter streamwrite = new StreamWriter(File.OpenWrite(@"Technologija.csv")))
{
if (a[2] == "Technologija")
{
streamwrite.WriteLine("\n ISBN : {0}, Pavadinimas: {1}, Autorius: {2}, Tipas: {3}, Leidykla: {4}, Išleidimo Metai: {5}, Puslapių skaičius: {6}", a[0], a[1], a[2], a[3], a[4], a[5], a[6]);
}
}
}

public void Output(string[] a)
{
if (!File.Exists(@"Autoriai.csv"))
File.Create(@"Autoriai.csv").Dispose();

using (StreamWriter streamWriter = new StreamWriter(File.OpenWrite(@"Autoriai.csv")))
{
streamWriter.WriteLine("\n{0}", a[2]);
}
}
public void Publishyear(string[] a)
{
if (a[5] == "2014")
{
for (int j = 1; j <= 5; j++)
Console.WriteLine("\nKnygos ISBN: {0}, Pavadinimas {1}, Autorius {2}", a[0], a[1], a[2]);
}
}
}
}

Answer

First of all welcome to programming! One good thing about c# is that its object oriented, you should take advantage of this! Create a book class to hold your data and manipulating it will be much easier. Also, if you're trying to return data the void keyword means that the function you write has no return value so you will not get anything back. Here's an example to get you started.

class Program
{
    static void Main(string[] args)
    {
        List<Book> books = new List<Book>;
        foreach (string line in File.ReadAllLines(@"Duomenys.txt"))
        {
            Book newBook = new Book();
            string[] a = line.Split(',');
            newBook.ISBN = a[0];
            newBook.bookName = a[1];
            newBook.author = a[2];
            newBook.genre = a[3];
            newBook.publisher = a[4];
            newBook.yearPublished = Convert.ToDateTime(a[5]);
            newBook.numberOfPages = Convert.ToInt32(a[6]);

            books.Add(newBook);
        }

        foreach (Book book in books)
        {
           // do stuff here!
        }
}

class Book
{
    public string ISBN { get; set; }
    public string bookName { get; set; }
    public string author { get; set; }
    public string genre { get; set; }
    public string publisher { get; set; }
    public DateTime yearPublished { get; set; }
    public int numberOfPages { get; set; }        
}