Need help with random number of possible variables in a file.

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 2694 Views - Last Post: 22 September 2012 - 09:55 AM Rate Topic: -----

#1 ljrobison08  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 03-June 12

Need help with random number of possible variables in a file.

Posted 20 August 2012 - 12:56 PM

Hello all,

I'm fairly new to C# programming, I have built a simple calculator for my mother to help with her business, and I have taught my self a lot of C++ through out the years. Now I am looking to take this a step further and have most of it contained in the program, with less work for her to do.

Here is a layout of the Form I have. I find it easier to explain what I want it to do with a picture.

Posted Image

Basically, I want her to open it up and click open and browse for a txt file with the recipe name she is looking for. (She creates her own makeup). I then want this recipe to be displayed in the Recipe TextBox. Then I want her to be able to type a number of ounces in the smaller text box below the filepath and click convert. The program would then read the recipe line by line and extract each ingredients name and weight(s) so that I may do calculations with them and return a larger/smaller recipe to the textBox.

Now I can do all this fine, I have a function for opening the file (as in the Load button and Filepath box actually work), and I have set up a function for reading the file.

The problem I have is, some of her recipes may have 2 ingredients, some may have 4 or more etc.

I.e the text files may look like this (Filepaths represent seperate files):


C:\Recipe.txt
10.23g Ingredient1
12.10g Ingredient2

C:\Recipe2.txt
1.02g IngredientX
4.01g Ingredient97
0.09g ThisStuff
1.23g ThisStuff2

What would be the most efficient way to store these variables if I don't know how many there are going to be? I don't want to just create a large array and most of it remained unused. I tried FileHelper library, but it seems to only work with files that have that each object has the same number of variables.

If I could figure this out the rest should be pretty easy.

On a side note, what would be the best format for me to have the .txt files in? (CSV? etc) I'm not too sure with something like this.

I thank anyone who takes the time to read this. As always, any input will be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with random number of possible variables in a file.

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5439
  • View blog
  • Posts: 11,669
  • Joined: 02-June 10

Re: Need help with random number of possible variables in a file.

Posted 20 August 2012 - 01:20 PM

Planning, planning, planning. Plus some experience with C# would help a lot.

A text file really doesn't make life easy here.

I would create a class of "Ingredient" that has properties of:
string Description
decimal GramsPerServing

Then a class of "Recipe" with properties of:
string Title
List<Ingredient> Ingredients
string Instructions
string Warning
TimeSpan ShelfLife

Instead of saving to a textfile, serialize your Recipe instance to an XML file. This way you preserve the recipe as a complete object with all the properties.

Now your form just has to deserialize the XML file and perform a loop through the List<Ingredient> to calculate how much is needed for the number of servings. No different than any cooking recipe. In this case a 'serving' is probably considered 1 ounce.

Separating data from GUI - PLUS - serializing the data to XML

90% or more of the problems we see here are because rookies don't stop and plan nor block out their code into logical segments.
This article should help you see a way to block out your code by stubbing out the methods and logic, then filling in each method as you research it:
This is the 'Help me with my homework' article you wanted.

Oh, and as for the unknown number of ingredients: That's were a ListView instead of a TextBox would be good. Put the instructions in one Textbox, warning in another, and add all the Ingredients to a ListView of two columns: Description & Quantity.
Was This Post Helpful? 3
  • +
  • -

#3 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,713
  • Joined: 08-June 10

Re: Need help with random number of possible variables in a file.

Posted 20 August 2012 - 01:33 PM

I'm with tlhIn`toq here. XML serialization is your best bet.

To answer your question simply, you'd want to use a List<T> when you want something like an array but don't know the size ahead of time. It has methods for adding and removing values.

But serialization here can help you a lot more than trying to parse a text file list of ingredients.

Also, instead of serialization, you could use a portable database, like Sql Server Compact, or sqlite. Some find database interaction easier than filesystem interaction.
Was This Post Helpful? 2
  • +
  • -

#4 ljrobison08  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 03-June 12

Re: Need help with random number of possible variables in a file.

Posted 20 August 2012 - 01:46 PM

Thank you so much. There is a lot of information in that post that I was looking for. I haven't really messed around with XML files yet, but I had an idea they would be the preferred method.

So would I have all the XMLs saved somewhere that could be loaded just as a txt file? Or would it be all contained in the program. I think I'm started to understand how you described the program flow. The reason I ask, is I want her to be able to add new recipes in on her own at times. If its just following any easy XML template like I saw in that one link you posted, she will have no problem with that.

But thank you very much again for the post, I will be accomplishing so much more on this project tonight because of it.

