Jump to content
  • Advertisement
Sign in to follow this  
Winegums

Couple of C# questions

This topic is 4081 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, as stated in my thread yesterday I've picked up C#. Things are going OK though I have a few questions... 1. How do I include multiple files (that i've made)? #include (unsurprisingly) doesn't work, and i can't find anything on the internet about how to include other files, mabye it's considered a trivial point or maybe i'm rubbish at searching. 2. I've noticed that many tutorials have using System.WinForms; this, however, causes errors on my compiler (VS 2005) unless i change it to using System.Windows.Forms; the two seem to have slightly different syntaxes (the odd function takes slightly different parameters, such as the message box function). Is this something I should worry about or try to change or are the differences largley cosmetic?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
1. How do I include multiple files (that i've made)? #include (unsurprisingly) doesn't work, and i can't find anything on the internet about how to include other files, mabye it's considered a trivial point or maybe i'm rubbish at searching.

the keyword in C# is using.

I have no clue about the second. It's possible that they are referring to the C# 1.0/1 interface, but I've never used C# 1.0 or 1.1.

Share this post


Link to post
Share on other sites
Quote:
Original post by Winegums
1. How do I include multiple files (that i've made)?


Just add them into your project and your compiler should do the rest. C# is different here than with C/C++, it doesn't need text import functions and classes / functions don't have to be in declared order. Header files don't exist in C#, your other source files will be aware of each other. If your files use different namespaces though you'll have to use the using keyword or just tack on the namespace to everything.

Quote:
Original post by Winegums
2. I've noticed that many tutorials have

using System.WinForms;

using System.Windows.Forms;

Is this something I should worry about or try to change or are the differences largley cosmetic?


The error likely has to do with the rest of the code, not the using statement. The difference is mostly cosmetic.


using System.WinForms;
// later in class
Forms.Button SomeButton = new Forms.Button();

using System.WinForms.Forms;
// later in class
Button SomeButton = new Button();


I prefer the first version, the "Forms." makes it clear what library the class is from.

Share this post


Link to post
Share on other sites
ok got it working, but now i've hit another problem - pointers.

it seems c# doesn't like me having easy access to them like in c++

is there a reason for this? does c# have a native way of implimenting things like linked lists or do i have to work around it's dislike of pointers to get linked lists to work? i've looked around and found the 'unsafe' keyword, but if theres a less ominous sounding way of doing things i'd appreciate knowing.

Share this post


Link to post
Share on other sites
Quote:
1. How do I include multiple files (that i've made)? #include (unsurprisingly) doesn't work, and i can't find anything on the internet about how to include other files, mabye it's considered a trivial point or maybe i'm rubbish at searching.


I think I remember reading that such thing as #includes (and header guards) weren't used in C#. If I remember well, files containing required classes and namespace are located automatically by the compiler, as long as you use "using" directives.

I would also advice on using "System.Windows.Forms", that's the standard, I'm pretty sure.

I've actually never written a C# program. I've read a whole book on it when I was bored, though.

[EDIT] Beaten on it.

Share this post


Link to post
Share on other sites
C# linked lists can be found in System.Collections.Generic.LinkedList

In C# you don't generally use pointers - preferring the safer references.
But you shouldn't be writing your own linked list, just use the built in one.

Share this post


Link to post
Share on other sites
Please do not try to program in C# like you would with C++, it really is a completely different language.

Anyway the is a linked-list class in .Net, check out the System.Collections.Generic namespace. Although I think List is more efficent then LinkedList.

Also be aware that all your classes are already references / pointers.

SomeClass A = new SomeClass();
SomeClass B = A;

in C++ this would copy A over to B, however in C#, B now points to A(so anything done with A affects B).

Share this post


Link to post
Share on other sites
Quote:
Original post by Winegums
ok got it working, but now i've hit another problem - pointers.

it seems c# doesn't like me having easy access to them like in c++

is there a reason for this? does c# have a native way of implimenting things like linked lists or do i have to work around it's dislike of pointers to get linked lists to work? i've looked around and found the 'unsafe' keyword, but if theres a less ominous sounding way of doing things i'd appreciate knowing.


Strictly speaking C# can use pointers, but just about the only place they're useful is when you're dealing with arrays of large value types and perhaps in some limited cases native interop. For what you're wanting to do, you definitely don't want to, and in fact can't, use pointers (since you can't get a pointer to a reference type), because really the object reference is basically the same thing (but it's not).

You probably don't need to write your own linked list class, since the .NET framework already has container classes. But if you wanted to build your own linked list class, you could certainly do that by simply using references to the objects.

And yes, you have to use 'unsafe' to use pointers. I've written a lot of unsafe code in C#, and I've found that except for some very limited instances it's pretty much unnecessary. Having said that, I felt kind of dirty using it at first, but I think that's the purpose of the unsafe keyword to start out with.

Obviously, you can't write verifiable code that's compiled as unsafe.




Share this post


Link to post
Share on other sites
thanks for the replies, it seems learning C# will be more involving than i expected.

is there a good place to learn how to use List properly?
I found an implimentation of a list class on MSDN, but it seems to use templates and alltogether be pretty complicated for what was a fairly simple concept in C++.

Share this post


Link to post
Share on other sites
I could be wrong, but I'd say that they would use templates (don't they call them Generics in C#?) to teach the concepts better, or be more dynamic.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!