true false

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1038 Views - Last Post: 28 February 2012 - 09:02 AM Rate Topic: -----

#1 The Dream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-February 12

true false

Posted 27 February 2012 - 07:15 AM

Hello

        private bool ReadAndValidateName(out string name)
        {
            name = "";
            if (txtName.Text.Length > 0)
            {
                name = txtName.Text;
                return true;
            }
           else
            {
           MessageBox.Show("Enter Letters Only", "Invalid Character", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtName.Focus();
                return false;
            }   
        }

        private bool ReadAndValidatePrice(out double price)
        {
            price = 0;
            double converted;
            converted = Convert.ToDouble(txtPrice.Text);

            if (converted >= 0.0)
            {
                price = Double.Parse(txtPrice.Text);
                return true;
            }
            else
            {
                MessageBox.Show("Enter Numbers Only", "Invalid Character", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPrice.Focus();
                return false;
            }
        }
          
        private bool ReadAndValidateInput(out string name ,out double price)
        {
            return ReadAndValidateName(out name) & ReadAndValidatePrice(out price);
            
        }


This is what i get but i need help whit ReadAndValidateInput i need to return if both method is true i need to return troue else return false. I know that is going to be a if and else but what do i need to get in to it to do it?

Is This A Good Question/Topic? 0
  • +

Replies To: true false

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: true false

Posted 27 February 2012 - 07:26 AM

So what is happening when the code is executed? Looking at the code, it looks fine. Your return statement in ReadAndValidateInput seems right.

Also, your validation in ReadAndValidatePrice is going to throw an exception if the user doesn't put in a number. You should really look to using double.TryParse instead.
Was This Post Helpful? 0
  • +
  • -

#3 The Dream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-February 12

Re: true false

Posted 27 February 2012 - 07:36 AM

Yes its fine but i want it when i press the butten a error message will com up if i have not enter any thing in textbox1 and textbox 2 thats way i need a if and else statement in ReadAndValidateInput.
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: true false

Posted 27 February 2012 - 07:43 AM

Again, looking at your code, that should be exactly what happens...at least for the Name. The app will crash if you don't put anything in the Price textbox.

So what does the current code do if you don't put anything the Name textbox?

I guess you would need something like this..

private void btnValidate_Click(object sender, EventArgs e)
{
    string name = string.Empty;
    double price = 0;

    if (ReadAndValidateInput(out name, out price))
    {
        MessageBox.Show("Data is good");
    }
}


Was This Post Helpful? 0
  • +
  • -

#5 The Dream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-February 12

Re: true false

Posted 27 February 2012 - 07:57 AM

i have com this far in to my windows form and can you help me out how do i use the bool in to the code?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace booking
{
    public partial class MainForm : Form
    {
        private double revenue = 0.0;
        private const int totalNumOfSeats = 240;
        private int numOfReservedSeats = 0;
        public MainForm()
        {
            InitializeComponent();
            InitializeGUI();

        }
        private void InitializeGUI()
        {
            rbtnReserve.Checked = true; 
            lstSeats.Items.Clear();
            txtName.Text = string.Empty;
        }
        private bool ReadAndValidateName(out string name)
        {
            name = "";
            if (txtName.Text.Length > 0)
            {
                name = txtName.Text;
                return true;
            }
           else
            {
           MessageBox.Show("Enter Letters Only", "Invalid Character", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtName.Focus();
                return false;
            }   
        }

        private bool ReadAndValidatePrice(out double price)
        {
            price = 0;
            double converted;
            converted = Convert.ToDouble(txtPrice.Text);

            if (converted >= 0.0)
            {
                price = Double.Parse(txtPrice.Text);
                return true;
            }
            else
            {
                MessageBox.Show("Enter Numbers Only", "Invalid Character", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPrice.Focus();
                return false;
            }
        }
          
        private bool ReadAndValidateInput(out string name ,out double price)
        {
            return ReadAndValidateName(out name) & ReadAndValidatePrice(out price);
            
        }
        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void txtPrice_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string costumerName = string.Empty;
            double seatPrice = 0.0;

            bool inputOk = ReadAndValidateInput(out costumerName, out seatPrice);

            if (inputOk)
            {
                numOfReservedSeats++;
                revenue += seatPrice;

            }
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {

        }

        private void lstSeats_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnOK_Click_1(object sender, EventArgs e)
        {

            lstSeats.Items.Add(string.Format("\t\t{0} \t{1}",txtPrice.Text, txtName.Text));
	       
        }

        private void txtPrice_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void rbtnReserve_CheckedChanged(object sender, EventArgs e)
        {
            txtName.Enabled = true;
            txtPrice.Enabled = true;
            btnOK.Enabled = true;
        }

        private void rbtnCancel_CheckedChanged(object sender, EventArgs e)
        {
            txtName.Enabled = false;
            txtPrice.Enabled = false;
            btnOK.Enabled = false;
        }
 
    }
}



Sorry if i ask to too much but im a really bad on this windows form
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: true false

Posted 27 February 2012 - 08:11 AM

This looks perfect.

        private void btnOK_Click(object sender, EventArgs e)
        {
            string costumerName = string.Empty;
            double seatPrice = 0.0;

            bool inputOk = ReadAndValidateInput(out costumerName, out seatPrice);

            if (inputOk)
            {
                numOfReservedSeats++;
                revenue += seatPrice;

            }
        }



You are using it exactly how you should. So what is the problem?
Was This Post Helpful? 0
  • +
  • -

#7 The Dream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-February 12

Re: true false

Posted 27 February 2012 - 08:16 AM

when i click th butten the error message dosent com up ot the text from my 2 textboxes dosent go in to my listbox
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,591
  • Joined: 16-October 07

Re: true false

Posted 27 February 2012 - 08:37 AM

return ReadAndValidateName(out name) & ReadAndValidatePrice(out price);



In C#, "&" is not a boolean and, "&&" is.
Was This Post Helpful? 0
  • +
  • -

#9 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: true false

Posted 27 February 2012 - 08:42 AM

View Postbaavgai, on 27 February 2012 - 11:37 AM, said:

return ReadAndValidateName(out name) & ReadAndValidatePrice(out price);



In C#, "&" is not a boolean and, "&&" is.


"&" means that if the first expression returns false, the second expression isn't evaluated.
"&&" means that both expressions will be evalutated.

http://msdn.microsof...cdk(VS.80).aspx
Was This Post Helpful? 0
  • +
  • -

#10 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: true false

Posted 27 February 2012 - 08:51 AM

View PostThe Dream, on 27 February 2012 - 11:16 AM, said:

when i click th butten the error message dosent com up ot the text from my 2 textboxes dosent go in to my listbox


What text do you put in the textboxes?

Why would you think the data would go into the ListBox? You don't have code in the OK button to do that.
Was This Post Helpful? 0
  • +
  • -

#11 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4431
  • View blog
  • Posts: 7,702
  • Joined: 08-June 10

Re: true false

Posted 27 February 2012 - 09:02 AM

View Posteclipsed4utoo, on 27 February 2012 - 09:42 AM, said:

View Postbaavgai, on 27 February 2012 - 11:37 AM, said:

return ReadAndValidateName(out name) & ReadAndValidatePrice(out price);



In C#, "&" is not a boolean and, "&&" is.


"&" means that if the first expression returns false, the second expression isn't evaluated.
"&&" means that both expressions will be evalutated.

http://msdn.microsof...cdk(VS.80).aspx


Actually, you've got that backwards.

static void Main(string[] args) {
    
    Console.WriteLine("using &&");
    var x = DoSomething(false, "1") && DoSomething(true, "2");

    Console.WriteLine("\nusing &");
    var y = DoSomething(false, "3") & DoSomething(true, "4");
    Console.ReadKey();
}

public static bool DoSomething(bool b, string message) {
    Console.WriteLine(message);
    return b;
}



Quote

using &&
1

using &
3
4


The other difference is that when the & operator is used against numeric datatypes, it's a bitwise and.
Was This Post Helpful? 1
  • +
  • -

#12 The Dream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-February 12

Re: true false

Posted 27 February 2012 - 09:04 AM

i have this code to get the info in to the list box lstSeats.Items.Add(string.Format("\t\t{0} \t{1}",txtPrice.Text, txtName.Text)); but i think i need to use it in bool inputOk = ReadAndValidateInput(out costumerName, out seatPrice);and remove the click1? but how can i do it sorry im a noob in this
Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,591
  • Joined: 16-October 07

Re: true false

Posted 27 February 2012 - 10:54 AM

View Posteclipsed4utoo, on 27 February 2012 - 11:42 AM, said:

View Postbaavgai, on 27 February 2012 - 11:37 AM, said:

return ReadAndValidateName(out name) & ReadAndValidatePrice(out price);



In C#, "&" is not a boolean and, "&&" is.


"&" means that if the first expression returns false, the second expression isn't evaluated.
"&&" means that both expressions will be evalutated.

http://msdn.microsof...cdk(VS.80).aspx


Sorry, this is completely wrong.

In a logical AND (&&), the second will never be evaluated. A bitwise AND (&) is just combining bits. A bitwise AND preforms an operation, combining the bits of the elements involved. http://msdn.microsof...c(v=vs.80).aspx

C# has overloaded the & operator to behave like a && with bool types. So & and && are identical for boolean (in C#)! However, it's still a poor choice when you intended a boolean operation. Also, in most other languages this trick isn't done so being in the habit of it will burn you.
Was This Post Helpful? 0
  • +
  • -

#14 The Dream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-February 12

Re: true false

Posted 27 February 2012 - 11:16 AM

View Posteclipsed4utoo, on 27 February 2012 - 08:51 AM, said:

View PostThe Dream, on 27 February 2012 - 11:16 AM, said:

when i click th butten the error message dosent com up ot the text from my 2 textboxes dosent go in to my listbox


What text do you put in the textboxes?

Why would you think the data would go into the ListBox? You don't have code in the OK button to do that.


private void btnOK_Click(object sender, EventArgs e) this is where i whant it but i can not get it in to it only if i get a private void btnOK_Click_1(object sender, EventArgs e)
Was This Post Helpful? 0
  • +
  • -

#15 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4431
  • View blog
  • Posts: 7,702
  • Joined: 08-June 10

Re: true false

Posted 27 February 2012 - 11:39 AM

View Postbaavgai, on 27 February 2012 - 11:54 AM, said:

So & and && are identical for boolean (in C#)!


Actually, that's not correct either, proven by the link you provided.

Quote

The operation

x && y


corresponds to the operation

x & y


except that if x is false, y is not evaluated, because the result of the AND operation is false no matter what the value of y is.


Take a look at my previous reply, and you'll see that when used with boolean arguments, the single & operator does not use short circuit evaluation.

Apparently, & is a versatile operator. It's the address-of operator when used as unary (and in an unsafe context only), a bitwise AND, and a non-short-circuit logical AND.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2