Problème de liste

Résolu
ellandebasque Messages postés 24 Date d'inscription mardi 11 février 2003 Statut Membre Dernière intervention 29 septembre 2010 - 16 mai 2005 à 18:12
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 - 16 mai 2005 à 18:27
Bonjour,
j'ai un
problème pour ajouter des éléments dans une collection : tousles
éléments sont égaux. Grâce au débogueur (pas-à-pas) j'ai constatéque
lors de l'ajout d'un élément tous les éléments étaient modifiés
parcelui qui est ajouté. Donc tous les éléments de ma liste sont égaux
audernier élément ajouté. Comme si les éléments de la liste
faisaientréférence à la même case mémoire ;-)


while(reader.read())
{


int id
= (int)Convert.ToInt32(reader["id_vitrine"]);

DateTime dbt;


dbt = new
DateTime(dt.Year, dt.Month,dt.Day, dt.Hour, dt.Minute, dt.Second);

DateTime fin;


fin =
(DateTime)reader["date_fin"];

DateTime locked;


locked =
(DateTime)reader["date_locked"];

int actif;


actif
=Convert.ToInt32(reader["actif"].ToString());

int css;


css =
Convert.ToInt32(reader["id_css"]);



vc.Add(new Vitrine(id, dbt, fin, locked, actif, css));
}

public int Add( Vitrine value )
{

//value = newVitrine(value.Id,
value.date_dbt, value.date_fin, value.date_locked,value.actif,
value.css);
//return (List.Add( value ));

return(List.Add(new
Vitrine(value.Id, value.date_dbt, value.date_fin,value.date_locked,
value.actif, value.css)));
}

Merci
d'avoir pris le temps de me lire. Si vous avez une solution ouune idée,
je vous en remercie par avance de me laisser un petit message.


------------ code complet --------------------



Le premier bout de code se trouve dans Vitrine.cs dans la focntion GetAllVitrines()



Vitrine.cs



using System;

using System.Collections;

using System.Data.SqlClient;



namespace ManagerVitrines.Classes

