Asp.net advanced tutorial (III)-Object

Source: Internet
Author: User

As we talked about how to construct bbs objects, a friend asked me to briefly introduce how to construct objects in c #. Let me briefly introduce this lesson.

Class in C # can also be called an object. It consists of the following parts: member variables, attributes, and methods, among them, it is essential that this class does not contain any parameters of the constructor, it does not specify the return type, the role is to initialize the class member variables, allocate memory and so on. Unlike c ++, the c # class only has constructor and does not require an analysis function. That is to say, you only need to allocate memory for the member variables and do not need to explicitly release the memory, this is because c # and java both release the memory through the garbage collector. To understand this, we can construct the simplest class, a class that does nothing:

Public class MyClass
{
Public MyClass (){};
}

You only need to create a new one for use, like this: MyClass myClass1 = new MyClass (); do not doubt that you have already created an object, although it does not do anything :). Next we will add a private struct member variable m_strTitle to it, and initialize this member variable in the constructor. The definition of the entire class will become like this:
Public class MyClass
{
// Private member variable
Private string m_strTitle;

// Constructor
Public MyClass ()
{
M_strTitle = "I have been assigned an initial value ";
}

}

Pay attention to the line of code defining member variables: private string m_strTitle; string is easy to understand, meaning that m_strTitle is of the string type. What is the role of the first private? This private (private) indicates that this member variable is private and can only be used by functions in this class. functions including subclass in other places cannot use this variable, except for private, you can also define it as public and protected. public indicates that this member variable can be used anywhere, and protected indicates that this variable can only be used in this class or subclass. Therefore, if we want to use this member variable, we can define it as public, but this is not a good programming habit for Object-Oriented Programming, because it breaks the encapsulation of classes, in c ++, a good way is to define a public function to access this private variable. in c #, a more convenient method is provided, that is, property ), now we add this attribute Title:
Public class MyClass
{
// Private member variable
Private string m_strTitle;

// Attributes
Public string Title
{
Get
{
Return this. m_strTitle;
}
Set
{
This. m_strTitle = value;
}
}
// Constructor
Public MyClass ()
{
M_strTitle = "I have been assigned an initial value ";
}

}

Let's take a look at how to define attributes. First, the scope qualifier is also required. Generally, public is used to indicate that this attribute can be used anywhere. Second, there are two keywords: this and value, this indicates the class itself, so this. m_strTitle indicates the member variable m_strTitle of this class. value indicates the value on the right of the equal sign when this attribute is used as the left value, as shown in the following code: myClass. title = "hello", the value of the value is "hello". Now, this class can be used, as shown below:

Public static void Main (String [] args)
{
MyClass myClass = new myClass (); // construct an instance of the MyClass class
Console. WriteLine (myClass. Title); // The result is: I have been assigned an initial value.
MyClass. Title = "my value has changed"; // you can change the value of the Title attribute.
Console. Writeline (myClass. Title); // The result is: My value has changed.
}
Now, let's add a MyMethod method to this class. This method has no return value and has a struct parameter.
Public class MyClass
{
// Private member variable
Private string m_strTitle;

// Attributes
Public string Title
{
Get
{
Return this. m_strTitle;
}
Set
{
This. m_strTitle = value;
}
}

// Constructor
Public MyClass ()
{
M_strTitle = "I have been assigned an initial value ";
}

// Method
Public void MyMethod (string a_str)
{
This. m_strTitle = a_str;
}
}

Here we can rewrite the program just now. The running result is the same as that just now:

Public static void Main (String [] args)
{
MyClass myClass = new myClass (); // construct an instance of the MyClass class
Console. WriteLine (myClass. Title); // The result is: I have been assigned an initial value.
MyClass. MyMethod ("My value changed"); // you can change the value of the Title attribute.
Console. Writeline (myClass. Title); // The result is: My value has changed.
}

The above briefly explains how to define a class. After reading this content, you may be able to understand the bbs object we constructed in the previous section. Let's take a look at its definition:

Namespace MyOwnClass
{
Using System;
Using System. Data. SQL;
Using System. Data;

//////////////////////////////////////// ////////////////////////////
//
// Class Name: BBS
//
// Description: Forum class, which constructs a forum object.
//
// Date: 2000/02/03
//
//////////////////////////////////////// ///////////////////////////
Public class BBS
{
// Private variable
Private string m_strTitle; // bbs name
Private int m_intForumCount; // Number of pages
Private int m_intTopicCount; // Number of posts
Private int m_intUserCount; // number of registered users

// Attributes
Public string Title
{
Get
{
Return m_strTitle;
}
}

Public int ForumCount
{
Get
{
Return m_intForumCount;
}
}

Public int TopicCount
{
Get
{
Return m_intTopicCount;
}
}

Public int UserCount
{
Get
{
Return m_intUserCount;
}
}

// Constructor
Public BBS (string a_strTitle)
{
//
// TODO: Add Constructor Logic here
//
M_strTitle = a_strTitle;

// Read the database
MyConnection myConn = new MyConnection ();
SQLCommand myCommand = new SQLCommand ();
MyCommand. ActiveConnection = myConn;
MyCommand. CommandText = "up_GetBBSInfo"; // call the Stored Procedure
MyCommand. CommandType = CommandType. StoredProcedure;

Try
{
MyConn. Open ();
SQLDataReader myReader;
MyCommand. Execute (out myReader );
If (myReader. Read ())
{
M_intForumCount = (int) myReader ["ForumCount"];
M_intTopicCount = (int) myReader ["TopicCount"];
M_intUserCount = (int) myReader ["UserCount"];
}
Else
{
Throw (new Exception ("table or stored procedure does not exist "));
}

// Clear the scene
MyReader. Close ();
MyConn. Close ();
}
Catch (SQLException e)
{
Throw (new Exception ("Database Error:" + e. Message ));
}

}
}
}

This is a little different from what we just talked about. First, let's look at the namespace MyOwnClass in the first line and declare that the namespace of this class is MyOwnClass. The namespace is like a package, which can contain many classes. Let's look at this line again: using System; this tells the compiler that I want to reference objects in the System namespace. Then what else can be understood?


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.