Accessing functions in a seperate codefile ?

  • Thread starter Thread starter Jimmy
  • Start date Start date
J

Jimmy

Hi,

Sorry for the complete newb question, but none of the 3 books I own on C#
cover this.

If I place say, 10 or 15 functions in a codefile, what's the best method to
call these functions from say Form1.cs ?

thanks for all the help, I'm really trying here.
 
Jimmy,

It depends on where you put them in a file. All of the functions that
you attached are part of a class. If they are static, then you can call the
method using the following code:

// Call a static method.
<typename>.<static method name>(<optional parameters>);

If the methods are instance methods, then you have to create an instance
of the type and then pass it to an instance of Form1 and then have Form1
access the methods internally.

Hope this helps.
 
Jimmy said:
Sorry for the complete newb question, but none of the 3 books I own on C#
cover this.

If I place say, 10 or 15 functions in a codefile, what's the best method to
call these functions from say Form1.cs ?

thanks for all the help, I'm really trying here.

You need to think in object-oriented terms. If the methods are static,
you just use ClassName.MethodName(). Otherwise, you'll need to have an
instance of the other type, and call the methods on that instance.
 
One way would be to create a class that holds your methods in another file
and to create an object of that class when you need to use the methods. For
example, if you have many methods that access a database you could do
something like:

//create a clsDataBase.cs file with all your database methods, then use it
clsDataBase db = new clsDataBase("Pass Any values needed for the constructor
here");
db.UpdateChildRows(rowsToUpdate);

Hope this helps

Marco
 
Jimmy,

All functions must be members of a class, so you can either create a class
and make the functions members of it, then instantiate the class into an
object and call them, or move them into a Dll, annd essentially do the same
thing. Below is a way to move them into a Dll.

1) Try using: New->Project & choose Class library. This will create a Dll
assembly project. Put your functions in there as members of a class (or
classes, which ever makes the most organization sense).

2) I'm not certain "best way" can be answered directly, because the next
step depends upon the answer to the following question: Do you need to
dynamically load the Dll at run time, or can you statically access it?

I'll deal with the static approach, as it's easier, and should get you
started:
3) In your client app, set a reference to the Dll project.
4) Add an appropriate "using" statement at the top of your code file that
will be calling the Dll function(s) to include the namespace you set up in
the Dll. This assumes the namespace you created in the Dll is different from
the client app's namespace. If they share the same namespace, you won't need
this step.
5) Instantiate the object(s) you put into the dll and call the method(s) on
the object(s) in the client app. You don't have to do anything special
besides setting the reference and including the namespace.

If you need to dynamically load the Dlll and then retrieve the method(s) of
interest, this is more complicated and can become rather long winded. I'll
take a short cut on this one and recommend you look up"Loading Assemblies
into an Application Domain", "MethodInfo class" and then "Reflection" for
starters in MSDN. Those aught to point you in the right direction.

Hope this helps & good luck.
 
Jon Skeet said:
You need to think in object-oriented terms. If the methods are static,
you just use ClassName.MethodName(). Otherwise, you'll need to have an
instance of the other type, and call the methods on that instance.

Excellent.

Thank you both, making the function static works in this situation, and
cured the problem.

Thanks for the help.
 
Thanks alot everyone for the very rapid responses with great examples, step
by steps, and links. I do appreciate the help and the kindness.
 
Back
Top