473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Should I use a STRUCT?

20
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
Jan 17 '10 #1
7 1795
markmcgookin
648 Expert 512MB
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.
Jan 17 '10 #2
rb0135
20
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
Jan 18 '10 #3
markmcgookin
648 Expert 512MB
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.

:)
Jan 18 '10 #4
rb0135
20
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
Jan 18 '10 #5
rb0135
20
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:

Expand|Select|Wrap|Line Numbers
  1. namespace xyz
  2. {
  3.     public class Course
  4.     {
  5.          object[] hole = new object[5];
  6.  
  7.          hole[0] = ....;
  8.         etc.. etc..
  9.     }
  10. }
  11.  
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
Jan 19 '10 #6
rb0135
20
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
Jan 19 '10 #7
markmcgookin
648 Expert 512MB
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

Expand|Select|Wrap|Line Numbers
  1. MyCourse.Holes[4]; 
  2. //4 because the array will be zero indexesd, i.e. starting at 0 not 1
  3.  
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

Expand|Select|Wrap|Line Numbers
  1. Hole myHole = new Hole();
  2. myHole = MyCourse.Holes[4];
  3.  
So your course class needs

Expand|Select|Wrap|Line Numbers
  1. public Hole[int index]
  2. {
  3.     return m_Holes[index];
  4. }
  5.  
to allow them to access the hole at the index you pass in from the m_Holes array
Jan 19 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
0
by: tmartsum | last post by:
I have a discussion in comp.std.c++ After a (bit stupid) suggestion on representing a fixed 'big' length int I moderated it to "Bitfields-ints should be allowed to have any fixed length" I...
64
by: Morgan Cheng | last post by:
Hi All, I was taught that argument valuse is not supposed to be changed in function body. Say, below code is not good. void foo1(int x) { x ++; printf("x+1 = %d\n", x); } It should be...
0
by: Chua Wen Ching | last post by:
Hi there, I am wondering when i had a class that extends an interface (currently in my interface i only had methods), but should i also extend Struct, Enum or even P/Invoke methods? If it...
46
by: clintonG | last post by:
Documentation tells me how but not when, why or where... <%= Clinton Gallagher http://msdn2.microsoft.com/en-us/library/saxz13w4(VS.80).aspx
6
by: Howard Gardner | last post by:
/* As it sits, this program won't compile for me. It will compile using either of the versions of tpt that I've commented out. Which versions SHOULD compile? I think that the code is...
28
by: steve yee | last post by:
i think c should adapt c++ template standard, as well as namespace. if so, c can replace c++ in many cases.
33
by: mscava | last post by:
Well I've got a problem, that is more theoretical than practital. I need to know benefits of RTTI. I see another way of doing it... class A { public: ~virtual A() {} enum Type { X, Y, Z }; ...
4
by: ianbrn | last post by:
Hello, I have a struct defined thus: typedef struct myStruct { int j; } myStruct; I saw somewhere calls with the following syntax: f1(&*a)
10
by: MisterE | last post by:
typedef struct sg { int a; } G; int c(G* g) { return g->a; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.