Creating Your Own Procedures

本文围绕Visual Basic.NET展开,介绍了创建程序和类的方法。程序方面,重点讲解了Sub和Function程序的特点与使用场景;类的创建上,说明了如何封装行为、创建属性和方法,还提及共享方法可避免实例化对象。最后将所学技术应用到Northwind解决方案中。
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
Chunks
Private Sub ProceduresSample()
  Dim strConn As String

  ' Call a procedure that
  ' doesn't return a value.
  InitControls()

  ' 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
object.

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
    Get
      Return mstrLoginID
    End Get
    Set(ByVal Value As String)
      mstrLoginID = Value
    End Set
  End Property

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

  Property DataSource() As String
    Get
      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")



  Response.Write(strConn)
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
book:

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
chapter.

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)
  Session.Abandon()

  Response.Redirect("Main.aspx")
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") = ""

  Server.Transfer("Main.aspx")
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

<think>好的,我现在需要处理用户关于“Onboarding Procedures in software development or IT context”的查询。首先,我得确认用户到底想知道什么。用户可能是在进入新公司或团队时,需要了解如何顺利融入,或者作为管理者,想优化现有的入职流程。 先回顾一下之前的对话历史。用户之前询问了PCB设计软件的翻译,我提供了术语解析和扩展场景翻译,还有相关问题。现在用户转向软件开发或IT环境中的入职流程,这可能是一个新的主题,但需要保持回答结构清晰,符合用户之前的要求,比如使用中文,正确使用LaTeX,生成相关问题等。 用户提到了几个引用,但根据指示,这些引用仅供参考,并不是用户真正的需求,所以应该主要参考用户当前的问题和之前的对话。之前的回答中,用户喜欢分点说明,结合示例和实际应用场景,比如快速排序的代码示例和相关问题。因此,这次回答也需要结构清晰,分步骤或模块,可能包括流程步骤、关键要素、示例模板和相关工具。 接下来,我需要定义Onboarding Procedures在软件开发和IT中的具体内容。通常包括环境配置、文档熟悉、代码库访问、团队融入等。可能需要分阶段说明,比如入职前准备、首日安排、首周任务、首月目标等。同时,结合用户提供的引用中的一些技术点,比如CI/CD集成、B/S架构,可能可以举例说明如何将这些技术融入入职流程。 另外,用户提到的引用中有关于测试用例管理系统和系统架构的内容,可能需要强调文档管理和工具使用在入职中的重要性。比如,使用集中化的测试管理系统帮助新成员快速上手,或者前后端分离架构如何影响开发环境的配置步骤。 还要注意生成相关问题,参考之前的例子,问题应围绕优化入职流程、工具选择、常见挑战等。同时,确保使用正确的中文术语,比如“持续集成工具”而非直接翻译的英文术语,除非必要。 最后,检查是否满足所有系统级指令:行内数学表达式用$...$,独立公式用$$...$$,LaTeX语法正确,中文回答,生成相关问题,引用标识。不过当前问题可能不涉及数学表达式,但如果有技术术语或工具名称,可能需要适当处理。 总结,回答结构应包括定义、核心流程、关键要素、示例模板、工具推荐、常见挑战和相关问题。确保每个部分简洁明了,结合实例,如使用Markdown代码块展示Checklist模板,引用中的云解决方案和CI/CD流程如何应用于入职自动化。同时,验证是否符合用户需求,即详细的指南或信息,帮助用户实际应用。</think>### Onboarding Procedures(入职流程)在软件开发/IT领域的核心要素与实施指南 在软件开发或IT环境中,**Onboarding Procedures** 指新成员(开发者、运维人员等)融入团队并熟悉技术栈、流程和工具的标准化流程,其目标是缩短适应周期,降低因环境配置或沟通不畅导致的生产力损失[^2]。以下是分阶段详解: --- #### 一、核心流程与关键步骤 1. **入职前准备(Pre-Onboarding)** - **环境预配置**: 提前为新成员分配开发环境权限(如Git仓库、CI/CD流水线、云服务器SSH密钥),并通过自动化脚本(Shell/Python)实现依赖安装,例如: ```bash # 示例:开发环境初始化脚本 sudo apt-get install -y docker-ce python3-pip pip install -r requirements.txt ``` - **文档整理**: 提供结构化文档: - 代码规范(如 PEP8、Google Java Style) - 项目架构图(参考用户引用中的B/S分离架构描述[^3]) - 内部工具使用指南(如测试用例管理系统[^2]) 2. **首日安排(Day 1)** - **工具链配置**: 确保本地开发环境与团队一致,例如: ```bash # 引用中的Python依赖安装命令[^4] pip install -U elasticsearch langchain transformers huggingface_hub ``` - **权限申请**: - 代码仓库(GitLab/GitHub)访问权限 - 监控系统(Grafana/Prometheus)查看权限 3. **首周任务(Week 1)** - **代码熟悉**: 分配一个简单的Bug修复任务,通过代码评审(Code Review)熟悉协作流程。 - **团队融入**: 安排与关键角色(产品经理、QA)的1:1会议。 4. **首月目标(Month 1)** - **独立交付**: 完成一个小型功能模块的开发,并通过CI/CD流水线部署到测试环境[^2]。 --- #### 二、关键要素与最佳实践 1. **自动化工具链** - 使用 **Infrastructure as Code (IaC)**(如Terraform)快速复制开发环境。 - 利用容器化(Docker/Kubernetes)保证环境一致性。 2. **文档质量** - 维护可搜索的Wiki(如Confluence),避免“口口相传”导致的信息断层。 - 提供**交互式教程**(如Jupyter Notebook)降低学习门槛。 3. **文化适配** - 明确团队沟通规范(如Slack响应时间、会议礼仪)。 - 分享技术选型背后的决策逻辑(例如为何选择云原生架构[^3])。 --- #### 三、示例:Onboarding Checklist模板 ```markdown ## 开发人员入职清单 - [ ] 完成SSH密钥配置并测试Git仓库访问 - [ ] 本地运行 `make setup` 完成环境初始化 - [ ] 阅读架构文档并提交3个疑问 - [ ] 通过CI/CD流水线部署首个测试版本 ``` --- #### 四、常见挑战与解决方案 | **挑战** | **解决方案** | |----------|--------------| | 环境依赖冲突 | 使用Docker镜像或虚拟环境(如Python的`venv`)隔离依赖 | | 文档过时 | 将文档更新纳入代码合并的强制检查项(如Git Hook) | | 跨时区协作 | 设立重叠工作时间(Overlap Hours)并异步沟通(Loom视频记录) | --- ### 相关问题 1. 如何衡量Onboarding流程的有效性(如通过“首次交付时间”指标)? 2. 远程团队(Remote Team)的Onboarding需要哪些额外措施? 3. DevOps团队与纯开发团队的Onboarding流程差异点是什么? [^2]: 参考集中化测试管理系统集成CI/CD的实践。 : 基于B/S架构的前后端分离开发模式。 [^4]: Python依赖管理的标准化操作示例。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值