473,785 Members | 2,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing functions in a seperate codefile ?

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.
Nov 15 '05 #1
6 9770
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>.<sta tic 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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Jimmy" <Bu**@Burrrrp.c om> wrote in message
news:3m******** *************@n ews.easynews.co m...
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.

Nov 15 '05 #2
Jimmy <Bu**@Burrrrp.c om> wrote:
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.Metho dName(). Otherwise, you'll need to have an
instance of the other type, and call the methods on that instance.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
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("Pa ss Any values needed for the constructor
here");
db.UpdateChildR ows(rowsToUpdat e);

Hope this helps

Marco

"Jimmy" <Bu**@Burrrrp.c om> wrote in message
news:3m******** *************@n ews.easynews.co m...
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.

Nov 15 '05 #4
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.

--
John C. Bowman
Software Engineer
Thermo Electron Scientific Instruments Div.
<Remove this before reply> jo*********@the rmo.com

"Jimmy" <Bu**@Burrrrp.c om> wrote in message
news:3m******** *************@n ews.easynews.co m...
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.

Nov 15 '05 #5

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.Metho dName(). Otherwise, you'll need to have an
instance of the other type, and call the methods on that instance.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Excellent.

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

Thanks for the help.
Nov 15 '05 #6
Thanks alot everyone for the very rapid responses with great examples, step
by steps, and links. I do appreciate the help and the kindness.
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2322
by: The Plankmeister | last post by:
Is it possible to access a function in a php document through a URL? For instance: http://www.somesiteorother.com/test.php?a_function And then have something like this in test.php: <?php function a_function() {echo "Hello!";}
8
2352
by: [RaZoR] | last post by:
hello, main script creates IE window, put an array there and then calls a child script. chils script makes an array element equal to some object and returns control to main script. then main script doesn't see that object and its functions. why? here is the code I implemented: //in main script
3
1227
by: Jeff Gaynor | last post by:
Hi, I am a moonlighting Java programmer who needs to get some JNI written in C. Thanks to legacy considerations, it must be in C. I hope I can frame this question in a way that you all can understand. Now, some of the calls I make returns a pointers to an objects. The objects themselves are written in C++. I need to call methods on these objects. How is this done? I hope there is wither a really short answer I can be given, or that...
0
773
by: Rashad Rivera | last post by:
Hi Gang, My question is directed more towards the MS JSctip.NET dev team. I understand that you guys are planning to implement events in the newer versions of JS.NET, but is there a way to work around this problem now? I thought I could access a function in a HTA application by passing it into a JS.NET assembly. I learned of that it in exposed in the asm as an Object(AKA: System.__ComObject). But I am unable to cast it to a local...
3
7153
by: Anjali Lourda | last post by:
Hi, I have defined a function in global.asax file. Could somebody please tell me how i am supposed to call that function from the other files of the same project. Global.asax public function getName() as String dim name as string name = "Abc" return name end function
2
1791
by: shaun duke | last post by:
I have been researching this over the last two days without success. I have a number of ultility functions that I want to make available to all pages. The pages will all be using code behind so my plan is to create an assembly CommonFunctions.dll place it in the /bin folder for the application and import the namespace into the code behind where required. Here is an abstract of CommonFunctions.vb
0
941
by: Darrel | last post by:
This may be a dumb question, but... can an ASPX page access any classes or functions from a web project that it isn't explicitely a part of (ie, compiled with?) I'm building an app where I want end-users to have the ability to add aspx pages as they wish. These are basically templates for their site. I'd like them to be able to reference functions that I create. In the old days of ASP, I'd make an include file with all the functions...
6
1285
by: Ant | last post by:
I have the following code which works fine for running some tests defined within a module: def a_test(): print "Test A" def b_test(): print "Test B" if __name__ == "__main__":
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.