collection list count (CollectionBase) list.add - problems

Discussion in 'ASP .Net' started by jason@cyberpine.com, Nov 2, 2003.

  1. Guest

    Hello. I've got this simple collection populate code I downloaded from
    the net (sorry can't find source now) I'm trying to test, but I can't
    seem to get it to work. Any help would be greatly appreciated.

    I've compiled the following VB.NET into a DLL:

    Imports System
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Collections

    Namespace jcpcollection

    Public Class Users
    Inherits CollectionBase

    'Retrieves an item from the collection by index
    Default Public Property Item(ByVal Index As Integer) As User
    Get
    Return CType(list.Item(Index), User)
    End Get
    Set(ByVal Value As User)
    list.Item(Index) = Value
    End Set
    End Property

    'Adds an item to the collection
    Public Function Addx(ByVal Item As User) As Integer
    Return list.Add(Item)
    End Function


    'test function
    Public Function test As Integer
    dim z as integer
    z=150
    return z
    End Function


    'Reports Total
    Public Function totalentries As Integer
    Return list.Count
    End Function



    Public Sub Remove(ByVal Item As User)
    list.Remove(Item)
    End Sub

    'A function that checks the length of the email address , applied to
    very member of the collection
    Public Shared Function ValidateEmail(ByVal emailAddress As String)
    If Not (emailAddress.Length > 0) Then Throw New
    ArgumentException("Please provide a valid email address")
    End Function

    End Class 'Users Collection - strongly typed IList collection of User
    objects


    Public Class User

    #Region "Attributes"

    'Internal storage for the UserName property
    Protected _UserName As String = Nothing

    'The user name of the user
    Public Property UserName() As String
    Get
    Return Me._UserName
    End Get
    Set(ByVal Value As String)
    Me._UserName = Value
    End Set
    End Property

    'Internal storage for the EmailAddress property
    Protected _EmailAddress As String = Nothing

    'Gets / Sets the EmailAddress of the User
    Public Property EmailAddress() As String
    Get
    Return Me._EmailAddress
    End Get
    Set(ByVal Value As String)
    Me._EmailAddress = Value
    End Set
    End Property

    #End Region


    #Region "Constructors/Destructors"

    'A constructor that accepts a DataTable
    Public Sub New(ByVal table As DataTable)

    If table.Rows.Count > 0 Then

    Dim row As DataRow

    row = table.Rows(0)

    'run the loop once because only one row can be matched to a member of
    a collection
    Me._EmailAddress = row("EmailAddress")

    Me._UserName = row("UserName")

    End If

    End Sub

    'Another constructor for initializing each member of the collection
    Public Sub New(ByVal nEmailAddress As String, ByVal nUserName As
    String)

    Me._EmailAddress = nEmailAddress
    Me._UserName = nUserName

    End Sub


    #End Region


    #Region "Functions/Procedures/Operations"

    #End Region

    End Class 'User Class

    End Namespace

    ===
    And here the ASP.NET (VB) I'm using to test it with:

    <%@ Import Namespace="System.IO" %>
    <%@ Import Namespace="System.Diagnostics"%>
    <%@ Import Namespace="System.Net" %>
    <%@ Import Namespace="System.Web.Mail"%>
    <%@ Import Namespace="System" %>
    <%@ Import Namespace="jcpcollection" %>

    <html>
    <body text=#336633 bgcolor=#CCAA99>

    <basefont size="2" font face="Veranda" color="#000000">

    <form method="post" runat="server">

    <asp:label id="mylabel" runat="server"/>
    <br>
    User:
    <asp:textbox value="" runat="server" id="userin"
    Rows="1" Width="200"/>
    <br>

    Email:
    <asp:textbox value="" runat="server" id="emailin"
    Rows="1" Width="200"/>
    <br>
    <br>
    <br>
    <asp:Button ID="submit" Text="Insert" Runat="server" onclick=testit
    />

    </form>




    <script language="VB" runat="server">


    Sub Page_Load(Src as object, E as EventArgs )

    'display total entries in collect here next

    Dim nUsers As New Users
    Dim nUser As User
    dim howmany as integer
    howmany = nUsers.totalentries
    response.write("<br>")
    response.write(howmany)
    response.write("<br>")
    response.write(nUsers.count)
    howmany = nUsers.test
    response.write("<br>")
    response.write(howmany)

    end sub



    sub testit(Src as object, E as EventArgs)

    Dim nUsers As New Users
    Dim nUser As User
    nUser = New User(emailin.text,userin.text)
    nUsers.ValidateEmail(emailin.text)
    dim xx as integer
    xx = nUsers.Addx(nUser)
    response.write("<br>")
    response.write(xx)
    response.write("<br>"+ nUser.username+"<br>"+ nUser.EmailAddress)


    end sub

    </script>


    ======

    When I press 'Insert' I don't see an updated count . I'm also getting
    a return code of 1 for ADDX.

    What's wrong? Other than my not know what the hell I'm doing - New to
    VB here.
     
    , Nov 2, 2003
    #1
    1. Advertising

  2. Natty Gur Guest

    Hi,

    Can't see the declaration of list. did you miss somthing ?

    Natty Gur[MVP]
    Phone Numbers:
    Office: +972-(0)9-7740261
    Fax: +972-(0)9-7740261
    Mobile: +972-(0)58-888377


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Natty Gur, Nov 3, 2003
    #2
    1. Advertising

  3. Guest

    Thanks for response.

    I think I figured out my problem. It was more conceptual than
    anything else.

    The collection is *always* recreated at postback and I was attempting
    to get a list count in the page_load of the asp.net, so naturally, I
    had a count of zero. The code was just an experiment to better
    understand vb.net collections.

    If I add a bunch of .addx(xxx,yyy) in the page_load and then get the
    count I do see the collection is populated. Though I gotta wonder,
    what the point is in using collections in web ap, when you need to
    store that info with some permanence somewhere anyways?

    All: Why use collections in Web aps? And not just store the info in db
    or flat file?
     
    , Nov 3, 2003
    #3
  4. Natty Gur Guest

    Performance,

    Retrieving data from memory is more efficient that from file or DB. But
    storing too much data on memory can cause memory caching on disk causing
    worse performance problem.

    Natty Gur[MVP]
    Phone Numbers:
    Office: +972-(0)9-7740261
    Fax: +972-(0)9-7740261
    Mobile: +972-(0)58-888377


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Natty Gur, Nov 3, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Mauldin
    Replies:
    0
    Views:
    383
    Steve Mauldin
    Aug 27, 2004
  2. Vadivel Kumar

    Performance of CollectionBase class

    Vadivel Kumar, Feb 15, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    1,518
    Vadivel Kumar
    Feb 17, 2005
  3. Web Team @ Borough of Poole

    Returning CollectionBase from WebService

    Web Team @ Borough of Poole, Apr 27, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,316
    Web Team @ Borough of Poole
    Apr 27, 2005
  4. Øyvind Isaksen
    Replies:
    1
    Views:
    1,024
    Øyvind Isaksen
    May 18, 2007
  5. Chad Scharf
    Replies:
    3
    Views:
    944
    Chad Scharf
    Oct 18, 2007
Loading...

Share This Page