Using Component (Public Access Modifier) in ASP.Net: Part 3


Here is Part 2

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. We will discuss the better technique that is by using Private Field in next article that is in Part - 4. If we declare any field with the Public access modifier, then the field can be accessed from outside the component. The example given below contains a public field named Welcome.


Class1.vb File Code

Imports Microsoft.VisualBasic

Public Class Class1
    Public WelcomeMsg As String

    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" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

Default.aspx.vb File Code

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim msg As New Class1()
        msg.WelcomeMsg = "Welcome to MINDCRACKER"
        Label1.Text = msg.SayMessage()
    End Sub
End Class

Note: Continue in Next Part.


Up Next
    Ebook Download
    View all
    View all