Solved

better way?

Posted on 2003-11-09
12
338 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

707 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