Solved

better way?

Posted on 2003-11-09
12
317 Views
Last Modified: 2010-04-16
What's the better way of doing this:

public class Inventory
{
   int[] available={0, 0};
   String[] products={"pen", "paper"};
   Hashtable InvenTable;

   public Inventory()
   {
      InvenTable=new Hashtable();
      for (int i=0; i<products.Length; i++)
          InvenTable.Add(products[i], Convert.ToString(available[i]));
   }
   public Hashtable Available
  {
     get{}
     set{}
  }
}

Whenever I instantiate this class, "available" is always initiated with 0's, which is not what I want. (I do not want to use a database since the number of products is small.) I am also going to allow updates of the Inventory, of course, from a user interface.

Any comments on improving the program is welcome.

btw, Is the singleton pattern useful in this case?

Thanks.

0
Comment
Question by:samliam
  • 6
  • 5
12 Comments
 
LVL 4

Expert Comment

by:Tonylmiller
ID: 9711834
What do you want "available" to be?  I never see you set it equal to anything. . .

If you really do not want to use a database, you could make a dataset and when you are finished, you could save it to xml.  This is really easy to do.  Then, next time you need the data, just read the xml file again into a dataset.  If you make changes, you could write them back out to xml again.

Good luck!

Tony
0
 

Author Comment

by:samliam
ID: 9712782
To create XML, I need to make a dataset. To make a dataset, I better off start with a database. Is there an easy way that I can create XML just starting with visual studio.net?
0
 

Author Comment

by:samliam
ID: 9712798
OK. I see that I can create a dataset using VS.net.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 9713152
Hello

  You don't need to use Dataset to work with XML files, you can use xml serialization to accomplish this work, here are some aricles to help you:

XML Serialization in .NET
http://www.codeproject.com/csharp/xmlserialize.asp
XML Serialization in C#
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=236
Introducing XML Serialization
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconintroducingxmlserialization.asp

and here's an aricle to save the hashtable to binary format, without using xml or dataset:

Hash Table and Serialization in .NET
http://www.codeproject.com/dotnet/phonebook.asp

HTH
0
 
LVL 4

Expert Comment

by:Tonylmiller
ID: 9714705
You can make your own dataset and then save it to xml, or you can make xml first.  It doesn't really matter.  But it is very easy to work with xml and datasets.  You will have to decide for yourself (or get some more advice) concerning which is easier - serialization or datasets.  Personally I think datasets and xml are two of the major tools within .NET and you might as well use them.  Datasets and xml can contain multiple related tables, so theoretically you could use it like a database if you don't want to use a database.

Good luck!

Tony
0
 

Author Comment

by:samliam
ID: 9725760
Thanks for the comments. My question really is on how to define the class. The data is simply a 2-D array with just 8 items. It should be like:

pen 98
paper 75
 ....

I want to put these in global.asax like a counter. These seem to be too simple to use an XML structure anyway..
0
 
LVL 4

Expert Comment

by:Tonylmiller
ID: 9726078
O.k., but how will you store your data?  If it can be hard coded into the program, that is fine.  XML would be another easy way to store it.
0
 

Author Comment

by:samliam
ID: 9734304
How do I store the data? That's the question. Hard-code is not the right way of course.

What I was thinking was maybe in the global.asax.cs? or web.config?

So the XML will look like this?

<supply>
  <pen>
     35
  </pen>
  <paper>
    78
  </paper>
</supply>
0
 
LVL 4

Accepted Solution

by:
Tonylmiller earned 50 total points
ID: 9734740
If you are using VS.NET, just make a new xml file and type in the data in the editor pretty much like you have it there.  Very easy. . .

Notice there are two "tab" things down in the lower left.  One is for xml, the other for data.  You can edit your data directly in a table format if you click on the "data" tab (or button or whatever it is called).

Here is some fake data that I used for troubleshooting:

<?xml version="1.0" encoding="utf-8" ?>
<data>
      <case>
            <defendant>Shannon Hon</defendant>
            <number>111111110</number>
            <warrant>true</warrant>
            <offense>Driving Under Influence</offense>
            <offensedetail>BAC = 0.12%</offensedetail>
            <ticket>3423523</ticket>
            <plea></plea>
            <sel>false</sel>
            <officers>Jim Jones, Tony Jones</officers>
            <attorneys></attorneys>
            <disposition></disposition>
            <orders></orders>
            <officer>Jim Jones</officer>
            <officer>Tony Jones</officer>
      </case>
      <case>
            <defendant>Krissie Huon</defendant>
            <number>111111111</number>
            <warrant>true</warrant>
            <offense>Theft</offense>
            <offensedetail></offensedetail>
            <plea></plea>
            <sel>false</sel>
            <officers>Fred Jones, AJ Jones, Krissie Jones</officers>
            <attorneys>(D)Terry Smith, (P)Sarah Brown</attorneys>
            <disposition></disposition>
            <orders></orders>
            <officer>Fred Jones</officer>
            <officer>AJ Jones</officer>
            <officer>Krissie Jones</officer>
      </case>
</data>


Here is how you read the data:

DataSet myDS = new DataSet();
myDS.ReadXml("c:\\DocketCaseData.xml", XmlReadMode.Auto);

That will read the xml into your dataset.

It is just as easy to save the data to xml.
0
 

Author Comment

by:samliam
ID: 9734922
It I "Add New Item" -> XML file, I get a page showing this line:

"<?xml version="1.0" encoding="utf-8" ?>"

If I click on the "data" button at the low left corner, It says:

"The XML Doc is not well formed, .. root element is missing." So I'll have to add something directly first, before I can use the table format?
0
 
LVL 4

Expert Comment

by:Tonylmiller
ID: 9735397
Yes.  You have to add your root element (<data> in my case).  Using the xml button.

Then you can add your records (<case> in my case).

If you paste the xml just like you typed it above, it should work.

"just make a new xml file and type in the data in the editor pretty much like you have it there. "

Good luck!

Tony
0
 

Author Comment

by:samliam
ID: 9735464
I still think web.config or global.asax is the better approach for my problem. Thanks for the XML starter.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Install Problem 13 29
PrintingFoundIt(Almost!(Damn!)) 1 26
Printing 1 51
transaction in asp.net, sql server 6 31
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question