473,320 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Beginner Confusion - C# Terminology - Simple Function as Example

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
4 2633
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
9
by: vidalsasoon | last post by:
Ok, this is the structure of my classes. " class Global " | " ------------------------------------------- " | ...
13
by: sathyashrayan | last post by:
Dear group, pls go through the following function definition: function at_show_aux(parent, child) { var p = document.getElementById(parent); var c = document.getElementById(child); var top ...
2
by: Glen | last post by:
Hello, I've written a script in python and put together a simple QFrame with a QTextBrowser with Designer. I've translated the C++ into python using puic4. The .py file is called outputWin.py. ...
331
by: Xah Lee | last post by:
http://xahlee.org/emacs/modernization.html ] The Modernization of Emacs ---------------------------------------- THE PROBLEM Emacs is a great editor. It is perhaps the most powerful and...
21
by: globalrev | last post by:
i have a rough understanding of lambda but so far only have found use for it once(in tkinter when passing lambda as an argument i could circumvent some tricky stuff). what is the point of the...
97
by: xahlee | last post by:
I'd like to introduce a blog post by Stephen Wolfram, on the design process of Mathematica. In particular, he touches on the importance of naming of functions. • Ten Thousand Hours of Design...
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.