Sorry for the double post, could not find the edit button anywhere. When I said:

"So would I have all the XMLs saved somewhere that could be loaded just as a txt file?"

I just meant, would she be able to click Load and choose the Recipe XML she wanted?
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5439
  • View blog
  • Posts: 11,669
  • Joined: 02-June 10

Re: Need help with random number of possible variables in a file.

Posted 20 August 2012 - 01:48 PM

If you're more comfortable with the files then yes, they would save individually just like text files.

Though I wouldn't make her try to enter into a file template. That's not user friendly.

With a little bit of work the same screen used to display a read-in file could also be used to save a new recipe. She would enter the title, instructions, ingredients and so on then click a 'Save' button and your code would then serialize the new recipe to a file.
Was This Post Helpful? 0
  • +
  • -

#6 ljrobison08  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 03-June 12

Re: Need help with random number of possible variables in a file.

Posted 20 August 2012 - 01:53 PM

Yeah I like that idea a lot more.

I eventually want to make this a self-containing "database" of sorts with built in tools she can use for actually making the recipes. The whole process she does is fairly simple, so I knew I could eventually program something the help her out. I tried in C++ first as it's my native language, (still not by any means advanced at C++ or anything). But I am finding C# to be much better to develop something like this with.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5439
  • View blog
  • Posts: 11,669
  • Joined: 02-June 10

Re: Need help with random number of possible variables in a file.

Posted 20 August 2012 - 02:01 PM

C# is an Object Oriented language. And the world is an Object Oriented environment. So it goes well. If you're not thinking in terms of 'objects' then you're at a disadvantage. This might help.

Think of objects (classes) in coding just as you would objects in the real world.

A Dodge Ram is an object.
It is made up of smaller objects: Engine, doors, tires
Each of those is made up of smaller objects: Bolts, pistons, etc.

Objects in coding can inherit from each other, usually from the general to the specific.

  • Class vehicle
    • Class truck : vehicle
      • Class Ram : Truck
        • class 2500FWD : Ram


A class is the blueprint for instanciating (making an instance of) the object.

DodgeRam is a class describing how to make an instance, but itself is not an actual thing you can interact with.

myDodgeRam is an instance of the class DodgeRam

I can do things with the object instance myDodgeRam

myDodgeRam.SerialNumber = 123456789;
myDodgeRam.FillUpTank();
float fuelLevel = myDodgeRam.FuelTankPercentageFull;
if (myDodgeRam.IsReady) myDodgeRam.StartEngine();


Anything defined in the base class is available to a child.

class truck : vehicle
{

    public float FuelTankPercentageFull
    {
       get; set;
    }
}

class DodgeRam : truck
{
    // I don't have to define a FuelTankPercentageFull here because I inherit it from my parent
}