{

//*********************************************************************

//

// Classe Vitrine

//

// Represente une Vitrine

//

//*********************************************************************



public class Vitrine

{



/*** PRIVATE FIELDS ***/



private static int _id_vitrine;

private static DateTime _date_dbt;

private static DateTime _date_fin;

private static DateTime _date_locked;

private static int _actif;

private static int _id_css;



private static Global gl = new Global();



/*** CONSTRUCTEUR ***/



public Vitrine(int id,
DateTimedate_dbt, DateTime date_fin, DateTime date_locked, int iActif,
intiCss)

{

// Validate Mandatory Fields//

if (id < 0 || id.ToString() = = null)

throw (new ArgumentOutOfRangeException("id"));



_id_vitrine = id;

_date_dbt = date_dbt;

_date_fin = date_fin;

_date_locked = date_locked;

_actif = iActif;

_id_css = iCss;

}



public Vitrine()

{}



/*** PROPRIETES ***/



public int Id

{

get {return _id_vitrine;}

}





public DateTime date_dbt

{

get

{


if (_date_dbt.ToString() == null
||_date_dbt.ToString().Length==0)

return DateTime.Now;

else

return _date_dbt;

}

set {_date_dbt = value;}

}





public DateTime date_fin

{

get

{


if (_date_fin.ToString() == null
||_date_fin.ToString().Length==0)

return DateTime.Now;

else

return _date_fin;

}

set {_date_fin = value;}

}



public DateTime date_locked

{

get

{


if (_date_locked.ToString() ==
null ||_date_locked.ToString().Length==0)

return DateTime.Now;

else

return _date_locked;

}

set {_date_locked = value;}

}



public int actif

{

get {return _actif;}

set {_actif = value;}

}



public int css

{

get {return _id_css;}

set {_id_css = value;}

}



/*** INSTANCE METHOD ***/



public bool Delete()

{

if (gl.connect2sql(gl.connDev))

{


string strReq = "delete from
v_vitrine (nolock)where id_vitrine = " + _id_vitrine;


string strReturnValue
=gl.connDev.excuteCommand(strReq);

if (strReturnValue == "true")

return true;

else

return false;

}

else

return false;

}



public bool Save()

{

if(gl.connect2sql(gl.connDev))

{

string strReq = "update v_vitrine";

strReq +" set id_css '" + _id_css + "',";

strReq +" set date_dbt '" + _date_dbt + "',";

strReq +" set date_fin '" + _date_fin + "',";


strReq +" set date_locked '" +
_date_locked +"',";

strReq +" set actif '" + _actif + "'";

strReq +" where id_vitrine '" + _id_vitrine +"'";


string strReturnValue
=gl.connDev.excuteCommand(strReq);

if (strReturnValue == "true")

return true;

else

return false;

}

else

return false;

}





/*** STATIC METHODS ***/



public static bool DeleteVitrine (int iVitrineId)

{

bool bValue = false;

if (iVitrineId > 0)

{


string strReq = "delete from
v_vitrine (nolock)where id_vitrine = " + iVitrineId.ToString();

}



return bValue;

}





public static VitrineCollection GetAllVitrines()

{

VitrineCollection vc = new VitrineCollection();

if(gl.connect2sql(gl.connDev))

{

string strReq = "select * from v_vitrine";


SqlDataReader reader
=gl.connDev.executeCommandReader(strReq);

int iCompteur = 0;

while(reader.Read())

{


int id
=(int)Convert.ToInt32(reader["id_vitrine"]);



DateTime dbt;


if
(reader["date_dbt"].ToString()!= "")

{



DateTime dt =Convert.ToDateTime(reader["date_dbt"]);



dbt = newDateTime(dt.Year, dt.Month, dt.Day,
dt.Hour, dt.Minute, dt.Second);

}

else



dbt = newDateTime(1999, 01, 01, 0, 0, 0, 0);



DateTime fin;


if
(reader["date_fin"].ToString()!= "")



fin =(DateTime)reader["date_fin"];

else



fin = newDateTime(1999, 01, 01, 0, 0, 0, 0);



DateTime locked;



if(reader["date_locked"].ToString() != "")



locked =(DateTime)reader["date_locked"];

else



locked = newDateTime(1999, 01, 01, 0, 0, 0, 0);



int actif;


if
(reader["actif"].ToString() !="")



actif =Convert.ToInt32(reader["actif"].ToString());

else

actif = -1;



int css;


if
(reader["id_css"].ToString()!= "")



css =Convert.ToInt32(reader["id_css"]);

else

css = -1;




vc.Add(new
Vitrine(id, dbt, fin,locked, actif, css));



//vc.Add(Convert.ToInt32(reader["id_vitrine"]),Convert.ToDateTime(reader["date_dbt"]),Convert.ToDateTime(reader["date_fin"]),Convert.ToDateTime(reader["date_locked"]),Convert.ToInt32(reader["actif"].ToString()),Convert.ToInt32(reader["id_css"].ToString()));

iCompteur++;

}

reader.Close();

return vc;

}

else

return null;

}





public static Vitrine GetVitrineById(int iVitrineId)

{

if (gl.connect2sql(gl.connDev))

{

try

{


string strReq "select * fromv_vitrine (nolock) where id_vitrine '" + iVitrineId +
"'";


SqlDataReader
reader =gl.connDev.executeCommandReader(strReq);

reader.Read();


Vitrine v = newVitrine((int)reader["id_vitrine"],
(DateTime)reader["date_dbt"],(DateTime)reader["date_fin"],
(DateTime)reader["date_locked"],(int)reader["actif"],
(int)reader["id_css"]);

reader.Close();

return v;

}

catch(Exception e)

{

Console.WriteLine(e.Message);

return null;

}

}

else

return null;

}



/*

public static VitrineCollection GetVitrineByMemberUsername(string username)

{



}



public static bool RemoveUserFromVitrine(int userId, int VitrineId)

{



}

*/



public static bool CloneVitrine(int VitrineId, string VitrineName)

{

if (gl.connect2sql(gl.connDev))

{


string strReq = "insert into
v_vitrine (id_css,date_dbt, date_fin, date_locked, actif)";

strReq += " values ";

strReq += "('" + _id_css + "'";

strReq += ",'" + _date_dbt + "'";

strReq += ",'" + _date_fin + "'";

strReq += ",'" + _date_locked + "'";

strReq += ",'" + _actif + "')";


string strReturnValue
=gl.connDev.excuteCommand(strReq);

if (strReturnValue == "true")

return true;

else

return false;

}

else

return false;

}

}

}



VitrineCollection.cs



using System;

using System.Collections;

//using ASPNET.StarterKit.IssueTracker.DataAccessLayer;



namespace ManagerVitrines.Classes

