Using Component (Private Access Modifier) in ASP.NET - Part 4


Introduction

It is very-very lazy way to create a property is to create a public field. For simple application it is good but if you are working on very secure application then it is very important to point out all such points too. Here in this article we will be using Private access modifier.



Class1.vb File Code

Imports Microsoft.VisualBasic

Public Class Class1
    Private WelcomeMsg As String


    Public Property Message() As String
        Get
            Return WelcomeMsg
        End Get
        Set(ByVal Value As String)
            If Value.Length > 5 Then
                Throw New Exception("Message too long!")
            End If
            WelcomeMsg = Value
        End Set
    End Property


    Public Function SayMessage() As String
        Return WelcomeMsg
    End Function

End Class


Default.aspx File Code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>


Default.aspx.vb File Code


Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgsHandles Me.Load
        Dim msg As New Class1
        msg.Message = "Welcome to MINDCRACKER"
        Label1.Text = msg.SayMessage
    End Sub
End Class


Now look at the above code we have used

If Value.Length > 5 Then
      Throw New Exception("Message too long!")
End If

In above code we have limited the Message size by 5. But the message we have used it longer then 5 characters. This will cause a server side error. We have to increase the message length as per actual message size.

Note: Continue in Next Part.

Comments

Popular posts from this blog

Customize User's Profile in ASP.NET Identity System

Lambda two tables and three tables inner join code samples