|
How To Calculate The Total Data Download in c#
|
|
|
|
|
Use a timer.
Use the best guess
|
|
|
|
|
|
Between us we could come up with some numbers.
Use the best guess
|
|
|
|
|
You need to create a small program in C# for calculating total downloaded data ?
|
|
|
|
|
Hello,
Im doing a program with Visual Studio in C #. I need to get information from another device by the Ethernet port, read that information, and if correct, run the program. Any idea how I could do or how to manage the Ethernet port?
Thank you.
|
|
|
|
|
The first thing to do is to check the manual for the device to see what protocol it uses to communicate. Assuming it is a network device then you can probably use network sockets, for which Google will find you lots of sample code.
Use the best guess
|
|
|
|
|
Hi! I made a library-project in C#.
I save the customers in one list and the books in one.
When I register a that a Customer borrows a book this is what happends: (Sorry for my english, I am Swedish)
First the customer (lender) is assigned to the book, so the book "knows" who has borrowed it.
bookToLend.BiblioteksKund = lender;
Then the book is added to a List-control owned by the customer-object so the customer-object remembers what books the customer has borrowed.
This works fine when the program is running. The problem occurs when I try to save the objects to disc and then retrieve them again when the program starts. I have asked for help and googled it but I can´t make it work.
The problem seems to be the cross-reference of objects.
I´ll be happy to send the project if someone wants to look at it.
I have tried to use Serialization but have not succeeded.
Perhaps Linq would be a better choise.
|
|
|
|
|
larsp777 wrote: Then the book is added to a List-control owned by the customer-object so the customer-object remembers what books the customer has borrowed.
This works fine when the program is running. The problem occurs when I try to save the objects to disc and then retrieve them again when the program starts. I have asked for help and googled it but I can´t make it work. Post that part of the code (in PRE tags), we'll have a look at it
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
List<Bok> BookList = new List<Bok>();
List<Kund> CustomerList = new List<Kund>();
static string dir = @"C:\Bibliotek\";
string path = dir + "Kunder.xml";
string path2 = dir + "Books.xml";
XmlSerializer xs = new XmlSerializer(typeof(List<Kund>));
XmlSerializer xs2 = new XmlSerializer(typeof(List<Bok>));
public void sparaKund()
{
using (Stream s = File.Create(path))
{
xs.Serialize(s,CustomerList);
}
using (Stream s2 = File.Create(path2))
{
xs2.Serialize(s2, BookList);
}
}
The file is created but is empty.
private void Form1_Load(object sender, EventArgs e)
{
if (File.Exists(path))
{
using (Stream s = File.OpenRead(path))
{
CustomerList = (List<Kund>)xs.Deserialize(s);
}
}
if (File.Exists(path2))
{
using (Stream s2 = File.OpenRead(path2))
{
BookList = (List<Bok>)xs2.Deserialize(s2);
}
}
}
|
|
|
|
|
According to your code snippet, you save freshly generated lists - which are empty. So the files are expected to contain the XML header only. Or did you just not show us the code where you fill the objects into the lists?
When you run your program in Debug mode (e.g. from Visual Studio, or a debug build), you can also use an Assertion before saving the data:
System.Diagnostics.Debug.Assert(CustomerList.Count > 0);
|
|
|
|
|
Well I didn't show the code where I fill the lists, but I do fill them.
I kind of works saving but not reloding.
Here is the XML-file:
<arrayofkund xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
|
|
|
|
|
Looks good enough for me. Can you now post the definition of the Kund and Book-classes?
I hope that each book has a unique number, or GUID, and I hope to see a list of those numbers in the Kund-object that borrowed them. My guess is that you're saving the lists without the references.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Well, you set the unique number when you register a book (Bok) or a customer (kund).
It's not a Commercial application.
Book:
[Serializable()]
public class Bok : ISerializable
{
protected int isbn = 0;
protected string titel = null;
protected string author;
protected int price;
protected String isType = null;
protected Kund biblioteksKund = null;
public int ISBN
{
get
{
return isbn;
}
set
{
isbn = value;
}
}
public string Titel
{
get
{
return titel;
}
set
{
titel = value;
}
}
public string Author
{
get
{
return author;
}
set
{
author = value;
}
}
public int Price
{
get
{
return price;
}
set
{
price = value;
}
}
public String IsType
{
get
{
return isType;
}
set
{
isType = value;
}
}
public Kund BiblioteksKund
{
get
{
return biblioteksKund;
}
set
{
biblioteksKund = value;
}
}
public Bok()
{
}
public Bok(int isbn, string titel, string author, int price, SerializationInfo info, StreamingContext ctxt)
{
ISBN = isbn;
Author = author;
Titel = titel;
Price = price;
biblioteksKund = new Kund();
this.isbn = (int)info.GetValue("ISBN", typeof(int));
this.author = (string)info.GetValue("Author", typeof(string));
this.titel = (string)info.GetValue("Titel", typeof(string));
this.price = (int)info.GetValue("Price", typeof(int));
this.biblioteksKund = (Kund)info.GetValue("Kund", typeof(Kund));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("ISBN", this.isbn);
info.AddValue("Author", this.author);
info.AddValue("Titel", this.titel);
info.AddValue("Price", this.price);
info.AddValue("BiblioteksKund", this.biblioteksKund);
}
public virtual void skrivUt()
{
Console.WriteLine("Författare: {0}", Author);
Console.WriteLine("Titel: {0}", Titel);
Console.WriteLine("Pris: {0}", Price);
}
public virtual String getInfo()
{
return null;
}
public String getStatus()
{
if (BiblioteksKund != null)
return ("Utlånad till: " + BiblioteksKund.Name);
else
return ("Ej utlånad");
}
}
}
Customer:
[Serializable()]
public class Kund : ISerializable
{
private int personNr = 0;
private string name;
private List<Bok> loan = new List<Bok>();
public int PersonNr
{
get
{
return personNr;
}
set
{
personNr = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public List<Bok> Loan
{
get
{
return loan;
}
}
public Kund()
{
}
public Kund(int personNr, string name)
{
PersonNr = personNr;
Name = name;
}
public Kund(SerializationInfo info, StreamingContext ctxt)
{
this.personNr = (int)info.GetValue("PersonNr", typeof(int));
this.name = (string)info.GetValue("Name", typeof(string));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("PersonNr", this.personNr);
info.AddValue("Name", this.name);
}
public virtual void skrivUt()
{
Console.WriteLine("Personnr: {0}", PersonNr);
Console.WriteLine("Namn: {0}", Name);
}
}
|
|
|
|
|
larsp777 wrote: It's not a Commercial application. That makes no difference.
larsp777 wrote: Well, you set the unique number when you register a book (Bok) or a customer (kund). What does 'skrivUt' mean?
There's no list on "who" borrowed "wich" book. Remember that a ISBN-number doesn't idenitfy a specific book, but a "title". One could have multiple copies of the same title
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Well, since it is run in a controlled environment I can make sure that there is only one copy with a specific ISBN.
Otherwise you are right of course, ISBN identyfies a title, not a specific book.
No, there is no list of who borrowed which book but each book-object "knows" who borrowed it with a reference to a customer object.
protected Kund biblioteksKund = null;
SkrivUt means "print" and is simply printing data about a customer but I don't think is is ever used here. (It was initially a assignment from my University made in Java.)
The reason I mentioned that it is not a Commercial application was that I could made sure that no two objects are the same. But maybe that doesn't matter. Shold I use something like a GUID?
|
|
|
|
|
larsp777 wrote: The reason I mentioned that it is not a Commercial application was that I could made sure that no two objects are the same. But maybe that doesn't matter. Shold I use something like a GUID? I dunno, and this is the place where things get complicated.
Let's say I borrowed Pratchetts' book "Small Gods". You have three books of this story - how are you gonna track each book you lent?
Answer; give every book a unique number (yeah, like a GUID )
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy Vluggen wrote: dunno, and this is the place where things get complicated. Let's
say I borrowed Pratchetts' book "Small Gods". You have three books of this story
- how are you gonna track each book you lent? Answer; give every
book a unique number (yeah, like a GUID )
Yes, but as I said it's a controlled environment where I make sure that there is only one copy of each book.
The question is if this actually is the reason to why it doesn´t work? Or could it be that the list I am trying to save is a empty list even if I use the same name?
This is the event for the button where I register the loan.
private void btnRegister_Click(object sender, EventArgs e)
{
Kund lender = null;
Bok bookToLend = null;
foreach (Kund K in CustomerList)
{
if (txtPersonNr.Text.Equals(K.PersonNr.ToString()))
lender = K;
}
if (lender == null)
{
textBox7.Text = "Kund saknas!";
return;
}
foreach (Bok B in BookList)
{
if (textBox6.Text.Equals(B.ISBN.ToString()))
bookToLend = B;
}
if (bookToLend == null)
{
textBox7.Text = "Bok saknas!";
return;
}
if (bookToLend.BiblioteksKund == null)
{
bookToLend.BiblioteksKund = lender;
lender.Loan.Add(bookToLend);
textBox7.Text = "Lån registrerat!";
}
}
|
|
|
|
|
larsp777 wrote: Or could it be that the list I am trying to save is a empty list even if I use the same name? Looks that way; a Kund holds a list of books. When you serialize the Kund, you write the PersonNr and the Name - but not the list.
Again, I'd recommend not saving a list of books, but numbers that are linked to a book.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy Vluggen wrote: Again, I'd recommend not saving a list of books, but numbers that are linked to
a book.
Ok, Think I missunderstood you some.
But I still have to save the objects somehow so I'm not really sure how that solves anything. Please explain.
Edit: Realised that you probably ment the list saved in customer, not the list that holds all the books. That could be a way of avoiding cross-reference I guess.
|
|
|
|
|
larsp777 wrote: But I still have to save the objects somehow so I'm not really sure how that solves anything. Please explain. It'd be a translation from a database-design.
Right now, each book would be stored in a Kund? That means that you'd be "moving" the book-data when the book moves between different Kunds; it'd be saved in a different list.
If you had three lists, life could become easier; one list to hold a collection of Kunds, one to hold the Books, and one to hold a pointer to Kund/Book combinations. In SQL, it'd be something like below;
CREATE TABLE Customer
(
Id INT IDENTITY(1,1)
PRIMARY KEY (Id)
)
CREATE TABLE Book
(
Id INT IDENTITY(1,1)
PRIMARY KEY (Id)
)
CREATE TABLE LentItems
(
CustomerId INT,
BookId INT
FOREIGN KEY (CustomerId) REFERENCES Book(Id),
FOREIGN KEY (BookId) REFERENCES Book(Id)
)
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy Vluggen wrote: Right now, each book would be stored in a Kund? That means that you'd be
"moving" the book-data when the book moves between different Kunds; it'd be
saved in a different list.
This is how it works (as far as I know):
Every book is stored in a list BookList and every customer is saved in CustomerList;
Every customer (Kund) has it´s own list loan where references are saved to books that are borrowed by that customer.
Again, this was a project I made in Java for a University-course a while back. Been trying to transfer it to C#.
Every book (Bok in Swedish) has a reference to the customer who borrowed the book, so it "knows" who borrowed it. The savingpart worked fine in Java but maybe works differently in C#.
|
|
|
|
|
larsp777 wrote: Again, this was a project I made in Java for a University-course a while back. Been trying to transfer it to C#.
Every book (Bok in Swedish) has a reference to the customer who borrowed the book, so it "knows" who borrowed it. The savingpart worked fine in Java but maybe works differently in C#. Those references are pointers; I don't think that the XmlSerializer is going to save the private loan-list on it's own.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy Vluggen wrote: Those references are pointers; I don't think that the XmlSerializer is going to
save the private loan-list on it's own.
Yes, I know they are pointers. That is why you get cross-reference, isn´t it?
Still, you could be right in that this is the problem.
Edit: I think this was one thing I considered when I was trying to find the answer. Saving a list of books was no problem.
|
|
|
|
|
Does the original Java-generated have it's loan-list in the XML?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|