{

//*********************************************************************

//

// Classe VitrineCollection

//

//*********************************************************************



public class VitrineCollection : CollectionBase

{

public enum VitrineFields

{

id_vitrine,

date_dbt,

date_fin,

date_locked,

actif,

type

}





public void Sort(VitrineFields sortField, bool isAscending)

{

switch (sortField)

{

case VitrineFields.date_dbt:



InnerList.Sort(newdate_dbtComparer());

break;

case VitrineFields.date_fin:



InnerList.Sort(newdate_finComparer());

break;

case VitrineFields.date_locked:



InnerList.Sort(newdate_lockedComparer());

break;

case VitrineFields.actif:



InnerList.Sort(newactifComparer());

break;

}

if (!isAscending) InnerList.Reverse();

}





private sealed class date_dbtComparer : IComparer

{

public int Compare(object x, object y)

{

Vitrine first = (Vitrine) x;

Vitrine second = (Vitrine) y;

return first.date_dbt.CompareTo(second.date_dbt);

}

}





private sealed class date_finComparer : IComparer

{

public int Compare(object x, object y)

{

Vitrine first = (Vitrine) x;

Vitrine second = (Vitrine) y;

return first.date_fin.CompareTo(second.date_fin);

}

}





private sealed class date_lockedComparer : IComparer

{

public int Compare(object x, object y)

{

Vitrine first = (Vitrine) x;

Vitrine second = (Vitrine) y;



returnfirst.date_locked.CompareTo(second.date_locked);

}

}



private sealed class actifComparer : IComparer

{

public int Compare(object x, object y)

{

Vitrine first = (Vitrine) x;

Vitrine second = (Vitrine) y;

return first.actif.CompareTo(second.actif);

}

}



public Vitrine this[ int index ]

{

get { return( (Vitrine) List[index] );}

set { List[index] = value;}

}



// ne fonctionne pas : passage par référence --> pb

// faut passer la vitrine par valeur

public int Add( Vitrine value )

{

//value =
newVitrine(value.Id, value.date_dbt, value.date_fin,
value.date_locked,value.actif, value.css);

return (List.Add( value ));


//return(List.Add(new Vitrine(value.Id, value.date_dbt,
value.date_fin,value.date_locked, value.actif, value.css)));

}



public int Add(int iId,
DateTimedtdate_dbt, DateTime dtdate_fin, DateTime dtdate_locked, int
iactif,int icss)

{

Vitrine v =new
Vitrine(iId, dtdate_dbt, dtdate_fin, dtdate_locked, iactif, icss);

return List.Add(v);

}



public int IndexOf( Vitrine value )

{

return( List.IndexOf( value ) );

}



public void Insert( int index, Vitrine value )

{

List.Insert( index, value );

}



public void Remove( Vitrine value )

{

List.Remove( value );

}



public bool Contains( Vitrine value )

{

// If value is not of type Vitrine, this will return false.

return( List.Contains( value ) );

}



protected override void OnInsert( int index, Object value )

{

if
(value.GetType() != Type.GetType("ManagerVitrines.Classes.Vitrine") )


throw new ArgumentException(
"value must be of typeVitrine.", "value" );

}



protected override void OnRemove( int index, Object value )

{

if
(value.GetType() != Type.GetType("ManagerVitrines.Classes.Vitrine") )


throw new ArgumentException(
"value must be of typeVitrine.", "value" );

}



protected override void OnSet( int index, Object oldValue, Object newValue )

{

if
(newValue.GetType() != Type.GetType("ManagerVitrines.Classes.Vitrine") )


throw new ArgumentException(
"newValue must be oftype Vitrine.", "newValue" );

}



protected override void OnValidate( Object value )

{

if
(value.GetType() != Type.GetType("ManagerVitrines.Classes.Vitrine") )


throw new ArgumentException(
"value must be of typeVitrine." );

}

}

}


De

1 réponse

cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
16 mai 2005 à 18:27
Salut,

public class Vitrine
{


/*** PRIVATE FIELDS ***/


private static int _id_vitrine;
private static DateTime _date_dbt;
private static DateTime _date_fin;
private static DateTime _date_locked;
private static int _actif;
private static int _id_css;

Les champs statiques sont communs à toutes les instances de la classe Vitrine, donc à chaque fois que tu le modifie c'est pour toutes les instances :-)

Cocoricoooooooo !!!!
coq
MVP Visual C#
3
Rejoignez-nous