9 Replies - 560 Views - Last Post: 30 December 2011 - 08:36 AM Rate Topic: -----

#1 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

"Saving" Functions?

Posted 29 December 2011 - 09:17 PM

Okay, so I'm more used to C++ programming and haven't spent a whole lot of time in C#. There's one thing that I haven't been able to figure out though based on C++ knowledge, and that is how to store functions. I use C# Visual Studios, and on C++ it's just on header files, how do you do it in C#? Create a function that can be used in other programs. I've looked all over the web for this but couldn't figure it out. Can someone help me out? It would help a lot and speed up my coding. Thanks!

:stupid:

Is This A Good Question/Topic? 0
  • +

Replies To: "Saving" Functions?

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: "Saving" Functions?

Posted 29 December 2011 - 10:26 PM

I guess I'm unsure what you mean. In C#, functions belong to something, either a class or a struct. They don't exist on their own. If you mean you want a class that contains functions to be exposed to other programs, you'd create a Class Library project. When built, that'll create a DLL that other .NET programs can reference. Make sure that the class and the functions are public, otherwise they won't be available.
Was This Post Helpful? 1
  • +
  • -

#3 cilaes  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 75
  • Joined: 12-December 11

Re: "Saving" Functions?

Posted 30 December 2011 - 06:05 AM

project > new class
Was This Post Helpful? -1
  • +
  • -

#4 Amatore  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 47
  • Joined: 27-December 11

Re: "Saving" Functions?

Posted 30 December 2011 - 06:13 AM

Start out with

File > New Project > Select Visual C# on the Left

Attached File  untitled.bmp (1.74MB)
Number of downloads: 22

After creating this file you can declare your class and then in the .NET app you want to use this class you right click in the Solution explorer and select "Add Reference..." and projects should be a choice there.
Was This Post Helpful? 0
  • +
  • -

#5 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: "Saving" Functions?

Posted 30 December 2011 - 07:38 AM

Okay, so just create a new class and add it as a reference whenever I need it, okay, thanks!
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: "Saving" Functions?

Posted 30 December 2011 - 07:44 AM

No, no, you're mixing up solutions to different problems there.

First, clarify what you want to do. Do you want to create a library for other programs to use, or do you want to put things in one file and access it from another, in the same program?
Was This Post Helpful? 0
  • +
  • -

#7 Amatore  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 47
  • Joined: 27-December 11

Re: "Saving" Functions?

Posted 30 December 2011 - 07:51 AM

ah, well my apologies for jumping to creating a class library. another way to add a class is to right click on your project and select "add" > "add Class" should be bottom most option.
Was This Post Helpful? 0
  • +
  • -

#8 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Re: "Saving" Functions?

Posted 30 December 2011 - 07:59 AM

If you want only one project to use it, then just add a class and write the function into it. Then use it in your project.

If you want multiple projects to use it, then create a class library (.dll) and add references to every project you want to use it in.
Was This Post Helpful? 0
  • +
  • -

#9 Wuzseen  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 72
  • Joined: 04-October 11

Re: "Saving" Functions?

Posted 30 December 2011 - 08:34 AM

I had a similar question when I started C#--and I think this is what you're really asking as well; you need to understand namespaces. There aren't really header files per se, nor do you import files as you would in, say, python. Instead, in the same project, if a function belongs to a class in the same namespace you have access to that function. This means if in your first .cs file, containing your main function, with namespace "blah" you want to access functions from another .cs file, in the other .cs file you need to
A. make sure it's in the same project
B. make sure it's in the same namespace.
IE declare it sorta like this:


namespace blah
{
     class functions
     {
        function test();
     }
}



So long as it's in the same project as your first .cs file, you can now use that test function.

It's a bit confusing, but is way less upkeep. The using directive you see in C# files is for including namespaces in assemblies (.dlls) the project references. This relates to making the class function library discussed above. Using also references namespaces declared in the same project, you could have namespace blah and namespace derp; accessing one in the other would be a matter of putting using blah/derp in the code.

What I detailed to you is the closest, in my opinion, to a C++ header file in C#. But you have to understand that, conceptually, headers don't really exist in C# in the same capacity they do in C++.

EDIT: As per Curtis Rutland's response below, you're most definitely correct--I edited my post to reflect that; thanks for the reminder!

This post has been edited by Wuzseen: 30 December 2011 - 08:39 AM

Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: "Saving" Functions?

Posted 30 December 2011 - 08:36 AM

The using directives are also for different namespaces in the same project, if you have multiple ones.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1