if statement in textbox error - c#

Doing a gui project for course and ran into a problem with my code. I have to run a bool method that will test to make sure that the words "tropical or saltwater" are only entered in and return true if correct.
But im having problems with getting the if statement to match the string that i have given it in the expression.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private bool Fishtype()
{
if (txtFishType.Text == "Tropical" | "easy" )
{
return true;
}
else
{
return false;
}
}
private bool Fishsize()
{
if(txtFishSize >= 3 && txtFishSize <= 20)
{
return true;
}
else
{
return false;
}
}
}

I think you meant for your if statement to be:
if (txtFishType.Text == "Tropical" || txtFishType.Text == "saltwater" ) { return true; }

Related

How to replace a specific string using method?

I have problem with my code , I want to replace specific string to a new one but it doesn't work
public void InsertYahoo(TextBox sender)
{
if (IsGmail(sender))
{
ReplaceGmail(sender);
}
else if(IsYahoo(sender))
{
return;
}
else
{
sender.Text +="#yahoo.com";
}
}
public bool IsYahoo(TextBox sender)
{
if (sender.Text.Contains("#yahoo.com")
{
return true;
}
else
{
return false;
}
}
public bool IsGmail(TextBox sender)
{
if (sender.Text.Contains("#gmail.com")
{
return true;
}
else
{
return false;
}
}
public void ReplaceGmail(TextBox sender)
{
sender.Text.Replace("#gmail.com, "#yahoo.com");
}
This code what i tried , so any suggestions?
Also I tried to get the index of #gmail.com and remove it but it did not work neither
Strings are immutable, so every method in the String class does not modify the current instance but returns a new one. You have to assign this to the original variable:
sender.Text = sender.Text.Replace("#gmail.com,"#yahoo.com");
If you are interested in why strings are immutable: Why .NET String is immutable?
Something like this:
//DONE: we should check for null
//DONE: it's Yahoo if it ends on #yahoo.com (not contains)
public static bool IsYahoo(TextBox sender) =>
sender != null &&
sender.Text.TrimEnd().EndsWith("#yahoo.com", StringComparison.OrdinalIgnoreCase);
public static bool IsGmail(TextBox sender) =>
sender != null &&
sender.Text.TrimEnd().EndsWith("#gmail.com", StringComparison.OrdinalIgnoreCase);
public static void InsertYahoo(TextBox sender) {
if (null == sender)
throw new ArgumentNullException(nameof(sender));
if (IsYahoo(sender))
return;
// Uncomment, In case you want to change gmail only
//if (!IsGmail(sender))
// return;
// If we have an eMail like bla-bla-bla#somewhere
int p = sender.Text.LastIndexOf('#');
// ... we change somewhere to yahoo.com
if (p > 0)
sender.Text = sender.Text.Substring(0, p) + "#yahoo.com";
}

C# Combobox user control does not funtion as it should

I am fixing a c# project that uses a user control named CompleteComboBox.
It functions partly.
The backspace does not work.
The first time the user enters a string into the textbox part, the list opens as it should, and stands on the right line. The second time - the list does not open but it does show the matched line in the textbox part. Then, if i open the list and start writing the beginning of one of the items but then click one of the items and press enter - this item is not selected rather the item that was selected while entering letters in the text box part.
Hope I am understood.
Here is the code:
namespace BestInvest.UserControls
{
public partial class CompleteComboBox : System.Windows.Forms.ComboBox
{
public event System.ComponentModel.CancelEventHandler NotInList;
private bool _limitToList = true;
private bool _inEditMode = false;
bool start;
public CompleteComboBox()
: base()
{
start = true;
this.Text = ClsConsts.InitializeTextForCombo;
}
[Category("Behavior")]
public bool LimitToList
{
get { return _limitToList; }
set { _limitToList = value; }
}
protected virtual void OnNotInList(System.ComponentModel.CancelEventArgs e)
{
if (NotInList != null)
{
NotInList(this, e);
}
}
protected override void OnTextChanged(System.EventArgs e)
{
if (_inEditMode)
{
string input = Text;
int index = FindString(input);
if (index >= 0)
{
_inEditMode = false;
SelectedIndex = index;
_inEditMode = true;
Select(input.Length, Text.Length);
//base.DroppedDown = true;
}
}
base.OnTextChanged(e);
}
protected override void OnValidating(System.ComponentModel.CancelEventArgs e)
{
if (this.LimitToList)
{
int pos = this.FindStringExact(this.Text);
if (pos == -1)
{
OnNotInList(e);
}
else
{
this.SelectedIndex = pos;
}
}
base.OnValidating(e);
}
protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
{
if (start)
{
base.DroppedDown = true;
//else
start = false;
}
if (e.KeyCode == Keys.Enter)
{
base.DroppedDown = false;
start = true;
}
_inEditMode = true;
base.OnKeyDown(e);
}
}
}
Your code is messy. Look at the start flag. It is not set to true in all cases you would expect. That's why first time behavior is different from second time.
Your OnKeyDown event will not trigger on all keys you would expect. That's why backspace doesn't work. There are several posts about detecting backspace out there: See for example this one

WPF/C#5.0 Publish/Subscribe to an array of static fields?

Using C# 5.0, I'm creating a publish/subscribe relationship on a static field, so that I can access it from multiple pages. In the host window, I have
public enum PLCStates
{
Good,
Bad,
Disabled
};
public static class PLCSafeStates
{
public static event EventHandler testStates1Changed;
private static PLCStates _testStates1;
public static PLCStates testStates1
{
get { return _testStates1; }
set
{
if (value != _testStates1)
{
_testStates1 = value;
if (testStates1Changed != null)
testStates1Changed(null, EventArgs.Empty);
}
}
}
}
And then in the pages hosted by the window, I have things like:
public FB1()
{
InitializeComponent();
SafteyFaults.PLCSafeStates.testStates1Changed += PLCSafeStates_testStates1Changed;
}
private void PLCSafeStates_testStates1Changed(object sender, EventArgs e)
{
var test2 = SafteyFaults.PLCSafeStates.testStates1;
if (test2 == SafteyFaults.PLCStates.Bad)
{
VisualStateManager.GoToState(btnFB, "PLCBad", true);
}
if (test2 == SafteyFaults.PLCStates.Good)
{
VisualStateManager.GoToState(btnFB, "PLCGood", false);
}
}
private void btnFB_Click(object sender, RoutedEventArgs e)
{
VisualStateManager.GoToState(btnOut1, "PLCBad",false);
if (SafteyFaults.PLCSafeStates.testStates1 == SafteyFaults.PLCStates.Good)
SafteyFaults.PLCSafeStates.testStates1=SafteyFaults.PLCStates.Bad;
else
SafteyFaults.PLCSafeStates.testStates1 = SafteyFaults.PLCStates.Good;
}
(right now, I don't have any business logic wired up yet- once I get this working, I'll link to actual data).
Anyhow, all of this works to create a single field I can subscribe to, modify, etc. But I need 20+ of these fields. I want to make 'testStates1' an array, but I've not been able to get it to work.
If I make the following edits to the code shown so far, it compiles and runs, but throws an error when I actually try to access the field (e.g. click on the button to change it):
//window
public static class PLCSafeStates
{
public static event EventHandler testStates1Changed;
private static PLCStates[] _testStates1;
public static PLCStates[] testStates1
{
get { return _testStates1; }
set
{
if (value != _testStates1)
{
_testStates1 = value;
if (testStates1Changed != null)
testStates1Changed(null, EventArgs.Empty);
}
}
}
}
//page
public FB1()
{
InitializeComponent();
SafteyFaults.PLCSafeStates.testStates1Changed += PLCSafeStates_testStates1Changed;
}
private void PLCSafeStates_testStates1Changed(object sender, EventArgs e)
{
var test2 = SafteyFaults.PLCSafeStates.testStates1[0];
if (test2 == SafteyFaults.PLCStates.Bad)
{
VisualStateManager.GoToState(btnFB, "PLCBad", true);
}
if (test2 == SafteyFaults.PLCStates.Good)
{
VisualStateManager.GoToState(btnFB, "PLCGood", false);
}
}
private void btnFB_Click(object sender, RoutedEventArgs e)
{
VisualStateManager.GoToState(btnOut1, "PLCBad",false);
if (SafteyFaults.PLCSafeStates.testStates1[0] == SafteyFaults.PLCStates.Good)
SafteyFaults.PLCSafeStates.testStates1[0]=SafteyFaults.PLCStates.Bad;
else
SafteyFaults.PLCSafeStates.testStates1[0] = SafteyFaults.PLCStates.Good;
}

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 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
}
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
}
(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");
}
}
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
}
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");
}

