Jump to content

Very simple question about references

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
Shinka

Shinka

    Newbie

  • Members
  • Pip
  • 7 posts
If I have two C-Sharp files;

X
aFolder/Y

I want my file X to use methods from Y, do I really need to create a project or I there is a way to say to the file X to look for Y ? I know how to create a project and add Y to the project, but I would like to keep as few files as possible. In Java it's pretty simple I would use the import keyword but in C# I don't know how.

#2
th3cl41

th3cl41

    Newbie

  • Members
  • Pip
  • 7 posts

Shinka said:

If I have two C-Sharp files;

X
aFolder/Y

I want my file X to use methods from Y, do I really need to create a project or I there is a way to say to the file X to look for Y ? I know how to create a project and add Y to the project, but I would like to keep as few files as possible. In Java it's pretty simple I would use the import keyword but in C# I don't know how.

in c# ,you can using "using" keyword like "import" keyword in java to use object / method from the other class, add dll before

#3
Shinka

Shinka

    Newbie

  • Members
  • Pip
  • 7 posts
I know about using, but it seems only to create aliases, for example if I have the files;

a.cs
and b.cs in the folder b

Even if I add "using b;" in a.cs, it won't allow me to use methods from b...

There must be a way, I can't believe you have to create "projects" each time.

#4
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
What files from a project don't you want to include? So long as you have a main method as an entry point into the application and all the proper assembly info you should be able to write your own application without creating a project, but you will end up coding a bunch of stuff that is automatically done when you create a project. You will also need to manually add all the usuall references. Not sure why you would want to do this except for practice? If its size your worried about you could streamline you app by adding the assembly info to your main coding file but this really doesn't save many rescources.

#5
Shinka

Shinka

    Newbie

  • Members
  • Pip
  • 7 posts
I don't want any files except my two cs files.

It's simple with the import keyword from java, how can I do it with C# ?

#6
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
All of the assembly info and reference calls are necessary for the code to work properly (most of it anyhow; not all the assembly info is really needed). You can put all the code into just two files but your not really saving much space. It is usually considered good practice to break it into more files to make them easier to read; thats why your visual studio does this for you. If you really want it all in just two files its mostly just cut and paste work except for a couple of things. Once you do this the Visual Studio will no longer add references for you so you will need to add these to the namespase.properties section of your code by hand. This will entail more work for most every part of your application and like I said before, really doesn't save much space.

#7
Ryan Marfone

Ryan Marfone

    Newbie

  • Members
  • PipPip
  • 11 posts
The simple solution for you would to be create a new project and add both class files to the project. I would refactor both files to use the same namespace. This will generally make your code easier to read.

#8
Termana

Termana

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,057 posts
are you compiling from the command line? if so can't you just do something like "csc a.cs b.cs" plus the other parts of the command. also the using keyword works on namespaces - not filenames.
Posted via CodeCall Mobile

#9
Shinka

Shinka

    Newbie

  • Members
  • Pip
  • 7 posts
So the answer is no, you can't create just a few files by hand as in Java, C, C#, you have to create a project to use classes in different files... That's annoying, for me at least.

Thanks for the answers guys.

#10
rhythm

rhythm

    Newbie

  • Members
  • Pip
  • 2 posts
i think what you're looking for is namespaces - if the two classes are declared in the same namespace, they will be able to see each other. the using keyword imports items from a different namespace.

namespaces in c# are akin to packages in java. the difference is that in c#, they are independent of folder structure. try declaring both classes (can be in different files) inside the same namespace.

so both classes will be inside their own block like:
namespace ab
{
class a{}
}.