Solved

better way?

Posted on 2003-11-09
12
300 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
 
LVL 22

Expert Comment

by:mnasman
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now