Methods defined as virtual in the base class can be overridden by the child class (at least in .NET languages like C#). This is often to account for more specific needs.

class truck : vehicle
{

    public virtual bool StartEngine()
    {
       // Do something to start the engine
       return true; // No checks or requirements
    }
}

class DodgeRam : truck
{
    public override bool StartEngine()
    {
        // Do a safety check first
        if (IsSeatBeltsEngaged && IsFootOnBrake)
        { 
            return true;
        }
        return false;
    }
}


You can even have a child class call the base classes methods which is often the smart way to go.

class truck : vehicle
{

    public virtual bool StartEngine()
    {
       // Notice there are no safety checks before trying to start up.
       try
       {
          // Do something to start the engine
          return true; // because we succeeded
       }
       catch(exception error)
       {
           return false; // because there was an error
       }
    }
}

class DodgeRam : truck
{
    public override bool StartEngine()
    {
        // Do a safety check first
        if (IsSeatBeltsEngaged && IsFootOnBrake)
        { 
            return base.StartEngine();
            // Now all the electrical work is in the base class
            // and not repeated in every child.
        }
    }
}


These should help as well.


Bulding an application - Part 1
Building an application - Part 2
Quick and easy custom events
Was This Post Helpful? 2
  • +
  • -

#8 ljrobison08  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 03-June 12

Re: Need help with random number of possible variables in a file.

Posted 20 August 2012 - 02:18 PM

I can't thank you enough for all this help. My first post on here, and I get exactly what I was looking for explained in a way I can understand. I think I'll definitely be sticking around here haha.

So, from what I can see, the best thing for me to do right now is definitely some planning.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5439
  • View blog
  • Posts: 11,669
  • Joined: 02-June 10

Re: Need help with random number of possible variables in a file.

Posted 20 August 2012 - 02:30 PM

WinForms is to a great extent more direct that WPF (Windows Presentation Foundation) but...

As this is all new for you anyway, WPF is the future of Windows applications so you might as well just jump in with both feet. Taking a year to be really comfortable with WinForms just to realize you are now a year further behind the new current technology just seems a shame.
Was This Post Helpful? 1
  • +
  • -

#10 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,713
  • Joined: 08-June 10

Re: Need help with random number of possible variables in a file.

Posted 20 August 2012 - 03:05 PM

And just think, now you've got a whole new thing to worry about being behind on (Metro-style apps). Of course, they draw heavily on WPF, so learning it will still help.
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5439
  • View blog
  • Posts: 11,669
  • Joined: 02-June 10

Re: Need help with random number of possible variables in a file.

Posted 20 August 2012 - 03:15 PM

Don't even get me started on Win8 and metro apps.

I installed Win8 on one of my laptops: What a piece of (*$($%&@) !!!

I plan on skipping over Win8 just like we skipped over that Vista garbage.

This post has been edited by tlhIn`toq: 20 August 2012 - 03:16 PM

Was This Post Helpful? 1
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,725
  • Joined: 05-May 12

Re: Need help with random number of possible variables in a file.

Posted 20 August 2012 - 04:25 PM

Off topic:

LOL!

<sarcasm>
But a recipe program would be perfect running on a Win8 tablet in the kitchen. Recipe's running fullscreen with no distracting chrome, or annoying pop ups from your kids or quilting group to distract you. Swipe from the right side and do a search for substitute ingredients. Hold down a finger in one of the corners to start YouTube and watch that latest baking techniques video. Swipe back to the start screen and go back to your recipe. Turn off device to wipe the screen because you have all these greasy, sticky fingerprints on it now from all the food ingredients. Turn device back on and it boots quickly. Start recipe program again swiping through the screens again to find it. Now screen is messy again. Give up and pull out the old index cards with the recipes and fire up the iPad to play some videos of the grandkids.
</sarcasm>

Notice that there isn't a Windows 8 ad campaign like the old Windows 7 campaign that went "Windows 7 was my idea."

Back on topic:
Seriously, though, you'll probably be better served learing WPF. A new side project I'm helping with is committing to going with WinForms because of the target audience's expected machine capabilities, but I'm planning on architecting the code to be MVC or MVVM style so that the 'V' can be easily replaced when it becomes evident that WPF is the clear winner long term.
Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5439
  • View blog
  • Posts: 11,669
  • Joined: 02-June 10

Re: Need help with random number of possible variables in a file.

Posted 21 August 2012 - 07:02 AM

My last couple of applications have been WPF instead of WinForms. I simply made the decision to go that direction because I do like the power available, databinding and behavior of WPF. Some stuff is harder to code that it was for WinForms (such as simply assigning a keystroke to a menu option) but other things (mostly involving auto resizing of controls and spaces) and ability to cascade styles down the control tree etc. are very nice.
Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,713
  • Joined: 08-June 10

Re: Need help with random number of possible variables in a file.

Posted 21 August 2012 - 07:52 AM

I prefer how the layout works in WPF. Everything is absolute by default in Windows Forms, and you have to screw around with anchoring and docking to keep things in position when you change the form size. By default, in WPF most controls will scale to fill their container (except goddamn StackPanels, but they're a different story). So you just have to get the alignment correct and things scale for you.

Regarding the Windows 8 stuff...I've been using it exclusively on my home PC now for several days, and I have to say, you get used to it pretty quick. At first, it's disorienting, but once you set it up the way you want it, it starts to grow on you. The fact that all the metro apps share the same hotkeys and same interfaces is a big bonus. The hotcorners are a pain on a multimonitor setup, but again, you get used to it eventually. And I do really like some of the improvements. The ribbon on Windows Explorer, the multimonitor taskbar, the new Task Manager, things like that are great.
Was This Post Helpful? 0
  • +
  • -

#15 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5439
  • View blog
  • Posts: 11,669
  • Joined: 02-June 10

Re: Need help with random number of possible variables in a file.

Posted 21 August 2012 - 08:24 AM

I hate the lack of a Start button/orb. I found a 3rd party replacement that will make due.

I hate the inability to turn off the child interface (tiles). There needs to be the ability to boot directly to the desktop. This alone makes it hard for me to see in the commercial world. How would you have a PC boot and launch the one application it's purposed with such as a kiosk, Point of Sale software, company full screen application, etc.

Splitting up settings sucks. Some stuff in "PC Settings" that as far as I can tell is only available from tiles, and the rest in control panels.

The transition is not full, its muddled. There needs to be the ability to run fully in one mode or the other. Having to flip back and forth is just a kludgy mess.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2