collection list count (CollectionBase) list.add - problems

  • Thread starter Thread starter jason
  • Start date Start date
J

jason

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.
 
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
 
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?
 
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
 
Back
Top