|
-
Oct 31st, 2005, 05:54 PM
#1
Thread Starter
Hyperactive Member
Console Class
We have the namespace System. In that namesapce there is a class that is Console. If we want to print any string on the screen in the console apllication, we will write as,
System.Console.WriteLine("My Application");
As you know that when we create classes, we have to create an instance of the class, which we can say an object. After creating the object of the class, Now we can access all the member and method of the entire class. But my question is that without declaring any method of the Console class, we access its method like WriteLine, Why?
Why we did not declare the object of the consloe class and then we access its member and method of the console class.
Thanks
"Visual Studio .NET 2005/.NET Framework 2.0"
The eyes are the greatest telescopes, one will ever need to study the universe. Let the heart be the tripod, the mind be the shaft, then let the eyes become the lens of that great telescope of which God gave to us all.
-
Oct 31st, 2005, 06:08 PM
#2
Re: Console Class
WriteLine is declared as Shared. A Shared member is a member of the class itself, rather than any paricular instance of that class. While you can qualify a Shared member with the name of a variable of the correct type, it is considered "proper" to use the class name to specifically indicate that it is a Shared member. Many classes have Shared members, and in fact some classes have nothing but Shared members, like the System.IO.File class.
-
Oct 31st, 2005, 06:15 PM
#3
Fanatic Member
Re: Console Class
Hi,
In actuall fact you do declare the class if you use the using function. Example.
Code:
using System;
namespace hello
{
Class clsMain
{
public static void Main()
{
Console.Writeline ("Hello world")
}
}
}
Its the same for all classes that are created and declared within C#, whether that be a custom class library that you have written, or the built in...
Using System;
using System.Windows.Forms;
Using System.Drawing;
etc
check out.
www.codeproject.com
www.msdn.microsoft.com
http://www.csharp-station.com/Tutorial.aspx
for all the information you need
Hope this helps
ta
kai
As the information I give is useful in its nature, consider using the RATE POST feature located on the bottom left of this post please..
A few things that make a good Developer a Great One.
Methodical and a thorough approach to research and design inevitably leads to success.
Forward thinking is the key to Flow of control.
Never test in the design environment, always test in real time, you get the REAL results.
CBSE & OOSE are the same animal, they just require different techniques, and thinking.
SEO is a globe of objectives, SE rankings is an end to a means for these objectives, not part of them.
The key to good design is explicit attention to both detail and response.
Think Freely out of the "Box" you're in..... You will soar to better heights.
Kai Hirst - MSCE, MCDBA, MCSD, MCP, MCAP, MSCT
-
Oct 31st, 2005, 06:21 PM
#4
Thread Starter
Hyperactive Member
Re: Console Class
 Originally Posted by jmcilhinney
WriteLine is declared as Shared. A Shared member is a member of the class itself, rather than any paricular instance of that class. While you can qualify a Shared member with the name of a variable of the correct type, it is considered "proper" to use the class name to specifically indicate that it is a Shared member. Many classes have Shared members, and in fact some classes have nothing but Shared members, like the System.IO.File class.
Dear I could not understand what you are saying. If you can explain your answer with the help of an example then i will be able to understand what you saying. I am very thankful to you.
Imran Ahmad Mughal
"Visual Studio .NET 2005/.NET Framework 2.0"
The eyes are the greatest telescopes, one will ever need to study the universe. Let the heart be the tripod, the mind be the shaft, then let the eyes become the lens of that great telescope of which God gave to us all.
-
Oct 31st, 2005, 06:30 PM
#5
Re: Console Class
kaihirst, you seem to be a bit fuzzy on a couple of points. The "using" statement does not DECLARE anything. It simply imports a namespace so that you do not have to qualify the names of the member types. Without "using System;" you would have to fully qualify the class name so:
Code:
System.Console.WriteLine("Hello");
Either way, there is no variable of type Console declared and no instance of the Console class created.
ahmad_iam, I forgot that I was in the C# forum I'm afraid. Shared is a VB.NET term. The C# equivalent is "static". I don't really know how much more I can tell you. You ask for an example but you've provided one yourself. You can call the WriteLine method without an instance of the Console class. That's what static members are. I suggest you check the help/MSDN for more information on the "static" keyword.
-
Oct 31st, 2005, 06:38 PM
#6
Thread Starter
Hyperactive Member
Re: Console Class
 Originally Posted by jmcilhinney
