473,378 Members | 1,523 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,378 software developers and data experts.

noob.. no functions in C#?

Searching the "How Do I.." pages.. I don't see anything about
functions like classic C/C++. If I want to do even a one-time
operation, I _have_ to create a class? Seems weird to have to define
an "object" when I will never use it, just would be creating it to run
something one time.
Mar 17 '07 #1
8 1126
Glenn wrote:
Searching the "How Do I.." pages.. I don't see anything about
functions like classic C/C++. If I want to do even a one-time
operation, I _have_ to create a class? Seems weird to have to define
an "object" when I will never use it, just would be creating it to run
something one time.
You can define a class with a static method. Then you do not
need an object.

Arne
Mar 17 '07 #2
On Fri, 16 Mar 2007 22:15:53 -0400, Arne Vajhřj <ar**@vajhoej.dk>
wrote:
>You can define a class with a static method. Then you do not
need an object.
Even better, you can define a _static_ class to hold your static
methods! That way you cannot even create an object. :)

That's the closest thing C# has to traditional functions.
--
http://www.kynosarges.de
Mar 17 '07 #3
Glenn wrote:
Searching the "How Do I.." pages.. I don't see anything about
functions like classic C/C++. If I want to do even a one-time
operation, I _have_ to create a class? Seems weird to have to define
an "object" when I will never use it, just would be creating it to run
something one time.
You don't have to create another class to add a function. You already
have a class where you can put it.

As C# is object oriented, all code is in methods inside classes.
Therefore, the code from where you wish to call the function also is
inside a class.

Then, of course it depends on what the function does. You might want to
create a class for it, and for other similar functions.
Eventhough C++ has object orientation support, it's still a functional
programming language where the ground level is the main program, and you
can use objects from there. In C# there is no ground level like that,
instead the main program is just a method in a class, and the project
knows what method to use to start the program.

It's a bit getting used to, but it's really nice to work in a truly
object oriented language.

--
Göran Andersson
_____
http://www.guffa.com
Mar 17 '07 #4
On Mar 16, 6:58 pm, Glenn <bagsm...@frontiernet.netwrote:
Searching the "How Do I.." pages.. I don't see anything about
functions like classic C/C++. If I want to do even a one-time
operation, I _have_ to create a class? Seems weird to have to define
an "object" when I will never use it, just would be creating it to run
something one time.
Take a look at the Math class in the .NET Framework. Most of the
functions there will be familiar. They're static: they don't require
you to create an instance in order to use them. The class merely
provides a grouping mechanism.

However, I'll warn you: what you're likely experiencing is a problem
with the paradigm shift. When I moved from C to OOP I, too, thought,
"Is there no way to just make a simple function?" My mistaken
assumption was that I would often need to make simple functions, or,
once I figured out that I could create static methods, that I would
often have need of static methods that weren't associated with any
class. After all, I'd been programming that way for years. How could I
program without writing stand-alone functions?

The truth of it is that you need them far, far less than you think.
Once you get the hang of designing software for OOP, you'll discover
that what you once wrote as stand-alone functions really do belong in
objects. Until you've designed quite a few classes, you can't really
see how to organize things effectively in an OOP language. However, I
predict that once you get the hang of it, you'll almost never need to
write a function that isn't connected to anything else.

Mar 18 '07 #5
Eventhough C++ has object orientation support, it's still a functional
programming language
I believe you meant to say "procedural" instead of functional (just trying
to prevent any confusion :-))
Mar 18 '07 #6
Lebesgue wrote:
>Eventhough C++ has object orientation support, it's still a functional
programming language

I believe you meant to say "procedural" instead of functional (just trying
to prevent any confusion :-))
Yes, you are absolutely correct. Thanks.

--
Göran Andersson
_____
http://www.guffa.com
Mar 18 '07 #7
Thanks to everyone for your input. I wasn't able to see the code on
the Math class, however, with the static suggestion, I ended up
finding Static Classes and Static Class Members (C#). There's a good
example in there (go to it through Help/Search).

Thanks again to all for your help :)
Glenn

On Sat, 17 Mar 2007 01:58:12 GMT, Glenn <ba******@frontiernet.net>
wrote:
>Searching the "How Do I.." pages.. I don't see anything about
functions like classic C/C++. If I want to do even a one-time
operation, I _have_ to create a class? Seems weird to have to define
an "object" when I will never use it, just would be creating it to run
something one time.
Mar 18 '07 #8
On Mar 18, 7:34 am, Glenn <bagsm...@frontiernet.netwrote:
Thanks to everyone for your input. I wasn't able to see the code on
the Math class, however, with the static suggestion, I ended up
finding Static Classes and Static Class Members (C#). There's a good
example in there (go to it through Help/Search).

Thanks again to all for your help :)
Glenn

On Sat, 17 Mar 2007 01:58:12 GMT, Glenn <bagsm...@frontiernet.net>
wrote:
Searching the "How Do I.." pages.. I don't see anything about
functions like classic C/C++. If I want to do even a one-time
operation, I _have_ to create a class? Seems weird to have to define
an "object" when I will never use it, just would be creating it to run
something one time.
Read about the Math class here:

http://msdn2.microsoft.com/en-us/lib...stem.math.aspx
Mar 19 '07 #9

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

Similar topics

2
by: spawncamper | last post by:
Hi - I posted here a few weeks back and people were kind enough to help. In lieu of my PL/SQL class I take in a few weeks, I was hoping to get some more advice. Here's my scenario: I need a...
3
by: We need more power captain | last post by:
Hi, I know less than a noob, I've been asked to do some compiles in VC++ 6 without knowing too much at all. (I'm a COBOL program normally so this is all too much for me) I open VC++6, open...
8
by: Rich Grise | last post by:
I think I've finally found a tutorial that can get me started: http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html and I've been lurking for awhile as well. What happened is,...
42
by: Holger | last post by:
Hi guys Tried searching for a solution to this, but the error message is so generic, that I could not get any meaningfull results. Anyways - errormessage:...
7
by: DJP | last post by:
Hi, I had sort of a noob question on memory allocation for strings. char *str1 = "Hello World!"; char str2 = "Hello World!"; In the above bit are both str1 & str2 stack allocated or heap...
4
by: Computer Guy | last post by:
Hi I have recently started working with PHP and am making a neighborhood website. I am connecting to my Mysql database and am having difficulty understanding what is happening in the example on...
6
by: Lang Murphy | last post by:
I'm baaaaack... some of you answered a question I had last week. Only problem is: I'm a dope who doesn't understand most of what y'all posted. Raw noob when it comes to .Net and C#. So I'm going...
4
by: larry | last post by:
Ok I'm a Python noob, been doing OK so far, working on a data conversion program and want to create some character image files from an 8-bit ROM file. Creating the image I've got down, I open...
4
by: .nLL | last post by:
Hi, im am a classic asp developer and started to learn asp.net but got stuck with a simple problem even before i step in to further. to learn i have started from a simple project (a login system...
1
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: 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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.