Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-17-2004, 02:44 AM   PM User | #1
Unknown
New Coder

 
Join Date: Jan 2004
Location: Illinois
Posts: 72
Thanks: 2
Thanked 0 Times in 0 Posts
Unknown is an unknown quantity at this point
C#, need Help

I'm trying to compare a input by the user with a data filed with in an object. Here is the swtich statment.

Code:
			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:
Code:
			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:
Code:
C:\Documents and Settings\***\My Documents\Visual Studio Projects\RNA\Class1.cs(56): A constant value is expected

Last edited by Unknown; 03-17-2004 at 02:48 AM..
Unknown is offline   Reply With Quote
Old 03-17-2004, 03:52 AM   PM User | #2
Unknown
New Coder

 
Join Date: Jan 2004
Location: Illinois
Posts: 72
Thanks: 2
Thanked 0 Times in 0 Posts
Unknown is an unknown quantity at this point
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.
Unknown is offline   Reply With Quote
Old 03-17-2004, 08:28 AM   PM User | #3
shmoove
Regular Coder

 
Join Date: Dec 2003
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
shmoove is an unknown quantity at this point
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
shmoove is offline   Reply With Quote
Old 03-17-2004, 05:20 PM   PM User | #4
Unknown
New Coder

 
Join Date: Jan 2004
Location: Illinois
Posts: 72
Thanks: 2
Thanked 0 Times in 0 Posts
Unknown is an unknown quantity at this point
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?
Unknown is offline   Reply With Quote
Old 03-18-2004, 08:39 AM   PM User | #5
shmoove
Regular Coder

 
Join Date: Dec 2003
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
shmoove is an unknown quantity at this point
Code:
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
shmoove is offline   Reply With Quote
Old 03-19-2004, 03:54 AM   PM User | #6
Unknown
New Coder

 
Join Date: Jan 2004
Location: Illinois
Posts: 72
Thanks: 2
Thanked 0 Times in 0 Posts
Unknown is an unknown quantity at this point
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.
Unknown is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:33 AM.


Advertisement
Log in to turn off these ads.