简体   繁体   中英

Trouble calling a method from a class

Can anybody help me with this?: I'm trying call a method from a my class "numbers" to show that if the entered number is over 50, on button click a message box shows displaying "high" but if it's below 50 it displays "low". I can't figure out what i'm doing wrong here. This is the code from my class:

    private int number; 
    private string getNumber(int num)
    {

        number = num;
        return number.ToString();
    }

        public int numProperty
    {
        get { return number; }
        set { number = value; }
    }

     public void isHighorlow()
        {    
            if (number >=50)
            {

            }
            else
            {
                return;
            }

        }

Note: the int "number" is property that gets it's value from a text box too. & here is the code from my form:

     numbers info = new numbers();
     private void Btn_Click(object sender, EventArgs e)
    { 
      info.numProperty = Convert.ToInt32(numberBOX.Text);
      info.isHighorlow = Messagebox.Show = ("High");
    }

I know that I've not added the "low" bit yet because i'm still trying to see how this works. Sorry if it seems confusing as i'm still learning c#. I get the error message: cannot assign isHighorlow because it's part of a method group. And I also realise it's much easier if I just do an if statement on the textbox, but I'm practising Classes and Methods so I'm trying to do it this way. thanks.

I'm guessing you want something like this:

public string isHighorlow(int number)
{    
    if (number >=50)
    { 
         return "High";
    }
    else
    {
         return "Low";
    }
}


numbers info = new numbers();
private void Btn_Click(object sender, EventArgs e)
{ 
  Messagebox.Show(info.isHighorlow(Convert.ToInt32(numberBOX.Text)))
}

isHighorLow is a method, not a property.

MessageBox.Show is a method.

Not sure what you are trying to do but it should be :

 if(info.isHigh(Convert.ToInt32(numberBox.Text)))
   Messagebox.Show("High");
  else
    Messagebox.Show("Low");

Meaning you have a method isHigh like so:

public bool isHigh()
{
   return number>=50
}

Try to change your code this way:

private int _number; 

private string GetNumber(int number)
{
    _number = number;
    return number .ToString();
}

public int Number
{
    get { return _number; }
    set { _number = value; }
}

public string IsHigh()
{   
    get { if (number >= 50) return true; }
}

numbers info = new numbers();
private void Btn_Click(object sender, EventArgs e)
{ 
    info.Number = Convert.ToInt32(numberBOX.Text);
    MessageBox.Show(info.IsHigh ? "High" : "Low");
}

In your class you have defined void isHighorlow() .
This means that you have a method that returns nothing.
Of course such method cannot be used on the left part of an expression like you have done.

Probably you want to write in your class

public bool isHighorlow() 
{     
    if (number >=50) 
    { 
        return true;
    } 
    else 
    { 
        return false; 
    } 

}

in this way you declare a method that return True if the internal value is >= 50 or false otherwise.
Now, in your form you can use the method in this way

Messagebox.Show(info.isHighorlow() ? "High" : "Low"); 

However, if the requirement is simply to return a flag for true or false, it is better to use a read only property changing the class code in this way

public bool isHighorlow() 
{     
    get
    {
        return (number >=50 ? true : false);
    } 
    // No set, read only
} 

IsHighOrLow should be as follows

    public bool isHighorlow()
    {    
        if (number >=50)
        {

           return true;
        }
        else
        {
            return false;
        }

    }

And in button click

if (info.isHighorlow){
//say high
} else
{
// say low
}

(disclaimer: double check boolean and associated constants per C#)

isHighOrLow doesn't do anything at all. Perhaps this would be better:

     public boolean isHigh()
        {    
            if (number >=50)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

Or, more concisely:

     public boolean isHigh()
        {    
            return number >=50;
        }

When you call it, this might be closer to what you need:

     numbers info = new numbers();
     private void Btn_Click(object sender, EventArgs e)
    { 
      info.numProperty = Convert.ToInt32(numberBOX.Text);
      if (info.isHigh())
      {
        Messagebox.Show("High");
      }
      else
      {
        Messagebox.Show("Low");
      }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM