Reading from a text file, and splitting each individual line into different arrays or lists
I'm trying to figure out the best way to solve a problem in my program. Basically, I've got a listbox that holds book codes(1,2,3, etc). 1, 2, 3 would represent 3 different books. I have a text file that holds information such as:
Text file format:
bookCode, bookTitle, bookPrice, quantityofBooks
Text file dummy information:
1, Book One, 22, 5
2, Book Two, 10, 3
3, Book Three, 5, 15
I don't always know how many lines there will be in this text file. What I'm trying to do is to split each individual line and put each line into some sort of array or list, and have the ability to access each individual item in those individual lists (items such as: Book One, Book Two, so that I can put these into a listbox) And, say for example someone clicks on "Book Two" in the list box, my label will change with the price for Book Two.
How can I put the bookTitle (Book One, Book Two, etc) into a listbox, and how can I split each line into separate lists and each individual items for that line into a list?
I'm not quite sure I've explained what I want as well as it could be explained. So I apologise if this has come across mind boggling.
c# design-patterns c#-4.0
add a comment |
I'm trying to figure out the best way to solve a problem in my program. Basically, I've got a listbox that holds book codes(1,2,3, etc). 1, 2, 3 would represent 3 different books. I have a text file that holds information such as:
Text file format:
bookCode, bookTitle, bookPrice, quantityofBooks
Text file dummy information:
1, Book One, 22, 5
2, Book Two, 10, 3
3, Book Three, 5, 15
I don't always know how many lines there will be in this text file. What I'm trying to do is to split each individual line and put each line into some sort of array or list, and have the ability to access each individual item in those individual lists (items such as: Book One, Book Two, so that I can put these into a listbox) And, say for example someone clicks on "Book Two" in the list box, my label will change with the price for Book Two.
How can I put the bookTitle (Book One, Book Two, etc) into a listbox, and how can I split each line into separate lists and each individual items for that line into a list?
I'm not quite sure I've explained what I want as well as it could be explained. So I apologise if this has come across mind boggling.
c# design-patterns c#-4.0
Split should do what you're looking for. string partArray = inputLine.Split(',');
– That Chuck Guy
Mar 19 '12 at 1:08
I want to split the lines, then I want to store those lines into a list/array, and once I've got the lines stored I want to split again by ','. Is it possible to do this?
– user676853
Mar 19 '12 at 1:12
add a comment |
I'm trying to figure out the best way to solve a problem in my program. Basically, I've got a listbox that holds book codes(1,2,3, etc). 1, 2, 3 would represent 3 different books. I have a text file that holds information such as:
Text file format:
bookCode, bookTitle, bookPrice, quantityofBooks
Text file dummy information:
1, Book One, 22, 5
2, Book Two, 10, 3
3, Book Three, 5, 15
I don't always know how many lines there will be in this text file. What I'm trying to do is to split each individual line and put each line into some sort of array or list, and have the ability to access each individual item in those individual lists (items such as: Book One, Book Two, so that I can put these into a listbox) And, say for example someone clicks on "Book Two" in the list box, my label will change with the price for Book Two.
How can I put the bookTitle (Book One, Book Two, etc) into a listbox, and how can I split each line into separate lists and each individual items for that line into a list?
I'm not quite sure I've explained what I want as well as it could be explained. So I apologise if this has come across mind boggling.
c# design-patterns c#-4.0
I'm trying to figure out the best way to solve a problem in my program. Basically, I've got a listbox that holds book codes(1,2,3, etc). 1, 2, 3 would represent 3 different books. I have a text file that holds information such as:
Text file format:
bookCode, bookTitle, bookPrice, quantityofBooks
Text file dummy information:
1, Book One, 22, 5
2, Book Two, 10, 3
3, Book Three, 5, 15
I don't always know how many lines there will be in this text file. What I'm trying to do is to split each individual line and put each line into some sort of array or list, and have the ability to access each individual item in those individual lists (items such as: Book One, Book Two, so that I can put these into a listbox) And, say for example someone clicks on "Book Two" in the list box, my label will change with the price for Book Two.
How can I put the bookTitle (Book One, Book Two, etc) into a listbox, and how can I split each line into separate lists and each individual items for that line into a list?
I'm not quite sure I've explained what I want as well as it could be explained. So I apologise if this has come across mind boggling.
c# design-patterns c#-4.0
c# design-patterns c#-4.0
asked Mar 19 '12 at 1:03
user676853
Split should do what you're looking for. string partArray = inputLine.Split(',');
– That Chuck Guy
Mar 19 '12 at 1:08
I want to split the lines, then I want to store those lines into a list/array, and once I've got the lines stored I want to split again by ','. Is it possible to do this?
– user676853
Mar 19 '12 at 1:12
add a comment |
Split should do what you're looking for. string partArray = inputLine.Split(',');
– That Chuck Guy
Mar 19 '12 at 1:08
I want to split the lines, then I want to store those lines into a list/array, and once I've got the lines stored I want to split again by ','. Is it possible to do this?
– user676853
Mar 19 '12 at 1:12
Split should do what you're looking for. string partArray = inputLine.Split(',');
– That Chuck Guy
Mar 19 '12 at 1:08
Split should do what you're looking for. string partArray = inputLine.Split(',');
– That Chuck Guy
Mar 19 '12 at 1:08
I want to split the lines, then I want to store those lines into a list/array, and once I've got the lines stored I want to split again by ','. Is it possible to do this?
– user676853
Mar 19 '12 at 1:12
I want to split the lines, then I want to store those lines into a list/array, and once I've got the lines stored I want to split again by ','. Is it possible to do this?
– user676853
Mar 19 '12 at 1:12
add a comment |
3 Answers
3
active
oldest
votes
declare this class
public class Book
{
string BookName;
int bookCode;
decimal bookPrice;
int bookQuanity
}
Then read from the file using this code:
List<Book> Books = new List<Book>();
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
string str;
string strArray;
str = sr.ReadLine();
strArray = str.split(',');
Book currentBook = new Book();
currentBook.bookCode = strArray[0];
currentBook.BookName = strArray[1];
currentBook.bookPrice = double.parse(strArray[2]);
currentBook .bookCode = int.parse(strArray[3]);
Books.add(currentBook);
}
}
//listbox = the name you gave your listbox
listbox.DataSource = Books;
listbox.ValueMember = "bookCode";
listbox.DisplayMember = "BookName";
What's the best way to implement the read code? I'm using an open button to activate this storing process. Code: private void openToolStripMenuItem_Click(object sender, EventArgs e) - but all I'm getting is: an object reference is required for the non-static field, method
– user676853
Mar 19 '12 at 1:50
Ah Yes I messed up it should be currentBook.bookCode ect .... not Book.bookCode edit made to answer
– Micah Armantrout
Mar 19 '12 at 1:57
Ahh okay, now those errors are gone, I'm getting: Book is a 'type' But is used like a 'variable' - this is line: "Books.add(Book);"
– user676853
Mar 19 '12 at 2:01
ok fixed that too
– Micah Armantrout
Mar 19 '12 at 2:09
Okay, that got rid of the errors. How would I now go about putting the bookcodes into a listbox?
– user676853
Mar 19 '12 at 2:42
|
show 3 more comments
I would create class for the Book first
public class Book
{
public int BookCode { set;get;}
public string BookTitle { set;get; }
public decimal BookPrice { set;get}
public int Quantity { set;get;"
}
Read from your datasource( in this case the text file) and store it in a List of our Book Class
List<Book> bookList=new List<Book>();
bookList=BookManager.GetBookList(); // gets the list of books read from the text file.
Store this in your global scope so that you can access it from any of your methods in the form.
When you want to access specific data about a book use Linq to get it
var book=(from p in bookList where p.BookCode=2).FirstOrDefault();
if(book !=null)
{
lblPrice.Text=book.BookPrice.ToString();
}
To put the BookTitles to a List box, you can mention the DataTextField as BookTitle when you bind that.
lstBooks.DataSource=bookList;
lstBooks.DataValueField="BookCode";
lstBooks.DataTextField="BookTitle ";
lstBooks.DataBind();
To Read the file from your text file and fill it to your list of Book object, do some thing like this
public List<Book> GetBookList()
{
List<Book> objBooks=new List<Book>();
using (StreamReader file = new StreamReader(@"C:dataist.txt"))
{
while ((line = file.ReadLine()) != null)
{
char delimiters = new char { ',' };
string parts = line.Split(delimiters);
Book objBook=new Book();
objBook.BookCode=parts[0];
objBook.BookTitle =parts[0];
objBook.BookPrice =Convert.ToDecimal(parts[0]);
objBook.Quantity =Convert.ToInt32(parts[0]);
objBooks.Add(objBook);
}
file.Close();
}
return objBooks;
}
If possible, I will store the book details to a database table. It is more easy to manage and maintain and i dont need to worry about reading and writing to file.
I am assuming you are doing this for a windows form app.
add a comment |
Book objBook = new Book();
objBook.BookCode=Convert.ToInt32(parts[0]);
objBook.BookTitle =parts[0];
objBook.BookPrice =Convert.ToDecimal(parts[0]);
objBook.Quantity =Convert.ToInt32(parts[0]);
Your array indexing example is always pinned at [0]...
– Aaron Hudon
Nov 14 '18 at 22:10
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9763796%2freading-from-a-text-file-and-splitting-each-individual-line-into-different-arra%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
declare this class
public class Book
{
string BookName;
int bookCode;
decimal bookPrice;
int bookQuanity
}
Then read from the file using this code:
List<Book> Books = new List<Book>();
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
string str;
string strArray;
str = sr.ReadLine();
strArray = str.split(',');
Book currentBook = new Book();
currentBook.bookCode = strArray[0];
currentBook.BookName = strArray[1];
currentBook.bookPrice = double.parse(strArray[2]);
currentBook .bookCode = int.parse(strArray[3]);
Books.add(currentBook);
}
}
//listbox = the name you gave your listbox
listbox.DataSource = Books;
listbox.ValueMember = "bookCode";
listbox.DisplayMember = "BookName";
What's the best way to implement the read code? I'm using an open button to activate this storing process. Code: private void openToolStripMenuItem_Click(object sender, EventArgs e) - but all I'm getting is: an object reference is required for the non-static field, method
– user676853
Mar 19 '12 at 1:50
Ah Yes I messed up it should be currentBook.bookCode ect .... not Book.bookCode edit made to answer
– Micah Armantrout
Mar 19 '12 at 1:57
Ahh okay, now those errors are gone, I'm getting: Book is a 'type' But is used like a 'variable' - this is line: "Books.add(Book);"
– user676853
Mar 19 '12 at 2:01
ok fixed that too
– Micah Armantrout
Mar 19 '12 at 2:09
Okay, that got rid of the errors. How would I now go about putting the bookcodes into a listbox?
– user676853
Mar 19 '12 at 2:42
|
show 3 more comments
declare this class
public class Book
{
string BookName;
int bookCode;
decimal bookPrice;
int bookQuanity
}
Then read from the file using this code:
List<Book> Books = new List<Book>();
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
string str;
string strArray;
str = sr.ReadLine();
strArray = str.split(',');
Book currentBook = new Book();
currentBook.bookCode = strArray[0];
currentBook.BookName = strArray[1];
currentBook.bookPrice = double.parse(strArray[2]);
currentBook .bookCode = int.parse(strArray[3]);
Books.add(currentBook);
}
}
//listbox = the name you gave your listbox
listbox.DataSource = Books;
listbox.ValueMember = "bookCode";
listbox.DisplayMember = "BookName";
What's the best way to implement the read code? I'm using an open button to activate this storing process. Code: private void openToolStripMenuItem_Click(object sender, EventArgs e) - but all I'm getting is: an object reference is required for the non-static field, method
– user676853
Mar 19 '12 at 1:50
Ah Yes I messed up it should be currentBook.bookCode ect .... not Book.bookCode edit made to answer
– Micah Armantrout
Mar 19 '12 at 1:57
Ahh okay, now those errors are gone, I'm getting: Book is a 'type' But is used like a 'variable' - this is line: "Books.add(Book);"
– user676853
Mar 19 '12 at 2:01
ok fixed that too
– Micah Armantrout
Mar 19 '12 at 2:09
Okay, that got rid of the errors. How would I now go about putting the bookcodes into a listbox?
– user676853
Mar 19 '12 at 2:42
|
show 3 more comments
declare this class
public class Book
{
string BookName;
int bookCode;
decimal bookPrice;
int bookQuanity
}
Then read from the file using this code:
List<Book> Books = new List<Book>();
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
string str;
string strArray;
str = sr.ReadLine();
strArray = str.split(',');
Book currentBook = new Book();
currentBook.bookCode = strArray[0];
currentBook.BookName = strArray[1];
currentBook.bookPrice = double.parse(strArray[2]);
currentBook .bookCode = int.parse(strArray[3]);
Books.add(currentBook);
}
}
//listbox = the name you gave your listbox
listbox.DataSource = Books;
listbox.ValueMember = "bookCode";
listbox.DisplayMember = "BookName";
declare this class
public class Book
{
string BookName;
int bookCode;
decimal bookPrice;
int bookQuanity
}
Then read from the file using this code:
List<Book> Books = new List<Book>();
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
string str;
string strArray;
str = sr.ReadLine();
strArray = str.split(',');
Book currentBook = new Book();
currentBook.bookCode = strArray[0];
currentBook.BookName = strArray[1];
currentBook.bookPrice = double.parse(strArray[2]);
currentBook .bookCode = int.parse(strArray[3]);
Books.add(currentBook);
}
}
//listbox = the name you gave your listbox
listbox.DataSource = Books;
listbox.ValueMember = "bookCode";
listbox.DisplayMember = "BookName";
edited Oct 27 '16 at 2:28
miiworld2
1019
1019
answered Mar 19 '12 at 1:18
Micah ArmantroutMicah Armantrout
4,88632552
4,88632552
What's the best way to implement the read code? I'm using an open button to activate this storing process. Code: private void openToolStripMenuItem_Click(object sender, EventArgs e) - but all I'm getting is: an object reference is required for the non-static field, method
– user676853
Mar 19 '12 at 1:50
Ah Yes I messed up it should be currentBook.bookCode ect .... not Book.bookCode edit made to answer
– Micah Armantrout
Mar 19 '12 at 1:57
Ahh okay, now those errors are gone, I'm getting: Book is a 'type' But is used like a 'variable' - this is line: "Books.add(Book);"
– user676853
Mar 19 '12 at 2:01
ok fixed that too
– Micah Armantrout
Mar 19 '12 at 2:09
Okay, that got rid of the errors. How would I now go about putting the bookcodes into a listbox?
– user676853
Mar 19 '12 at 2:42
|
show 3 more comments
What's the best way to implement the read code? I'm using an open button to activate this storing process. Code: private void openToolStripMenuItem_Click(object sender, EventArgs e) - but all I'm getting is: an object reference is required for the non-static field, method
– user676853
Mar 19 '12 at 1:50
Ah Yes I messed up it should be currentBook.bookCode ect .... not Book.bookCode edit made to answer
– Micah Armantrout
Mar 19 '12 at 1:57
Ahh okay, now those errors are gone, I'm getting: Book is a 'type' But is used like a 'variable' - this is line: "Books.add(Book);"
– user676853
Mar 19 '12 at 2:01
ok fixed that too
– Micah Armantrout
Mar 19 '12 at 2:09
Okay, that got rid of the errors. How would I now go about putting the bookcodes into a listbox?
– user676853
Mar 19 '12 at 2:42
What's the best way to implement the read code? I'm using an open button to activate this storing process. Code: private void openToolStripMenuItem_Click(object sender, EventArgs e) - but all I'm getting is: an object reference is required for the non-static field, method
– user676853
Mar 19 '12 at 1:50
What's the best way to implement the read code? I'm using an open button to activate this storing process. Code: private void openToolStripMenuItem_Click(object sender, EventArgs e) - but all I'm getting is: an object reference is required for the non-static field, method
– user676853
Mar 19 '12 at 1:50
Ah Yes I messed up it should be currentBook.bookCode ect .... not Book.bookCode edit made to answer
– Micah Armantrout
Mar 19 '12 at 1:57
Ah Yes I messed up it should be currentBook.bookCode ect .... not Book.bookCode edit made to answer
– Micah Armantrout
Mar 19 '12 at 1:57
Ahh okay, now those errors are gone, I'm getting: Book is a 'type' But is used like a 'variable' - this is line: "Books.add(Book);"
– user676853
Mar 19 '12 at 2:01
Ahh okay, now those errors are gone, I'm getting: Book is a 'type' But is used like a 'variable' - this is line: "Books.add(Book);"
– user676853
Mar 19 '12 at 2:01
ok fixed that too
– Micah Armantrout
Mar 19 '12 at 2:09
ok fixed that too
– Micah Armantrout
Mar 19 '12 at 2:09
Okay, that got rid of the errors. How would I now go about putting the bookcodes into a listbox?
– user676853
Mar 19 '12 at 2:42
Okay, that got rid of the errors. How would I now go about putting the bookcodes into a listbox?
– user676853
Mar 19 '12 at 2:42
|
show 3 more comments
I would create class for the Book first
public class Book
{
public int BookCode { set;get;}
public string BookTitle { set;get; }
public decimal BookPrice { set;get}
public int Quantity { set;get;"
}
Read from your datasource( in this case the text file) and store it in a List of our Book Class
List<Book> bookList=new List<Book>();
bookList=BookManager.GetBookList(); // gets the list of books read from the text file.
Store this in your global scope so that you can access it from any of your methods in the form.
When you want to access specific data about a book use Linq to get it
var book=(from p in bookList where p.BookCode=2).FirstOrDefault();
if(book !=null)
{
lblPrice.Text=book.BookPrice.ToString();
}
To put the BookTitles to a List box, you can mention the DataTextField as BookTitle when you bind that.
lstBooks.DataSource=bookList;
lstBooks.DataValueField="BookCode";
lstBooks.DataTextField="BookTitle ";
lstBooks.DataBind();
To Read the file from your text file and fill it to your list of Book object, do some thing like this
public List<Book> GetBookList()
{
List<Book> objBooks=new List<Book>();
using (StreamReader file = new StreamReader(@"C:dataist.txt"))
{
while ((line = file.ReadLine()) != null)
{
char delimiters = new char { ',' };
string parts = line.Split(delimiters);
Book objBook=new Book();
objBook.BookCode=parts[0];
objBook.BookTitle =parts[0];
objBook.BookPrice =Convert.ToDecimal(parts[0]);
objBook.Quantity =Convert.ToInt32(parts[0]);
objBooks.Add(objBook);
}
file.Close();
}
return objBooks;
}
If possible, I will store the book details to a database table. It is more easy to manage and maintain and i dont need to worry about reading and writing to file.
I am assuming you are doing this for a windows form app.
add a comment |
I would create class for the Book first
public class Book
{
public int BookCode { set;get;}
public string BookTitle { set;get; }
public decimal BookPrice { set;get}
public int Quantity { set;get;"
}
Read from your datasource( in this case the text file) and store it in a List of our Book Class
List<Book> bookList=new List<Book>();
bookList=BookManager.GetBookList(); // gets the list of books read from the text file.
Store this in your global scope so that you can access it from any of your methods in the form.
When you want to access specific data about a book use Linq to get it
var book=(from p in bookList where p.BookCode=2).FirstOrDefault();
if(book !=null)
{
lblPrice.Text=book.BookPrice.ToString();
}
To put the BookTitles to a List box, you can mention the DataTextField as BookTitle when you bind that.
lstBooks.DataSource=bookList;
lstBooks.DataValueField="BookCode";
lstBooks.DataTextField="BookTitle ";
lstBooks.DataBind();
To Read the file from your text file and fill it to your list of Book object, do some thing like this
public List<Book> GetBookList()
{
List<Book> objBooks=new List<Book>();
using (StreamReader file = new StreamReader(@"C:dataist.txt"))
{
while ((line = file.ReadLine()) != null)
{
char delimiters = new char { ',' };
string parts = line.Split(delimiters);
Book objBook=new Book();
objBook.BookCode=parts[0];
objBook.BookTitle =parts[0];
objBook.BookPrice =Convert.ToDecimal(parts[0]);
objBook.Quantity =Convert.ToInt32(parts[0]);
objBooks.Add(objBook);
}
file.Close();
}
return objBooks;
}
If possible, I will store the book details to a database table. It is more easy to manage and maintain and i dont need to worry about reading and writing to file.
I am assuming you are doing this for a windows form app.
add a comment |
I would create class for the Book first
public class Book
{
public int BookCode { set;get;}
public string BookTitle { set;get; }
public decimal BookPrice { set;get}
public int Quantity { set;get;"
}
Read from your datasource( in this case the text file) and store it in a List of our Book Class
List<Book> bookList=new List<Book>();
bookList=BookManager.GetBookList(); // gets the list of books read from the text file.
Store this in your global scope so that you can access it from any of your methods in the form.
When you want to access specific data about a book use Linq to get it
var book=(from p in bookList where p.BookCode=2).FirstOrDefault();
if(book !=null)
{
lblPrice.Text=book.BookPrice.ToString();
}
To put the BookTitles to a List box, you can mention the DataTextField as BookTitle when you bind that.
lstBooks.DataSource=bookList;
lstBooks.DataValueField="BookCode";
lstBooks.DataTextField="BookTitle ";
lstBooks.DataBind();
To Read the file from your text file and fill it to your list of Book object, do some thing like this
public List<Book> GetBookList()
{
List<Book> objBooks=new List<Book>();
using (StreamReader file = new StreamReader(@"C:dataist.txt"))
{
while ((line = file.ReadLine()) != null)
{
char delimiters = new char { ',' };
string parts = line.Split(delimiters);
Book objBook=new Book();
objBook.BookCode=parts[0];
objBook.BookTitle =parts[0];
objBook.BookPrice =Convert.ToDecimal(parts[0]);
objBook.Quantity =Convert.ToInt32(parts[0]);
objBooks.Add(objBook);
}
file.Close();
}
return objBooks;
}
If possible, I will store the book details to a database table. It is more easy to manage and maintain and i dont need to worry about reading and writing to file.
I am assuming you are doing this for a windows form app.
I would create class for the Book first
public class Book
{
public int BookCode { set;get;}
public string BookTitle { set;get; }
public decimal BookPrice { set;get}
public int Quantity { set;get;"
}
Read from your datasource( in this case the text file) and store it in a List of our Book Class
List<Book> bookList=new List<Book>();
bookList=BookManager.GetBookList(); // gets the list of books read from the text file.
Store this in your global scope so that you can access it from any of your methods in the form.
When you want to access specific data about a book use Linq to get it
var book=(from p in bookList where p.BookCode=2).FirstOrDefault();
if(book !=null)
{
lblPrice.Text=book.BookPrice.ToString();
}
To put the BookTitles to a List box, you can mention the DataTextField as BookTitle when you bind that.
lstBooks.DataSource=bookList;
lstBooks.DataValueField="BookCode";
lstBooks.DataTextField="BookTitle ";
lstBooks.DataBind();
To Read the file from your text file and fill it to your list of Book object, do some thing like this
public List<Book> GetBookList()
{
List<Book> objBooks=new List<Book>();
using (StreamReader file = new StreamReader(@"C:dataist.txt"))
{
while ((line = file.ReadLine()) != null)
{
char delimiters = new char { ',' };
string parts = line.Split(delimiters);
Book objBook=new Book();
objBook.BookCode=parts[0];
objBook.BookTitle =parts[0];
objBook.BookPrice =Convert.ToDecimal(parts[0]);
objBook.Quantity =Convert.ToInt32(parts[0]);
objBooks.Add(objBook);
}
file.Close();
}
return objBooks;
}
If possible, I will store the book details to a database table. It is more easy to manage and maintain and i dont need to worry about reading and writing to file.
I am assuming you are doing this for a windows form app.
edited Mar 19 '12 at 1:21
answered Mar 19 '12 at 1:12
ShyjuShyju
146k87331440
146k87331440
add a comment |
add a comment |
Book objBook = new Book();
objBook.BookCode=Convert.ToInt32(parts[0]);
objBook.BookTitle =parts[0];
objBook.BookPrice =Convert.ToDecimal(parts[0]);
objBook.Quantity =Convert.ToInt32(parts[0]);
Your array indexing example is always pinned at [0]...
– Aaron Hudon
Nov 14 '18 at 22:10
add a comment |
Book objBook = new Book();
objBook.BookCode=Convert.ToInt32(parts[0]);
objBook.BookTitle =parts[0];
objBook.BookPrice =Convert.ToDecimal(parts[0]);
objBook.Quantity =Convert.ToInt32(parts[0]);
Your array indexing example is always pinned at [0]...
– Aaron Hudon
Nov 14 '18 at 22:10
add a comment |
Book objBook = new Book();
objBook.BookCode=Convert.ToInt32(parts[0]);
objBook.BookTitle =parts[0];
objBook.BookPrice =Convert.ToDecimal(parts[0]);
objBook.Quantity =Convert.ToInt32(parts[0]);
Book objBook = new Book();
objBook.BookCode=Convert.ToInt32(parts[0]);
objBook.BookTitle =parts[0];
objBook.BookPrice =Convert.ToDecimal(parts[0]);
objBook.Quantity =Convert.ToInt32(parts[0]);
answered Nov 14 '18 at 21:46
David ParkerDavid Parker
1
1
Your array indexing example is always pinned at [0]...
– Aaron Hudon
Nov 14 '18 at 22:10
add a comment |
Your array indexing example is always pinned at [0]...
– Aaron Hudon
Nov 14 '18 at 22:10
Your array indexing example is always pinned at [0]...
– Aaron Hudon
Nov 14 '18 at 22:10
Your array indexing example is always pinned at [0]...
– Aaron Hudon
Nov 14 '18 at 22:10
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9763796%2freading-from-a-text-file-and-splitting-each-individual-line-into-different-arra%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Split should do what you're looking for. string partArray = inputLine.Split(',');
– That Chuck Guy
Mar 19 '12 at 1:08
I want to split the lines, then I want to store those lines into a list/array, and once I've got the lines stored I want to split again by ','. Is it possible to do this?
– user676853
Mar 19 '12 at 1:12