chain of responsiblity design pattern gives error: not all code paths return a value

This codes give me eror:"ConcreteHandler1 : Handler" and others.Error: "not all code paths return a value " error in functions?
public abstract class Handler
{
protected Handler BirSonrakiAdim;
public void NextStep(Handler BirSonrakiAdim)
{
this.BirSonrakiAdim = BirSonrakiAdim;
}
public abstract ReturnFaiz HandleRequest(int mevduat);
}
public class ConcreteHandler1 : Handler
{
public override ReturnFaiz HandleRequest(int mevduat)
{
if (mevduat > 0 && mevduat < 1000)
{
return ReturnFaiz.faiztype1;
}
else if (BirSonrakiAdim != null)
{
BirSonrakiAdim.HandleRequest(mevduat);
}
else
return ReturnFaiz.faiztype1;
}
}
public class ConcreteHandler2 : Handler
{
public override ReturnFaiz HandleRequest(int mevduat)
{
if (mevduat > 1000 && mevduat < 3000)
{
return ReturnFaiz.faiztype2;
}
else if (BirSonrakiAdim != null)
{
BirSonrakiAdim.HandleRequest(mevduat);
}
else
return ReturnFaiz.faiztype1;
}
}
public class ConcreteHandler3 : Handler
{
public override ReturnFaiz HandleRequest(int mevduat)
{
if(mevduat>3000)
{
return ReturnFaiz.faiztype3;
}
else if(BirSonrakiAdim!=null)
{
BirSonrakiAdim.HandleRequest(mevduat);
}
else
return ReturnFaiz.faiztype1;
}
}
public enum ReturnFaiz
{
faiztype1=20,
faiztype2=30,
faiztype3=40
}
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
;
}
}
protected void BtnHesapla_Click(object sender, EventArgs e)
{
Handler h1 = new ConcreteHandler1();
Handler h2 = new ConcreteHandler2();
Handler h3 = new ConcreteHandler3();
h1.NextStep(h2);
h2.NextStep(h3);
Label1.Text= h1.HandleRequest(Convert.ToInt32(TextBox1.Text)).ToString();
}
}
In your elseif statement in public override ReturnFaiz HandleRequest(int mevduat) you are not returning anything...
so should be:
if (mevduat > 0 && mevduat 1000 && mevduat 3000)
{
return ReturnFaiz.faiztype3;
}
else if(BirSonrakiAdim!=null)
{
return BirSonrakiAdim.HandleRequest(mevduat);
}
else
return ReturnFaiz.faiztype1;
You say your methods returns a ReturnFaiz enum, but in the else if clauses you return nothing, because after it passes through the else if the code that does return something is never visited. Which is not valid because your method claims you do return something, and that's a promise the compiler intends you to keep.
What you could do is declare a ReturnFaiz that is called Empty (I would advice to make up more readable names for faiztype1 etc) and has a value of -1. Then you return that at the bottom of every method as a sort of default return value: if the method hasn't returned when it reaches that, it will return ReturnFaiz.Empty.
So:
public enum ReturnFaiz
{
Empty = -1,
faiztype1=20,
faiztype2=30,
faiztype3=40
}
public class ConcreteHandler2 : Handler
{
public override ReturnFaiz HandleRequest(int mevduat)
{
if (mevduat > 1000 && mevduat < 3000)
{
return ReturnFaiz.faiztype2;
}
else if (BirSonrakiAdim != null)
{
BirSonrakiAdim.HandleRequest(mevduat);
}
else
return ReturnFaiz.faiztype1;
return ReturnFaiz.Empty; // default return value if nothing else applies.
}
}

Categories

Resources