Creating Your Own Procedures-程序员宅基地

技术标签: string  function  ASP.NET Developer's JumpStart  class  parameters  properties  object  

Creating Your Own Procedures
As you write Visual Basic .NET applications, you'll want to break your code
up into manageable "chunks." This shouldn't be a new concept to you, and it
should make sense. The larger the blocks of code you're working with, the
harder it is to debug, maintain, and manage the code. You've seen several
examples of this throughout this chapter already.

The building block of Visual Basic .NET code is the procedure. A procedure
has a beginning (a declaration), a middle (the code inside the procedure)
and an end (the End Sub statement, for example). As you'll see throughout
this book, there are a number of types of procedures, but for now, we'll
focus on Sub and Function procedures.

A Sub procedure is a block of code that includes code, takes some action,
but doesn't return a value. A Function procedure takes some action but
returns a single value. So far, you've seen event procedures—Sub
procedures that run in reaction to events on pages.

Why use procedures? Certainly, if you find that you're writing the same
lines of code in multiple places, you would want to create a procedure you
could call from all the places you would otherwise use the code and copy
that code into the new procedure.

If you double-click the Creating Procedures button on the VBLanguage.aspx
Web Form, it will lead you to the procedure shown in Listing 7.5.

Listing 7.5 Procedures Are Useful for Breaking Up Code into More Manageable
Private Sub ProceduresSample()
  Dim strConn As String

  ' Call a procedure that
  ' doesn't return a value.

  ' Call a Function to Return a Value
  strConn = ConnectStringBuild("BJones", "password")

  Response.Write("strConn = " & strConn & "<BR><BR>")
End Sub

The InitControls procedure is a simple Sub that takes an action (hiding a
control) and returns no value at all:

Private Sub InitControls()
  ' Demonstrate a procedure
  ' that doesn't return a value.
  btnDataTypes.Visible = False
End Sub

The ConnectStringBuild procedure allows you to pass parameters, making it
possible to create generalized functionality. VB6 developers should note
that all parameters are passed by value in VB .NET, unless you specify
otherwise using the ByRef keyword. (This is the exact opposite of the way
VB6 works.) In addition, if you don't specify ByRef or ByVal, Visual Studio
.NET inserts the ByVal keyword for you. The ConnectStringBuild procedure
provides an example of this, allowing you to specify two parameters that
the procedure uses in creating an appropriate ADO.NET connection string:

Private Function ConnectStringBuild( _
ByVal LoginID As String, _
ByVal Password As String) As String
  Return String.Format("Provider=sqloledb;" & _
   "Data Source=(local);Initial Catalog=Northwind;" & _
   "User ID={0};Password={1}", LoginID, Password)
End Function

Creating Classes
Creating your own classes allows you to encapsulate behavior in a reusable
fashion and allows you to provide the templates for creating multiple
instances of an object based on the template. That is, using classes, you
can create your own additions to the base class library provided by the
.NET Framework—every single object you use in your applications is based
on some class, created by some developer, describing the behavior of that

The techniques for creating classes in VB .NET haven't changed much since
VB6, although you can now have more than one class per file. You still need
to create properties, methods, and possibly events. Methods and event
declarations have not changed much since VB6, but creating properties
requires a slightly different syntax.

As an example, we've provided the LoginInfo class, which provides LoginID,
Password, and DataSource properties, and the ConnectStringBuild method.

Double-click Creating Classes on the VBLanguage.aspx Web Form to find a
procedure that creates an object of the LoginInfo type. The definition for
this class also resides in the VBLanguage.aspx file, just below the end of
the Web Form's class. The code for this class is shown in Listing 7.6.

