...

View Full Version : C#, need Help



Unknown
03-17-2004, 02:44 AM
I'm trying to compare a input by the user with a data filed with in an object. Here is the swtich statment.



string Icodon = Console.ReadLine();
for(int i=0;i < codons.Length;i++)
{
switch(Icodon)
{
case codons[i].codon1:
Console.WriteLine(codons[i].codon1);
break;
default:
Console.WriteLine("Default");
break;
}

}

here is the object, its defined:


codon methCodon = new codon("Methionine", "ATG", null, null, null,null, null);
codon[] codons = new codon[1];
codons[0]=methCodon;

I also put it in an object array, can anyone tell me whats wrong?
Here is the error when I compile it:


C:\Documents and Settings\***\My Documents\Visual Studio Projects\RNA\Class1.cs(56): A constant value is expected

Unknown
03-17-2004, 03:52 AM
Maby i wasn't clear. Is there a way to test a switch statment with an objects property? like test wether an input is equal to the name property of an object. The object is in an array.

shmoove
03-17-2004, 08:28 AM
The cases in a switch statement can't be defined by variables (like the compiler told you), just with constants. You can do it with an if..else if...else statement instead.

shmoove

Unknown
03-17-2004, 05:20 PM
if else statments will take too long. What i tried was comparing it to a property of an object but the not through the obejcts array and it worked. Is there an easier way?

shmoove
03-18-2004, 08:39 AM
string Icodon = Console.ReadLine();
for(int i=0;i < codons.Length;i++)
{
if (Icodon == codons[i].codon1) {
Console.WriteLine(codons[i].codon1);
else {
Console.WriteLine("Default");
}
}

If..else takes too long?

shmoove

Unknown
03-19-2004, 03:54 AM
Yeah, it isn't that hard. Now I have another problem. I'm trying to replace a letter or whitespace in a string using the Replace() function thats built in. Can someone please tell me how come this won't work:

string s = "Danger NoSmoking";
a.replace(s, " ", "!")


This is from a book I have and I followed that exmaple to the point and didn't recive what I wanted. I eve try that exmaple there and it didn't work.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum