C# - my first stupid question
-
All the exmaples I see are making one fat .cs file - what is the equivelant of #include, so I can see the contents of one file from another ? Thanks Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002
-
All the exmaples I see are making one fat .cs file - what is the equivelant of #include, so I can see the contents of one file from another ? Thanks Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002
You don't. C# -- like Java -- doesn't look for dependencies until the linking stage (if you could call it that). main.cs
using System;
public class Test
{
public Test() { }public static void Main(string [] args) {
Foo f = new Foo();
f.SayHello();
}
}foo.cs
using System;
public class Foo
{
public Foo() { }
public void SayHello() {
System.Console.WriteLine("Hello Chrstian!");
}
}That will work just fine, assuming both files are in the compiling process. James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978
-
You don't. C# -- like Java -- doesn't look for dependencies until the linking stage (if you could call it that). main.cs
using System;
public class Test
{
public Test() { }public static void Main(string [] args) {
Foo f = new Foo();
f.SayHello();
}
}foo.cs
using System;
public class Foo
{
public Foo() { }
public void SayHello() {
System.Console.WriteLine("Hello Chrstian!");
}
}That will work just fine, assuming both files are in the compiling process. James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978
Why might it not work ? I did this last night and still got nowhere. If I create a file and save it, do I need to add it to the project somewhere as well ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002
-
Christian Graus wrote: If I create a file and save it, do I need to add it to the project somewhere as well ? Yes, it will have to be added to the project or it won't get compiled. How did you go about creating a file and saving it? If you go through the IDE it should be added to the project for you. How I add a new class: Right click my project in the Solution Explorer, choose Add then Add New xxxxx (class, component, windows form, etc). Ah! Just a shot in the dark, do the namespaces match between your two files? Come to think of it, my example didn't have a namespace for foo.cs :-O If you change the namespace you can let the IDE help you a little, right click on the project and choose Properties, then change the Default Namespace to the base namespace you use. HTH, James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978
No, I didn't do it quite like this, but I did it through the IDE. I made sure my namespaces matched, but I'll try it fllowing what you said and see how it goes. Thanks. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002
-
Why might it not work ? I did this last night and still got nowhere. If I create a file and save it, do I need to add it to the project somewhere as well ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002
Christian Graus wrote: If I create a file and save it, do I need to add it to the project somewhere as well ? Yes, it will have to be added to the project or it won't get compiled. How did you go about creating a file and saving it? If you go through the IDE it should be added to the project for you. How I add a new class: Right click my project in the Solution Explorer, choose Add then Add New xxxxx (class, component, windows form, etc). Ah! Just a shot in the dark, do the namespaces match between your two files? Come to think of it, my example didn't have a namespace for foo.cs :-O If you change the namespace you can let the IDE help you a little, right click on the project and choose Properties, then change the Default Namespace to the base namespace you use. HTH, James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978
-
All the exmaples I see are making one fat .cs file - what is the equivelant of #include, so I can see the contents of one file from another ? Thanks Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002
If you're compiling as part of the same project, you don't need #include; the compiler can automatically access the classes in all the files that are compiled together. If you're compiling into separate assemblies (aka dlls), you'll need to add the referenced one either to the project or using /r on the command line.
-
If you're compiling as part of the same project, you don't need #include; the compiler can automatically access the classes in all the files that are compiled together. If you're compiling into separate assemblies (aka dlls), you'll need to add the referenced one either to the project or using /r on the command line.
Thanks - it seems the way I added the file did not make it part of the proejct, I did the exact same thing this morning, but added the class by right clicking in the class view and it all worked fine. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002