By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,016 Members | 2,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,016 IT Pros & Developers. It's quick & easy.

Beginner Confusion - C# Terminology - Simple Function as Example

P: n/a
Sorry for the simple question but thanks in advance:

My goal is to create reusale code for a web app in C#.
I have written the code already as a windows app but here is where I am
confused:

To keep it really easy let's say this is the code:

function addition(int, int);
int X;
int Y;
int Z = x + y;

return Z;

I want to be able to have this "program" as "compartmentalized" code so
I code reuse it whenever I need....

Do I:
1. use this as a function?

2. and does the function need to be in a class?

3. how would I "save" this code in some way to use in Application1 or
Application2. Does it get called as a Class or a Function?

4. Do I need to create a Class Library or .DLL? and how does this
relate to namespaces?

5. If we then go to ASP.NET, how would I implement it as reusable for
the web? I could create the form and then use the onSubmit but do I
call the function the class or the .dll?

6. And if I wanted to use it in more than one website, is there a way
to bundle the function as well as the HTML needed to execute it?

////////
Header and encoding info.......
<p>This is the addition sample.
</p>
<form name="form1" method="post" action="">
<p>
<input type="text" name="textfield">
Enter X</p>
<p>
<input type="text" name="textfield2">
Enter Y</p>
<p>
<input type="submit" name="Submit" value="Add">
(plus the function or class, .dll?)</p>
<p>
<input type="text" name="textfield3">
This is returned Z</p>
</form>
//////////////////

Thanks SO much. I REALLY appreciate any and all help!
-Ranginald

Apr 7 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In C# every method must be inside a class or structure.
You can't have a method (function) in a file and then use it.

Class library compiles to a .NET DLL

So if you would like to use one class in multiple projects then make a
class library project put your code there compile the code and then
refrence the dll from other projects. That way you can use your class
like any other class that you have written (or is included with .NET)

It seems that you are confused as you don't quite distinguish a method
from a class and class from dll.
Maybe you should read some beginner books before you start working on
some projects?
lp
Jan Hancic
http://cwizo.blogspot.com

Apr 7 '06 #2

P: n/a
Hello,

In C#, there is no such thing as "standalone functions" All "functions" must
be members of a class.
So the simplest version of your code would be as follows:

public class Math
{
public static int Addition(int x, int y)
{
return x + y;
}
}

You can compile this class as part of dll, using class library project, and
use it from any other project.
Your confusion between classes and functions scares me a little bit. Class
is a pattern describing instance behavior (methods) and data (fields), while
function is, well, everyone knows what a function is (I hope so)
Namespaces are there just to separate logical parts of code, said freely, so
dlls are in no way related to namespaces.
You can reference dlls made as class libraries from asp.net and finally,
yes, there is a way to deploy (bundle) the web site code along with
referenced assemblies.

"Ranginald" <da*******@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
Sorry for the simple question but thanks in advance:

My goal is to create reusale code for a web app in C#.
I have written the code already as a windows app but here is where I am
confused:

To keep it really easy let's say this is the code:

function addition(int, int);
int X;
int Y;
int Z = x + y;

return Z;

I want to be able to have this "program" as "compartmentalized" code so
I code reuse it whenever I need....

Do I:
1. use this as a function?

2. and does the function need to be in a class?

3. how would I "save" this code in some way to use in Application1 or
Application2. Does it get called as a Class or a Function?

4. Do I need to create a Class Library or .DLL? and how does this
relate to namespaces?

5. If we then go to ASP.NET, how would I implement it as reusable for
the web? I could create the form and then use the onSubmit but do I
call the function the class or the .dll?

6. And if I wanted to use it in more than one website, is there a way
to bundle the function as well as the HTML needed to execute it?

////////
Header and encoding info.......
<p>This is the addition sample.
</p>
<form name="form1" method="post" action="">
<p>
<input type="text" name="textfield">
Enter X</p>
<p>
<input type="text" name="textfield2">
Enter Y</p>
<p>
<input type="submit" name="Submit" value="Add">
(plus the function or class, .dll?)</p>
<p>
<input type="text" name="textfield3">
This is returned Z</p>
</form>
//////////////////

Thanks SO much. I REALLY appreciate any and all help!
-Ranginald

Apr 7 '06 #3

P: n/a
Thanks so much.
I tried it and it worked.
I was able to create a class library containing the Math Class and the
Addition Method, compile it as a .dll and then reference it in another
project.

This may be way ahead but is an ASP.NET server control similar in
concept to the delployed code and assemblies you mentioned? If this is
way too big a question to explain I understand.

Thanks again.

Apr 8 '06 #4

P: n/a
Yes, everything in .net is a class(object) in .dll(assembly).
"Ranginald" <da*******@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
Thanks so much.
I tried it and it worked.
I was able to create a class library containing the Math Class and the
Addition Method, compile it as a .dll and then reference it in another
project.

This may be way ahead but is an ASP.NET server control similar in
concept to the delployed code and assemblies you mentioned? If this is
way too big a question to explain I understand.

Thanks again.

Apr 8 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.