Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: C#, need Help

  1. #1
    New Coder
    Join Date
    Jan 2004
    Location
    Illinois
    Posts
    72
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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.

  • #2
    New Coder
    Join Date
    Jan 2004
    Location
    Illinois
    Posts
    72
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    New Coder
    Join Date
    Jan 2004
    Location
    Illinois
    Posts
    72
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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?

  • #5
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    New Coder
    Join Date
    Jan 2004
    Location
    Illinois
    Posts
    72
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •