Question How to Make namespace and put methods in?

Ali P S

Member
Joined
Jul 26, 2014
Messages
5
e.g.

for motion related methods:

Button1.Left = MyMethods.Motions.SlowToFastToSlow(btnX, 0, 100, 8);

And ...

How to create them? Can i put methods source inside project?
 
Done ...
1.Add a class to project:

C#:
namespace NameSpace
{
 class Class
 {
  int F(int X)
  {
   return X ^ 2;
  }
 }
}

In Form.cs:

C#:
using System;
using System.Collections.Generic;
.
.
.(etc.)
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
 {
  NameSpace.Class obj = new NameSpace.Class();
  int X = 1;
  int Y;

  public Form1()
  {
   InitializeComponent();
  }

  private void timer1_Tick(object sender, EventArgs e)
  {
   Y = obj.F(X);
  }
 }
}
 
Last edited:
Normally you would import the namespace, just as you have done for System and System.Collections.Generic, and then use the type name unqualified.

Yes, but when we insert NameSpace, we can access it by typing:
C#:
using NameSpace;
Class obj = new Class();
Without "NameSpace."

And ... Also without typing "using NameSpace" we should type "NameSpace." To access...
C#:
(without "using"):
NameSpace.Class obj = new NameSpace.Class();
 
Yes, but when we insert NameSpace, we can access it by typing:
C#:
using NameSpace;
Class obj = new Class();
Without "NameSpace."
Yeah, that's what I said.
And ... Also without typing "using NameSpace" we should type "NameSpace." To access...
C#:
(without "using"):
NameSpace.Class obj = new NameSpace.Class();
Absolutely but, while you don't ever have to import any namespace, it's usual to import a namespace if you would use it in code more than once. It just makes your code easier to read. The only time you genuinely must qualify a type is when there is a name clash and you need to qualify which of more than one type with the same name you mean.
 
Back
Top Bottom