Return ComboBox as Boolean - c#

I know its not best practice to use a boolean for a comboBox but for this application which returns data a yes/no is all thats required. I'm trying to return whether its yes or no but am getting a warning 'possible unintended reference' Any help cleaning up the code is greatly appreciated.
public bool PlayDataToEnd
{
get
{
return this.PlayDataToEnd.SelectedValue == "Yes";
}
set
{
this.PlayDataToEnd.SelectedValue = true;
}
}

Suppose your internal ComboBox is named playDataToEndCombo:
public bool PlayDataToEnd
{
get
{
return playDataToEndCombo.SelectedValue.ToString() == "Yes";
}
set
{
playDataToEndCombo.SelectedValue = value ? "Yes" : "No";
}
}
I think you should use Index with a convention: 0 for Yes and 1 for No:
public bool PlayDataToEnd
{
get
{
return playDataToEndCombo.SelectedIndex == 0;
}
set
{
playDataToEndCombo.SelectedIndex = value ? 0 : 1;
}
}

Related

Passing a bool in c#

hey i'm making a discord bot and im trying to set it so that with a command a bool can be set to true / false (it will initiate a different response futher on) but when i do it, it stays as false despite it being set to true. im fairly sure its due to not passing the variable through. how would i go about doing it in this instance?
the bool is called (opse)
namespace ConsoleApp5
{
public class Commands : ModuleBase<SocketCommandContext>
{
bool opse;
[Command("opsset")]
public async Task trueset(string op)
{
if (op == "true")
{
opse = false;
Console.WriteLine("Operations is set to active! Set by " +
Context.Message.Author.Username);
}
if (op == "false")
{
opse = true;
Console.WriteLine("Operations is set to inactive! Set by " +
Context.Message.Author.Username);
}
}
[Command("operations")]
public async Task ops()
{
if (opse = true)
{
await Context.Channel.SendMessageAsync("Operations are not currently active. Check your
designated schedule to see when operations are active");
}
if (opse = false)
{
await Context.Channel.SendMessageAsync("Operations are currently active. Message you SO
or file an absent report");
}
}
}
}
You're using a single = to check for the value of opse, which is incorrect. A single = is used to assign new values.
To compare values you should use ==:
if (opse == true)
{
...
}
if (opse == false)
{
...
}

My IF property is being ignored in my class when i use it

public class Irritante : Child
{
/*Fields*/
private int ir_numeroBirras;
private double ir_mediaBirras;
/*Properties*/
public int NumeroBirras
{
get { return ir_numeroBirras; }
set { if (value > 0) ir_numeroBirras = value; }
}
public double MediaBirras
{
get { return ir_mediaBirras; }
set { ir_mediaBirras = value; }
}
//Constructor
public Irritante(string nome, int idade, int numBirras, double mediaDasBirras) : base(nome, idade)
{
NumeroBirras = numBirras;
ir_mediaBirras = mediaDasBirras;
}
When i try to use the contructor Irritante with the property NumeroBirras it is ignoring the if(value>0)
This means i can still add a 0 to this field with client code, which i should not be able to, any tips? i cant find it anywhere
The default value of ir_numeroBirras is 0. You can't put a 0 using the property. But if you test using a 0 as parameter value, you are being fooled by the default value.
If you're talking about you shouldn't put a 0 in the parameter of Irritante ctor, that's quite different
public Irritante(string name, int idade, int numBirras, double mediaDasBirras) : base(nome, idade)
{
if(numBirras < 1) throw new ArgumentOutOfRangeException(nameof(numBirras), "Hey, you can't drink 0 beers");
ir_numeroBirras = numBirras;
ir_mediaBirras = mediaDasBirras;
}

how to disable userinteraction of nsview

I am trying to do something similar as UserInteractionEnabled = false in iOS, but it seems that this property is not available in NSView. I also see that one approach is to implement the hittest method to return null, but the hittest for this lass already has a non-trivial implementation and I don't think I can set it to return null. I found a (deprecated) property called acceptsTouchEnabled, and I m wondering if it can achieve the same thing as UserInteractionEnabled.
The project is done in Xamarin, btw.
FreeHandView = new PaintBoardControl
{
BackgroundColor = UIColor.Clear,
TranslatesAutoresizingMaskIntoConstraints = false,
UserInteractionEnabled = false
};
this is the original declaration of the variable, where the UserInteractionEnabled is set to false.
and this is the implementation of the hittest method in my Mac app:
public override NSView HitTest(CGPoint aPoint)
{
aPoint = ContentContainer.ConvertPointFromView(aPoint, this);
aPoint = new CGPoint(aPoint.X + _freehandViewLeftConstraint.Constant,
aPoint.Y + _freehandViewTopConstraint.Constant);
if (_hitTestBitArray != null && aPoint.X >= 0 && aPoint.Y >= 0 &&
_hitTestBitArrayWidth > aPoint.X && _hitTestBitArrayHeight > aPoint.Y)
{
var index = aPoint.Y * _hitTestBitArrayWidth + aPoint.X;
return _hitTestBitArray.Get((int)index) ? this : null;
}
return null;
}
Sometimes hitTest don't work at all try using something like this maybe mousedown can help you
public class ExtendedNSView : NSView
{
public bool IsEnabled { get; set; }
public override void MouseDown(NSEvent theEvent)
{
if (IsEnabled)
{
base.MouseDown(theEvent);
}
}
}

StackOverflowException when trying to assign public value to private backing field

I have a public property with private backer like the one below. On the line selected = Selected; I get an SO exception. It would appear that assignment is causing some infinite recursion. Can someone give a more detailed explanation of what is happening? What should the code be instead?
//other class stuff AssetTypes is an enum btw.
private AssetTypes? selected = null;
public AssetTypes? Selected
{
get
{
return selected;
}
set
{
selected = Selected;
if (selected == AssetTypes.Image)
{
image.Click();
}
else if (selected == AssetTypes.Video)
{
video.Click();
}
else
{
selected = null;
}
}
}
The only part that can be changed is the assignment in the setter. The if-else logic needs to be untouched.
A setter has a parameter called value, which is the value that the property is being set to. You should get the new value from that, not from Selected, which just calls the getter, which returns the previous value.
Hence, this line does nothing:
selected = Selected;
And you should never assign to Selected from within its own setter, because that calls the same setter, hence your infinite recursion.
Try this instead:
set
{
if (value == AssetTypes.Image)
{
image.Click();
selected = value;
}
else if (value == AssetTypes.Video)
{
video.Click();
selected = value;
}
else
{
selected = null;
}
}
I think the problem is in the else: Selected = null
Use selected = null with the lowercase s.
Greetings!
You have an infinite recursion on Selected = null, that's why you get a stackoverflow exception. It calls over and over Selected set until it finally blows up with an exception. Change it by
selected = null;
selected = Selected does nothing
what is passed to Set is value;
public AssetTypes? Selected
{
get
{
return selected;
}
set
{
if (selected == value) return; // use this to not do it again
selected = value;
if (selected == AssetTypes.Image)
{
image.Click();
}
else if (selected == AssetTypes.Video)
{
video.Click();
}
else
{
selected = null; // Selected = null; was the recursion
}
NotifyPropertyChanged("Selected"); // this optional and only if you implement INPC
}
}

how to make read only property available in my usercontrol

i make user control from 3 text boxes but i don not how to declare read only property to it i tried many things but it do not work here is my code to make the control
i want to make it read only when needed like if i add checkbox i want if checkbox.check=true make my control readonly
public partial class dateIN : UserControl
{
Dates datess = new Dates();
public dateIN()
{
InitializeComponent();
}
private void dateIN_Leave(object sender, EventArgs e)
{
if (txtDay.Text != "" || txtMonth.Text != "" || txtYear.Text != "")
{
if (!datess.IsHijri(txtDay.Text.Trim() + "/" + txtMonth.Text.Trim() + "/" + txtYear.Text.Trim()))
{
txtDay.Focus();
}
}
}
public string Day
{
set { txtDay.Text = value; }
get { return txtDay.Text; }
}
public string Month
{
set { txtMonth.Text = value; }
get { return txtMonth.Text; }
}
public string Year
{
set { txtYear.Text = value; }
get { return txtYear.Text; }
}
need to know how to make read only property available here plz
just remove the set { } part of the property
Example:
public string Day
{
get { return txtDay.Text; }
}
I dont know the correlation of where your "txtDay", "txtMonth", "txtYear" come from, but you could do something like
public partial class dateIN : UserControl
{
...
...
private bool AllowEditing()
{ return SomeCondition when SHOULD be allowed...; }
public string Day
{
// only allow the set to apply the change if the "AllowEditing" condition
// is true, otherwise, ignore the attempt to assign.
set { if( AllowEditing() )
txtDay.Text = value; }
get { return txtDay.Text; }
}
// same concept for month and year too
}
so may you add some flag to your set when it is true then you set a value.
also you can work with textbox property called ReadOnly.

Categories