kaihirst, you seem to be a bit fuzzy on a couple of points. The "using" statement does not DECLARE anything. It simply imports a namespace so that you do not have to qualify the names of the member types. Without "using System;" you would have to fully qualify the class name so:
Code:
System.Console.WriteLine("Hello");
Either way, there is no variable of type Console declared and no instance of the Console class created.
ahmad_iam, I forgot that I was in the C# forum I'm afraid. Shared is a VB.NET term. The C# equivalent is "static". I don't really know how much more I can tell you. You ask for an example but you've provided one yourself. You can call the WriteLine method without an instance of the Console class. That's what static members are. I suggest you check the help/MSDN for more information on the "static" keyword.
Ok tell me can we use the member of the class without declaring the object of the class. Dear explain your answer with the help of any example. By providing an example, I can better understand. As you know if you have the better concepts of the programming language, then you can create a well affected apllication. I hope you will.
Thanks
Imran Ahmad Mughal
"Visual Studio .NET 2005/.NET Framework 2.0"
The eyes are the greatest telescopes, one will ever need to study the universe. Let the heart be the tripod, the mind be the shaft, then let the eyes become the lens of that great telescope of which God gave to us all.
-
Oct 31st, 2005, 06:47 PM
#7
Re: Console Class
You can call any member of a class (or structure) that is declared "static" without creating an instance of the class, e.g.
Code:
// Call the static WriteLine method of the Console class.
System.Console.WriteLine("This line was written without creating an instance of the Console class.")
Any members NOT declared "static" are considered instance members, and can only be called by first creating an instance of the class.
http://msdn.microsoft.com/library/de...rfStaticPG.asp
-
Oct 31st, 2005, 06:58 PM
#8
Thread Starter
Hyperactive Member
Re: Console Class
 Originally Posted by jmcilhinney
You can call any member of a class (or structure) that is declared "static" without creating an instance of the class, e.g.
Code:
// Call the static WriteLine method of the Console class.
System.Console.WriteLine("This line was written without creating an instance of the Console class.")
Any members NOT declared "static" are considered instance members, and can only be called by first creating an instance of the class.
http://msdn.microsoft.com/library/de...rfStaticPG.asp
Thanks jmcilhinney, Now the point is clear and I can now better understand what you are saying. I am very thankful to you.
Dear It is my suggest to you, that is provide exmple with your answer.
Imran Ahmad Mughal
"Visual Studio .NET 2005/.NET Framework 2.0"
The eyes are the greatest telescopes, one will ever need to study the universe. Let the heart be the tripod, the mind be the shaft, then let the eyes become the lens of that great telescope of which God gave to us all.
-
Nov 1st, 2005, 12:28 AM
#9
Re: Console Class
Far out you guys, get different avatars. It's so confusing 
Anyway, static classes of non-static classes are used to group related methods that don't form part of an object as such, but relate to instances of the class. Non-static methods of non-static classes are used for object instance specific functionality.
An example of this in the .NET framework would be the String class, it's not a static class but has static members that you can call without needing a String object, such as String.Compare(), String.Concat(), etc.
The Console class, on the other hand, is declared as static - you can't create an instance of it at all, and all its member functions must also be static. You could talk about THE Console object, but not A Console object - since there can only be one.
Here is some more information on the topic, also from MSDN.
Hope that helped a bit as well
-
Nov 1st, 2005, 12:50 AM
#10
Re: Console Class
 Originally Posted by penagate
Far out you guys, get different avatars.
I was here first.
-
Nov 1st, 2005, 01:10 AM
#11
Re: Console Class
You should get the different avatar anyways. As if you only know two languages :P
Bill
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|