Asp.net advanced tutorial --- object

Source: Internet
Author: User

Class in C # can also be called an object. It consists of the following parts: member variables, attributes, and
Method, which is required
This class does not contain any parameters. It does not specify the return type and serves as a member of the initialization class.
Variable and memory allocation. And
C ++ is different. c # class only has constructor and does not require analysis functions. That is to say, you only need to allocate memory for member variables.
Without explicit release.
Put the memory, because c # and java both release the memory through the garbage collector. To understand this, we can
To 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, even though it does not do anything :). Below we
Add a private struct member variable m_strTitle to it and initialize this member variable in the constructor,
The definition of the entire class is like this:
Public class MyClass
{
// Private member variable
Private string m_strTitle;

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

}

Note: This line of code defines the member variables: private string m_strTitle; here, string is easy to understand
Ming m_strTitle is
For the string type, what is the role of the first private? This private (private) indicates this Member
The variable is private.
Internal functions can be used, and functions including subclasses in other places cannot use it, except for private
You can also define
Public and protected indicate that this member variable can be used anywhere.
Protected indicates this change
Quantity can only be used in this class or subclass. Therefore, if we want to use this member variable, we can define it as p
Ublic, but
This is not a good programming habit for programming, because it destroys the encapsulation of classes. The good method in c ++ is to redetermine
To access this
Private variables, and c # provides a more convenient method, that is, property.
This attribute Title is added:

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


Can be used anywhere
This attribute is used, followed by two keywords: this and value, this represents the class itself, so this. m _
StrTitle indicates the member variable m_strTitle of this class. value indicates the equal sign when this attribute is used as the left value.
The value on the right, like this: myClass. Title = "hello", then the value of value is "hello". Okay,
This class is ready for use, 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 contains a character.
Parameters.
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 classes. After reading these contents, you may be able to understand what we constructed in the previous section.
Bbs objects, let's
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 ));
}

}
}
}

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.