Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

struct / arrays / location? 2

Status
Not open for further replies.

Acidon

Programmer
May 1, 2004
21
Hello C# Gurus, I need a piece of advice.

I am creating an editor for my little game, which I am creating while I learn the C# language. For example, for the Monster Editor, I am creating a struct full of arrays to hold the monster data for all monster records.

My question: Where should I define this struct? In VB, I defined them in their own .bas file. It was nice and clean. Here, I am a bit lost. Can I define all of these structs in a separate location/file? If so, please tell me what / where that should be.

I know that it's obvious that I am a newb here. Just looking for some insight. I appreciate any responses.

Also, I have no C# book at home. I have over 70 books on C, C++, Perl, etc. but no C#. Could I get you to list me some quality C# resources online? I mean somewhere I can go to quickly see the syntax for if statements, loops, etc.

Thank you in advance. Have a great day.

Acidon
 
Sounds to me like all you need is a define this struct as a .cs file in your project namespace. That way it will be available throughout. I tend to use a .cs file for every class, struct and enum in my project, sometimes it makes sense to include more than one in the same file (especially when using private classes or multiple 'releated' enums).

As for C# guides. For a language reference MSDN is the best place...


Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
Any reason to use a struct over a class?

If you use a class you can inherit from a base class that has properties that are in common with all monsters.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Thank you very much for your responses. I believe I'm on the right track now.

Acidon
 
Also - in C++ it is worth noting that a class and a struct are **EXACTLY** the same aside from the default access level (private for class, public for struct) - you can inherit from a struct and so on.

In C# - a class is always a reference value and a struct is usually a value type.

This has huge consequences when using PInvoke and also for remoting.

M.


Hollingside Technologies, Making Technology work for you.
 
I only had 1 semester of C++ in school while I was there, unfortunately. I am having a bit of trouble, maybe I could get 1 more answer here that I haven't been able to find elsewhere.. I am using Visual Studio.NET, coding this in C# btw.

Where would i create an instance of my struct if I wanted it available anywhere in the program? Or at least available anywhere in one particuliar code file.

I am reading the data from file into the instance I created without a problem. Unfortunately I can't then use the instance to write the information back to disk within a different method - because the different method does not see the instance.

I hope that I am explaining this right.. I just want to be able to access the instance from within any method concerning this part of the program.


If you need more information please let me know. I'm hoping that this is a general enough question that I don't have to post my sad code. :)

Thanks for your help guys, really appreciate the responses. I'll be buying a book or 2 in a week or so, then I won't be buggin you guys with these newbie questions.

Acidon
 
Acidon,
I'm not quite sure I fully understand your problem but I'll try to point you in the right direction.

Acidon said:
Unfortunately I can't then use the instance to write the information back to disk within a different method - because the different method does not see the instance.
Try to have this different method and your struct or class in the same namespace. If you don't want that to be the case, then simply add a "using xxx" statement at the beginning of your file to access the namespace where your struct or class is.

The most natural place to put that struct is in the file you'll be using it. If you'll be using different files, then the other files where you will be using it should simply include a "using" statement to enable you to access the struct from different namespaces. Note that a "different file" does not mean "different namespace" as it is possible to have a namespace scattered in different files.

I hope I was clear enough.

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 

JCrux063,

Thank you very much. That was exactly what I was looking for. Sometimes the obvious stares me in the face and I just don't see it until it's explained the right way for me, be it in a book or from someone else.

Well, it's finally clicked now. Should be smooth sailing now (at least for a couple hours, hehe). Thanks again! Have a great day.

Acidon
 
Oops.. :(

I meant to let you all know that every response I received in this thread gave me some insight and helped point in the right direction. This may not need to be said, but I didn't want anyone thinking that I didn't benefit from or appreciate all of the responses I received. You are all great and I really like this community. Maybe someday, far in the future, I can help others in the same fashion. Have a great day.

Acidon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top