Listing 7.6 Classes Can Be Used to Group Data with Procedures That Interact
with That Data
Public Class LoginInfo
  Private mstrLoginID As String
  Private mstrPassword As String
  Private mstrDataSource As String

  Property LoginID() As String
      Return mstrLoginID
    End Get
    Set(ByVal Value As String)
      mstrLoginID = Value
    End Set
  End Property

  Property Password() As String
      Return mstrPassword
    End Get
    Set(ByVal Value As String)
      mstrPassword = Value
    End Set
  End Property

  Property DataSource() As String
      Return mstrDataSource
    End Get
    Set(ByVal Value As String)
      mstrDataSource = Value
    End Set
  End Property

  Public Function ConnectStringBuild() As String
    Return String.Format("Provider=sqloledb;" & _
      "Data Source=(local);Initial Catalog=Northwind;" & _
      "User ID={0};Password={1}", Me.LoginID, Me.Password)
  End Function
End Class

To use this class, you will declare a new instance of it, set the
properties you require, and then invoke the ConnectStringBuild method to
have it return the value. Listing 7.7 shows an example of creating and
using a class.

Listing 7.7 Using Your Own Class Is as Simple as Declaring a New Instance
of the Class
Private Sub ClassSample()
  Dim oLogin As New LoginInfo()

  With oLogin
    .LoginID = "BJones"
    .Password = "password"
    .DataSource = "(local)"

    Response.Write("ConnectString=" & _
      .ConnectStringBuild() & "<BR>")
  End With
End Sub

One problem with the LogInfo class, as it stands, is that you must create a
unique instance of the class each time you want to generate the connection
string. Sometimes, you may want to be able to call a method of a class
without needing to instantiate the object yourself. For example, when you
used the String.Format method, you didn't need to create a String object
variable, set it equal to a new String object, and then work with the
object. The String class provides the Format method (among many of its
methods) in a special way that allows the .NET Framework to instantiate an
object for you, as necessary. By adding the Shared keyword to a method, you
won't need to create an instance of the parent object before calling the
method—the .NET Framework will handle this for you.

We've provided a class, in DataHandler.vb, that we'll use throughout many
examples (and we'll expand the class as necessary). This class provides a
shared ConnectStringBuild method—you pass it user ID and password values,
and the method returns the appropriate connection string. The important
thing to remem-ber is that because the method is shared, you needn't create
an instance of the DataHandler class—simply call the method, and the .NET
Framework will do the work for you.

The following procedure, called when you click Classes with Shared Methods
on VBLanguage.aspx, calls the ConnectStringBuild method of the DataHandler
class. Note that the code never creates an instance of the DataHandler
class because the ConnectStringBuild method is shared. Here's the code:

Private Sub SharedMethodSample()
  Dim strConn As String

  strConn = _
   DataHandler.ConnectStringBuild("Bjones", "Password")

End Sub

The definition for the DataHandler class can be found in the DataHandler.vb
file within the VBLanguage.sln solution. This class looks like this:

Public Class DataHandler
  Public Shared Function ConnectStringBuild( _
   ByVal LoginID As String, _
   ByVal Password As String) As String
    Dim strConn As String

    If LoginID = String.Empty Then
      LoginID = "sa"
    End If
    strConn = String.Format("Provider=sqloledb;" & _
     "Data Source=(local);Initial Catalog=Northwind;" & _
     "User ID={0};Password={1}", LoginID, Password)

    Return strConn
  End Function
End Class

Applying What You Learned
Let's now take some of the techniques you have learned throughout this
chapter and apply them to the Northwind solution you are building. Follow
these steps to add a class that will be used throughout the rest of this

Bring up your Northwind.sln file.

Set Option Strict to On.

Select the Project, Add Class menu item.

Set the class name to DataHandler.vb.

Add a shared method named ConnectStringBuild to this DataHandler class.
Re-create the code shown in the shared ConnectStringBuild method in this

Bring up Main.aspx in the page designer.

Double-click the Logout LinkButton control and modify the lnkLogout_Click
procedure so it looks like this:

Private Sub lnkLogout_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs)

End Sub

Open Login.aspx in the page designer.

Double-click the Login button and modify the btnLogin_Click procedure so
that it looks like this:

Private Sub btnLogin_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnLogin.Click
  Session("LoginID") = "sa"
  Session("Password") = ""

End Sub

For now, you will just set LoginID to "sa" so you can log in to your SQL
Server database.

