bool not holding value?

aewarnick

Senior Contributor
Joined
Jan 29, 2003
Messages
1,031
Why do dots always print when the bool value should be set to true after one dot?

Code:
private void do3() 
		{
			if(TorF(TB1.Text))
			RTB2.Text=TB1.Text;
		}
	bool check2=false;
		public bool TorF(string str)
		{
			char[] chr = str.ToCharArray();
			
            
			foreach (char dummy in chr)
			{
				if (char.IsDigit(dummy)) return true;
				else if (dummy=='.'&& check2==false) {check2=true; return true;}
			}
			return false;
		}
 
The way that is coded, you are continuing the loop if it finds a dot and check2 is true. I guess if it finds another digit after that, it'll return true. What you probably want to do is add another clause to your tests, so if it finds a dot and check2 is true, to return false.
 
I tried and tried till I could try no more!! So I just changed it completely:

Code:
//do3
		public void do3() 
		{
			TB1.Focus(); TB1.SelectAll();
				check2(TB1.Text);
			RTB2.Text=y;
		}
bool b=false;
string y="";
		void check2(string text)
		{
			char[] chrarray = text.ToCharArray();
            
			foreach (char x in chrarray)
			{
				if (char.IsDigit(x)) y+=x;
				else if (x=='.' && b==false)
				{b=true; y+=x;}
			}
		}
 
What exactly are you trying to do with your do3 and check2 functions? If you're trying to validate a string, you might do better to just use regular expressions. From the function names, I can't tell anything and I'm too lazy to read through the code to see what you want to do :)

-ner
 
I'm with you. I don't feel like figuring it out either. It works now, so I am happy. Actually I have a subscripted array problem now that should be simple but is giving me trouble. I'll post that elsewhere.
 
Back
Top