cs_baby85
Messages postés79Date d'inscriptionmercredi 22 octobre 2008StatutMembreDernière intervention17 juin 2009
-
22 avril 2009 à 20:46
cs_baby85
Messages postés79Date d'inscriptionmercredi 22 octobre 2008StatutMembreDernière intervention17 juin 2009
-
28 avril 2009 à 16:15
bonsoir,
j'ai un probleme au niveau de .length
j ai fais une methode qui prend comme parmetre une variable string
public string getstrg(string tx)
{
string[] s = new string[tx.Length];
...
}
lorsque j'ai l appel de cette methde elle marche bien mais mon probleme est lorsque j e donne comme parametre d'entrée un element de tableau string
for (int p = 0; p < tab_nm.Length; p++)
{
tab_nmv[p] = gt.getstrg(tab_nm[p]);
..
}
il m affiche cette message d'erreur Object reference not set to an instance of an object au niveau tab_nmv[p] = gt.getstrg(tab_nm[p]) et string[] s = new string[tx.Length];
j attends votre réponse
Merci d'avance
A voir également:
C# instanceof
Instanceof c# - Meilleures réponses
Object reference not set to an instance of an object.' c# - Meilleures réponses
cs_Bidou
Messages postés5487Date d'inscriptiondimanche 4 août 2002StatutMembreDernière intervention20 juin 201361 23 avril 2009 à 08:15
Le code est vraiment pas très beau
Tu fais quoi si tu reçois plus de 100 enregistrements? Avec des tableaux initialisés à cette taille (100) tu risques d'avoir des problèmes...
=> Utilises List<string> à la place.
Quand on utilise un reader, il faut ABSOLUMENT le fermer!
=> reader.Close()
Pour terminer, ton erreur est normale et vient de l'utilisatation des tableaux. Quand tu initialises un tableau de string, il insère tout des null dedans. Ta méthode va remplir les x premiers mais les autres seront toujours null. Du coup, quand tu fais un ToString dessus, ça coince (cela dit en passant, le ToString ne sert à rien dans ce cas).
=> En utilisant la List<string> comme expliqué ci-dessus, ça devrait régler ton problème.
cs_baby85
Messages postés79Date d'inscriptionmercredi 22 octobre 2008StatutMembreDernière intervention17 juin 2009 22 avril 2009 à 21:51
bonsoir,
nn car gt est une instantiation de classe
Get gt=new Get();
for (int p = 0; p < tab_nm.Length; p++)
{
tab_nmv[p] = gt.getstrg(tab_nm[p]);
..
}
le probleme lorsue j'ai ttester ma methode avec une chaine de caractere il marche bien mais quand j ai le teste avec un element du tableau de type string il m affiche une message d'erreur
cs_baby85
Messages postés79Date d'inscriptionmercredi 22 octobre 2008StatutMembreDernière intervention17 juin 2009 22 avril 2009 à 22:45
ici je fais l appel a la methode getstrg(tab_nm[p].ToString()
public bool test_nom_Ap(string nm_p)
{
string[] tab_nm = new string[100];
string[] tab_nmv = new string[100];
Get gt=new Get();
int h = 0;
string req = "select Nom_Ap from serveur";
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand oCommand = new SqlCommand(req, connection);
connection.Open();
bool b=false;
oCommand.CommandText = req;
SqlDataReader reader = oCommand.ExecuteReader();
while (reader.Read())
{
string result1 = reader["Nom_Ap"].ToString();
tab_nm[h] = result1;
h++;
}
for (int p = 0; p < tab_nm.Length-1; p++)
{
tab_nmv[p] = gt.getstrg(tab_nm[p].ToString());
if (nm_p==tab_nmv[p])
{
b= false;
break;
}
else b=true;
}
return b;
}
et voila ma methode getstrg
public string getstrg(string tx)
{
string[] s = new string[tx.Length];
string z;
for (int i = 0; i < tx.Length; i++)
{string ss=tx.Substring(i, 1);
if(ss!=" ")
{
s[i] = ss;
}
}
z =string.Join("", s);
return z;
}
sachant que le tableau tab_nm n'est pas null et tab_nmv est null car il n as pa pu le remplir