Author Topic: [VB.NET][C][C++][Tutorial] Using Namespaces  (Read 2068 times)

0 Members and 1 Guest are viewing this topic.

Offline Huntondoom

  • Baron
  • ****
  • Posts: 856
  • Cookies: 17
  • Visual C# programmer
    • View Profile
[VB.NET][C][C++][Tutorial] Using Namespaces
« on: December 19, 2010, 06:31:59 pm »
Using Namespaces
by Huntondoom

intro:
Since I didn't know C#, but this subject would be handy for Vb.net as well as C#,
so I learned how to do this in VB.net and C#, so more people can understand it
and use it
(if you see a mistake, please say pm me about it, because if you post it, and I correct it, then you Post will become useless)

Content:
0x00 - What is Namespaces?
0x01 - Getting Started
0x02 - How to use Namespaces?



0x00 - What is Namespaces?

Every code should have done it sometime, but in a different way
you made a sub, and you want a function in it but you decide to call it,
and write the function beneath the sub:
Code: (vb) [Select]
Sub Main()
'do stuff
Getstuff(string, Integer)
end sub
Function Getstuff(ByVal string as string, ByVal Integer as integer)
'Getstuff
end Function
Code: (C#) [Select]
Static void Main()
{
//do Stuff
Getstuff(string, int)
}
static void Getstuff(string string, int integer)
{
//do Stuff
}
Namespace works with that kind of principle,
but can give more options, and may be more detailed then what some people usually do
this method is also used to build Class Library (.dll)


0x01 - Getting Started

if you're going to use it, then make sure that the namespaces will be placed beneath you're code that will be placed in your program, since Visual Studio, usually say that it can't rebuild the form of my application cause it uses something that is found also in Namespaces

0x02 - How to use Namespaces

Using and making Namespaces isn't that difficult.
so I have made an example, I made some Namespaces that would be used in a program that would check the Evilzone site (in theory)

so lets start, first we begin with making a namespace called Evilzone:
Code: (vb) [Select]
Namespace Evilzone

End Namespace
Code: (C#) [Select]
Namespace Evilzone{

}

now I entered some Subjects you can choice through the Namespace Evilzone
the subjects you eventually will be able to choice:
  • Home
  • News
  • Forum
  • IRC
  • Contact
  • AboutUs
  • Upload
  • Affiliation
  • Mail
in code:
Code: (vb) [Select]
Namespace Evilzone
Public Class Home
end Class
Public Class News
end Class
Public Class Forum
end Class
Public Class IRC
end Class
Public Class Contact
end Class
Public Class AboutUs
end Class
Public Class Upload
end Class
Public Class Affiliation
end Class
Public Class Mail
end Class
end Namespace
Code: (C#) [Select]
Namespace Evilzone{
Class Home
{
}
Class News
{
}
Class Forum
{
}
Class IRC
{
}
Class Contact
{
}
Class AboutUs
{
}
Class Upload
{
}
Class Affiliation
{
}
Class Mail
{
}
}
(the Public in Public Class, doesn't have that much effect, so you can leave it out, but I always write it like this)
now when you are going to recall it, it will look something like this:


as you can see it doesn't matter in which order you put it, because it will put it in alphabetic order, now you must understand that to use a create Class you must assign it to a variable:
Code: (vb) [Select]
Dim Something as Evilzone.Forum
Code: (C#) [Select]
Evilzone.Forum Something
or you Import Evilzone so you wont have to recall it by Evilzone.Forum but by Forum
Code: (vb) [Select]
imports Evilzone

Dim Something as Forum
Code: (C#) [Select]
Using Evilzone

Forum Something

now we need to give these 9 class
function so they can be used, so we will start with the Class forum.
and give it the function:
  • Login
  • logout
  • LoginResult

in code:

Code: (vb) [Select]
Namespace Evilzone
Class Forum
Public Sub Login(ByVal Username as String, ByVal Password as string)
'do stuff to login
end sub
Public Sub Logout()
'do stuff to login
end sub
Public Function Login Result()
Dim B as boolean
'check if you have been logged in, yes or no and write it to B
return B
end Function
Code: (C#) [Select]
Namespace Evilzone{
Class Forum{
public void Login(ByVal Username as String, ByVal Password as string){
//do stuff to login
}
public void Logout(){
//do stuff to login
}
public void Login Result(){
bool B
//check if you have been logged in, yes or no and write it to B
return B
}
now Evilzone.forum has functions

and those can be called as long Evilzone.Forum was set into a variable:

in code:
Code: (vb) [Select]
Public Class Form1
Private Sub Main()
Dim Forum As Evilzone.Forum
Forum.Login("Huntondoom", "************")
Forum.LoginResult()
Forum.Logout()
End Sub
End Class
Code: (C#) [Select]
Public Class Form1{
Private void Main(){
Evilzone.Forum Forum
Forum.Login("Huntondoom", "************")
Forum.LoginResult()
Forum.Logout()
}
}
(no that is not my real password, nor the same length)

this way you can create a lot of new namespace
even make really long(and useless) namespace

I hope you have learned how to use namespace and will use it in your own codes

 
« Last Edit: May 22, 2011, 10:58:22 pm by Huntondoom »
Aslong as you are connected to the internet, you'll have no privacy

Advanced Internet Search
Clean Up!