Should I use a STRUCT?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rb0135
    New Member
    • Jan 2010
    • 20

    Should I use a STRUCT?

    Hi,

    Creating an app for a C# windows mobile device for my golfing.

    I normally use VB.NET and it seems easier working with global vars, etc. but I prefer to switch to C#.

    Anyway, I need to "group" about 10-15 vars (mixture of double, int and string) for the hole I am playing.

    Shoud I use a struct and group these vars? I need to access them globally and at the moment the few vars I have created are working globally, but before going to far, I would like to get this right.

    I have a generalvars class, where I am placing routines that need to be accessed via different forms, and I am using a few datasets to store information that gets written to an XML file.

    Can anyone offer some pointers if I am on the right track or is there another "container type" to use (other than a class).

    Thanks,
    Rob
  • markmcgookin
    Recognized Expert Contributor
    • Dec 2006
    • 648

    #2
    Ok, I think the first step you should do is look into Object Orientated Programming.. It sounds perfect for this.

    You could create a course class, which has a collection of 18 hole objects, then you could create a hole class, with variables like hazards, bunkers, length, width of fairway, elevation, hole position etc... within each of these classes you could create all the methods that you talk about. So when you come to access your hole you can just access the methods/properties you need with things like:

    MyCourse.Holes[11].DistanceToPin( myPosition);

    So think about creating an object for the main aspects of your system. Here's a wikipedia link to some OO concepts, I think that if this is a social or fun project as opposed to a time critical business one (although to be honest, even if it is, I would recommend learning OO) this could be a great opportunity to learn something new.

    Writing the application like this makes it much more scalable and easier to debug, and write.

    Let me know if you have any questions, and keep us posted on how you get on.

    Comment

    • rb0135
      New Member
      • Jan 2010
      • 20

      #3
      Thanks for the reply.

      I used to do a lot of OOP back in the days of Pascal, and C++, but switched to VB for ease of getting the job done. I prefer to use OOP but I've lost most of the talent there. I have done small C# programs, but nothing complicated.

      This isnt business critical, just a fun project, mainly to create a complicated C# program for my own and a friends use.

      I like your idea and appreciate your time to reply to my initial question. Thanks also for the link.

      I have written about 90% of it at the moment, interfacing into the GPS and recording what is needed, but, with incorrect OOP style of variable storage.

      I like your idea and will now go about creating the "perfect and correct" app.

      I will keep you posted, but it might be another week until I get some time to do it.

      Thanks again,
      Rob

      Comment

      • markmcgookin
        Recognized Expert Contributor
        • Dec 2006
        • 648

        #4
        Rob, no worries, always glad to help a fellow golfer!

        I have toyed with the idea of a similar app myself, but never got round to doing it.

        There's nothing to stop you hacking in a few OO classes back into your project, I mean I do "fairly" OO stuff all day long, but no one is perfect and we do hack little bits of code around it, there is probably no "perfect" code out there at all. Don't be afraid to stick a few classes in and benifit from their OO-ness, but leave all the quick and dirty code in there.. hell, if it ain't broke, don't fix it right?

        I have done a lot of work with WM and GPS and stuff, so feel free to ask away anything here.

        :)

        Comment

        • rb0135
          New Member
          • Jan 2010
          • 20

          #5
          I just have to get out of the VB mindset and think OOP again..

          I was hacking the C# just to get a proof of concept as to whether my idea was going to work or not and more importantly, interfacing the GPS (which ended up the easiest). Now, it has got to a point that yes, this will work and then I realised, even though the app is mine, I should do it "properly" and to do so, the structure of my variables was like VB.. just plonked in a class.

          There are a few apps around for us golfers, but they just dont combine all the features I wanted (like all programs seem to be) and that usually leads me to write my own.

          Thanks again... I am sure I will be asking more questions soon.

          Rob

          Comment

          • rb0135
            New Member
            • Jan 2010
            • 20

            #6
            Mark,

            Just wanted to check something. First of all, I load each hole as I go from xml files, so I am thinking of only have one "HOLE" object.

            You say:
            You could create a course class, which has a collection of 18 hole objects, then you could create a hole class, with variables like hazards, bunkers, length, width of fairway, elevation, hole position etc...
            So are you suggesting something like:

            Code:
            namespace xyz
            {
                public class Course
                {
                     object[] hole = new object[5];
            
                     hole[0] = ....;
                    etc.. etc..
                }
            }
            Still a little lost on your idea I think.

            Are you able to show a small example to get me started on what you suggested.

            Thanks again for your help,
            Rob

            Comment

            • rb0135
              New Member
              • Jan 2010
              • 20

              #7
              Hi again,

              I have been playing around a bit and found (hopefully) the code structure needed. I have set my green class up and it has worked perfect so far..

              Thanks,
              Rob

              Comment

              • markmcgookin
                Recognized Expert Contributor
                • Dec 2006
                • 648

                #8
                Rob,

                Great! Glad to hear it's working.

                Yeah your Course class could have an array or something like that, in which you could store all your holes.

                So in the app, if you want to get details about The 5th hole, just

                Code:
                MyCourse.Holes[4]; 
                //4 because the array will be zero indexesd, i.e. starting at 0 not 1
                Then you can pull info out about your hole. I assume that you will be dealing with one hole at a time, so your form could have a "Hole" variable, then just assign it to your courses hole

                Code:
                Hole myHole = new Hole();
                myHole = MyCourse.Holes[4];
                So your course class needs

                Code:
                public Hole[int index]
                {
                    return m_Holes[index];
                }
                to allow them to access the hole at the index you pass in from the m_Holes array

                Comment

                Working...