?
Solved

Simple example on namespaces as a contianer for simple functions

Posted on 2004-11-04
3
Medium Priority
?
226 Views
Last Modified: 2010-04-15
Hi.

  I am new to the C# programming world but have been doing Delphi for over 6 years so I am not new to programming in general.  I am trying to get a handle on namespace and how to use them.  If I were creating an object that my application is to use, I think I could do this with the knowledge that I have already.  What I am having problems with is trying to move some of my utilies to C#.  Here is an example of what I am trying to do.

using System;

namespace MyLib
{
      namespace MyStrUtils
           {

//  think that I am suppost to have a class define here

                public char FirstCharInStr(string S);
                           {

                            }

            }

      namespace MyNumUtils
           {
//  think that I am suppost to have a class define here

                public int WhichIntIsLarger(int a; int b);
                           {

                            }

            }
}        


Now please do not get hung up that C# most likely has method for doing the things that I have shown here (**THIS IS ONLY TO SHOW A SIMPLE EXAMPLE OF WHAT IT IS THAT I AM TRYING TO DO**).  So as I mention in the example, I think that I am suppost to create a class and if this is true what would be a valid class name here.

 Finally question is how would I use it in a project ie:

using MyLib.MyNumUtils;


///  code
int c = (WhichIntIsLarger(10, 14));

Hope this make sense.  Thank you for your help with this.

James
0
Comment
Question by:thumper631
  • 2
3 Comments
 
LVL 6

Expert Comment

by:ryerras
ID: 12496560
Just the way you showed, by the way, we use comma to separate the arguments of a method, not semi colon. I am sure you are aware of it
0
 
LVL 6

Expert Comment

by:ryerras
ID: 12496590
By the way, if the utility is a dll, then you should add the dll to by Add Reference. Otherwise it wouldnt compile in VS.NET, if you are planning to compile in command, you should pass the referenced dll as the compile argument.
0
 
LVL 8

Accepted Solution

by:
Razzie_ earned 1000 total points
ID: 12499296
Well you cannot declare methods directly in a namespace, so you should indeed create a class. For example:

namespace MyLib
{
   namespace MyStrUtils
   {
      public class StringManipulator
      {
           public static char FirstCharInStr(string S);
           { // .... //  }
      }
   }
}

'The name 'static' means that you don't have to create an instance of StringManipulator, so you could actually use it in another project using:

Char c = MyLib.MyStrUtils.StringManipulator.FirstCharInStr("SomeString");

or if you'd use 'using MyLib.MyStrUtils;' in your code, simply:

Char c = StringManipulator.FirstCharInStr("SomeString");


HTH,

Razzie



             
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question