Thread: trouble converting string to int

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    20

    trouble converting string to int

    Code:
     { // check if valid pair
                        
                        { 
                           jw.paired [0] = jw.siredata[x,2];
                           jw.paired [1] = jw.siredata[x,3];} 
    
    
                       
                            // Convert array paired to string
                        
                                StringBuilder builder = new StringBuilder();
                                foreach (int val in jw.paired)
                                {
                                    builder.Append(val);
                                    
                                }
                             
                       
                            // int lnk = builder;
                            int value2;
                            int.TryParse(val, out value2);
                           //  int value2 = Int32.Parse(builder);
    
    
                            if ((value2) == (jw.unlinked1)) {random_module();}
                            else 
                                if ((value2) == (jw.unlinked2))  {random_module();}
                                else
                                {
    
    
                                    jw.siredata[x, 3] = jw.linkedpairs[num, 0];
                                    jw.siredata[x, 4] = jw.linkedpairs[num, 1];
                                }
    I'm getting the error The name 'val' does not exist in the current context.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Uh, because val only exists within the context of your foreach loop. Also int.TryParse expects a String as the first parameter. val is not a String.

    Didn't you mean this:

    Code:
    int.TryParse(builder.ToString(), out value2);

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    20
    Thank you. That solved the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with program converting decimal to binary
    By simplyxsweet in forum C Programming
    Replies: 2
    Last Post: 10-03-2011, 10:04 PM
  2. Having trouble converting a variable
    By KasMage in forum C++ Programming
    Replies: 6
    Last Post: 07-14-2008, 03:43 PM
  3. converting string to int back to string
    By thenson in forum C Programming
    Replies: 4
    Last Post: 02-03-2008, 11:21 AM
  4. having trouble converting the following formula
    By Axel in forum C Programming
    Replies: 7
    Last Post: 10-18-2005, 04:39 AM
  5. C++: Converting Numeric String to Alpha String
    By JosephCardsFan in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2005, 07:07 AM