Change the Page_Load procedure of Main.aspx to set the Visible property,
depending on whether Session("LoginID") is an empty string, as shown here:

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

  Dim blnShow As Boolean
  blnShow = _
   (Session("LoginID").ToString = String.Empty)

  lnkLogout.Visible = blnShow
  hypEmpMaint.Visible = blnShow
  hypPwdChange.Visible = blnShow
End Sub

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。


禁止EditText焦点获取_禁止 edittext 获取焦点-程序员宅基地

在父布局上添加以下属性即可android:focusableInTouchMode="true"android:focusable="true"_禁止 edittext 获取焦点

android 平板右下角WIFI无信号问题_android 移植wifi wifi信号不显示-程序员宅基地

问题描述: android平板状态栏整合到了屏幕右下角, 我们在使用某些WIFI模块的时候会发现, WIFI热点扫描后, 可以看到热点有信号显示, 但是, 在连接上热点后, 右下角却始终没有信号格显示。 原因分析: 在WIFI连接后, 状态栏一直接收不到信号值变化的广播, 导致无法获取到WIFI信号值。至于为何收不到, 应该就跟模块和驱动有关了。fram_android 移植wifi wifi信号不显示

用 react + “ant-design-pro 的 Tabs“ 实现顶部菜单·记_ant design pro头部做tab_weixin79893765432...的博客-程序员宅基地

1、页面顶部添加 tab,点击切换 tab 时,可切换2、将 tab 与路由双向绑定3、调整样式(1)、水平垂直居中(2)、还原 tab 字体的样式_ant design pro头部做tab

Attaching detached POCO to EF DbContext - simple and fast-程序员宅基地

IntroductionRecently I was playing around with Entity Framework (EF) and evaluating it for some projects. I had a very hard time figuring out how to attach detached object graph to DBContext in...


前言如果你涉及软件开发,可能会经常听到“高内聚,低耦合”这种概念型词语。我在第一次接触这种词语时,也抱有很大的疑问,但我发现百度上大部分都是高大上的词汇解释。仔细通读这篇文章,你就能理解耦合性和内聚性的概念与关系。WHY?… …点击这里查看全文..._耦合度大白话


在运行PS时会提示:无法启动此程序,因为计算机丢失 XXX.DLL 错误信息。在电脑运行某一个程序的时候,需要很多操作系统的DLL动态库文件来支持才能正常的启动,如果没有找到程序需要的DLL,就会出现上面的错误信息。出错效果如下:那么如何解决这个问题呢?DLL 英文全称:Dynamic Link Library,即程序动态链接库文件,应用程序扩展文件。在电脑运行某一个程序的时候,需要很多操作系统的..._ps bate25.0安装后无法打开提示缺少文件


Windows下Python3安装Turtle报错及解决方案_"file \"c:\\users\\public\\turtleworkspace\\我的项目14-程序员宅基地

转自安装在Windows下Python3安装会提示如下报错:Looking in indexes: turtle Using cached"file \"c:\\users\\public\\turtleworkspace\\我的项目14\\我的文件.py\", line 15"

layui——下拉框监听_layui下拉框 监听-程序员宅基地

&lt;select name="" id="userSelect" lay-filter="userClass"&gt; &lt;option value="0"&gt;用户&lt;/option&gt; &lt;option value="1"&gt;vip&lt;/option&gt; &l_layui下拉框 监听



SAP ML 物料分类账详解(含取消激活物料帐方法)_sap okb9-程序员宅基地

一、业务背景:中国会计准则规定,对存货的核算必须采用历史成本法(即实际成本法).如果企业采用计划成本法或者定额成本法进行日常核算的,应当按期结转其成本差异,将计划成本或者定额成本调整为实际成本.“存货采用计划成本法核算,有利于简化财务会计处理工作,有利于考核采购部门的经营业绩,促使降低采购成本、节约支出”.完全成本法和变动成本法最核心、本质的差异在于对固定制造费用的处理上,_sap okb9

实验二 HTML以及J2EE简单编程-程序员宅基地