ChewDoggie
Programmer
Hello All,
I'm a newbie and am trying to get my head around Generic Collections. This is just practice, so no biggy on the response times. My Question: how do I retrieve a particular Adult from the following Adults list ?
I've created this first generic class called Adults (plural):
Then I created this other class called Adult (singular):
And the following lines of code produce a Message Box that says "52"...which is correct. There are 52 objects being added to the Adults object. As you can see, I'm calling the non-default contructor for the Adult class and for the purposes of this exercise, I'm not Disposing the Adult object each time I create it.
I'm using List as my vehicle of choice. If anyone can shed some more light on a better choice, I'm all eyes.
Thanks !
Chew
10% of your life is what happens to you. 90% of your life is how you deal with it.
I'm a newbie and am trying to get my head around Generic Collections. This is just practice, so no biggy on the response times. My Question: how do I retrieve a particular Adult from the following Adults list ?
I've created this first generic class called Adults (plural):
Code:
using System;
using System.Collections.Generic;
using System.Linq;
namespace MyCollections
{
public class Adults<T> : IEnumerable<T>, ICollection<T>
{
private List<T> TheseAdults = new List<T>();
public Adult GetAdult( int Memberid )
{
// This Method is unfinished
Adult A = new Adult();
return A;
}
public Adults()
{
}
#region IEnumerable<T> Members
public IEnumerator<T> GetEnumerator()
{
throw new NotImplementedException();
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
#endregion
#region ICollection<T> Members
public void Add( T item )
{
TheseAdults.Add( item );
}
public void Clear()
{
for ( int i = 0;i <= TheseAdults.Count - 1;i++ )
{
TheseAdults.Remove( TheseAdults[i] );
}
}
public bool Contains( T item )
{
return TheseAdults.Contains( item );
}
public void CopyTo( T[] array, int arrayIndex )
{
TheseAdults.CopyTo( array, arrayIndex );
}
public int Count
{
get
{
return TheseAdults.Count;
}
}
public bool IsReadOnly
{
get
{
return false;
}
}
public bool Remove( T item )
{
return TheseAdults.Remove( item );
}
#endregion
}
}
Then I created this other class called Adult (singular):
Code:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
namespace MyCollections
{
public class Adult
{
private string fname;
private string lname;
private string addr;
private string city;
private string state;
private string phone;
private DateTime expdate;
private string ConnStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Program Files\Microsoft SQL Server\MSSQL10.CHEWDOGGIE\MSSQL\DATA\library.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True";
public string City
{
get
{
return city;
}
set
{
city = value;
}
}
public string FirstName
{
get
{
return fname;
}
set
{
fname = value;
}
}
public string LastName
{
get
{
return lname;
}
set
{
lname = value;
}
}
public string Address
{
get
{
return addr;
}
set
{
addr = value;
}
}
public string State
{
get
{
return state;
}
set
{
state = value;
}
}
public string PhoneNumber
{
get
{
return phone;
}
set
{
phone = value;
}
}
public DateTime ExpirationDate
{
get
{
return expdate;
}
set
{
expdate = value;
}
}
public Adult()
{
}
public Adult(int MemberID)
{
try
{
using ( SqlConnection cnn = new SqlConnection( ConnStr ) )
{
using ( SqlCommand cmd = new SqlCommand( "GetMemberFromMemberid", cnn ) )
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( "@membernum", MemberID );
cnn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while ( reader.Read() )
{
this.FirstName = reader["FName"].ToString();
this.LastName = reader["LName"].ToString();
this.Address = reader["Addr"].ToString();
this.City = reader["City"].ToString();
this.State = reader["State"].ToString();
this.PhoneNumber = reader["Phone"].ToString();
this.ExpirationDate = Convert.ToDateTime( reader["ExpirationDate"].ToString() );
}
}
}
}
catch ( Exception ex )
{
throw new Exception( ex.Message );
}
}
}
}
And the following lines of code produce a Message Box that says "52"...which is correct. There are 52 objects being added to the Adults object. As you can see, I'm calling the non-default contructor for the Adult class and for the purposes of this exercise, I'm not Disposing the Adult object each time I create it.
Code:
private void Form1_Load( object sender, EventArgs e )
{
Adults<Adult> AdultList = new Adults<Adult>();
for ( int x = 1;x <= 100;x++ )
{
Adult A = new Adult( x );
if ( A.LastName != null )
{
AdultList.Add( A );
}
}
MessageBox.Show( AdultList.Count.ToString() );
}
I'm using List as my vehicle of choice. If anyone can shed some more light on a better choice, I'm all eyes.
Thanks !
Chew
10% of your life is what happens to you. 90% of your life is how you deal with it.