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

Introduction

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.

Component3.gif

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

HAVE A GREAT CODING!

Up Next
    Ebook Download
    View all
    Learn
    View all