sign value from combobox with enum to another class - c#

Im trying to sign a selected value from a combobox that holds enum values.
this is where i fill the combobox
cmbPrio.Items.AddRange(Enum.GetNames(typeof(PriorityType.Prioritytypes)));
cmbPrio.SelectedIndex = (int) PriorityType.Prioritytypes.Normal;
This is where im tryingt to set the value in my class
private void ReadInput()
{
if (String.IsNullOrEmpty(txtTodo.Text))
{
MessageBox.Show("Write something to do!");
}
else
{
currTasks.Priority = what code should go here??!
currTasks.Description = txtTodo.Text;
currTasks.TaskDate = dateTimePickerToDo.Value;
}
}
My class with the enum:
class PriorityType
{
public enum Prioritytypes
{
Very_Important,
Important,
Normal,
Less_Importan
}
}
My class where my get:set is:
private PriorityType priority;
public PriorityType Priority
{
get
{
return this.priority;
}
set
{
this.priority = value;
}
}

Fill the cmbPrio with enum values directly:
cmbPrio.Items.AddRange(Enum.GetValues(typeof(Prioritytypes)).Cast<object>().ToArray());
So you can assign it as enum value:
currTasks.Priority = (Prioritytypes)cmbPrio.SelectedItem;
This will also work if SelectedIndex doesn't correspond to the int value behind your enum type, e.g.
public enum Prioritytypes
{
Very_Important = 1,
Important = 2,
Normal = 4,
Less_Importan = 8
}
or if you change the order of entries in your combobox.

Related

Pass properties from array to a new class

i am working with a .net application where i have a web service that returns values in array form and now this array values i want to pass to a class and also as a reference to a private object. But since i am fresh new in programming i do not know how where an with what logic to start.
This is the private obj i created and i want to pass those references where CT is the array type and clsIn is the info that comes from another class but i have no idea how to pass neither of them.
private object TotInfo(clsIn In, CT ct)
{
TotInfo objFromCD = new TotInfo();
return objFromCD;
}
And here is the new class i have created that where i want to pass all the values from clsIn and CT:
public class TotInfo
{
// Object properties
private string LAST_OFFER;
private string LAST_OFFER_DATE;
private string CLOSING_REASON;
private string _NO;
private string _STATUS;
#region "GET/SET Property"
public string NO
{
get { return _NO; }
set { _NO = value; }
}
public string LAST_OFFER
{
get { return _LAST_OFFER; }
set { _LAST_OFFER = value; }
}
public string LAST_OFFER_DATE
{
get { return _LAST_OFFER_DATE; }
set { _LAST_OFFER_DATE = value; }
}
public string CLOSING_REASON
{
get { return _CLOSING_REASON; }
set { _CLOSING_REASON = value; }
}
public string STATUS
{
get { return _STATUS; }
set { _STATUS = value; }
}
#endregion
#region "Costruttori"
public CardsTotInfo() { }
public CardsTotInfo(string No, string lastOffer, string lastOfferDate, string closingReason, string status)
{
this.NO = No;
this.LAST_OFFER = lastOffer.ToUpper();
this.LAST_OFFER_DATE = lastOfferDate.ToUpper();
this.CLOSING_REASON = closingReason.ToUpper();
this.STATUS = status.ToUpper();
}
}
I have passed, or better say i think i have passed in the correct way the values of clsIn but i do not know how to pass the properties of the array type CT[].
I really need help.
Thank you in advance.
If CT is an object array and the data you get from the web service always comes in the same order, for instance using an arbitrary example:
object[] CT = { 1, DateTime.Now, "foo", true }
If you know that each property data inside the array will always be at the same index (you will always have a int in index 0 representing an Id, and a DateTime on index 1 representing the last offer day and so on)
I would say you need to set each property "manually":
private object TotInfo(clsIn In, CT ct)
{
TotInfo objFromCD = new TotInfo();
//get data from DB
//set the data from the array into the class properties
objFromCD.Id = (int)ct[0];
objFromCD.LastOfferDate = (DateTime)ct[1];
objFromCD.ClosingReason = (string)ct[2];
objFromCD.Available = (bool)ct[3];
return objFromCD;
}

Two properties - Second property type depends on first property

I have created a class that contains two variables: Type & Value. If the first property (Type) is filled, the second property (Value) can only contain a value that matches the type which is selected on the Type property.
public class Requirement
{
public RequirementType Type { get; set; }
public object Value { get; set; }
public enum RequirementType
{
OS, NetFramework, Connection
}
public enum OSType
{
// Used for RequirementType.OS
Win, Unix, MacOSX
}
public enum NetFrameworkType
{
// Used for RequirementType.NetFramework
Two, Three, Four, FourHalf
}
public enum ConnectionType
{
// Used for RequirementType.Connection
Internet, Connected, None
}
}
I'm using this class in the XAML:
<util:Requirement Type="OS" Value="Win" />
So for example, if the enum value OS has been chosen. The only valid values should be from the enum OSType. I started looking in the .Net source how they solved this with the System.Windows.Trigger & System.Windows.Setter but no success yet.. It seems to be something with the DependsOn attribute and XamlSetTypeConverterAttribute. Does someone know the solution to this problem?
You can use a backing field for value and check each type as it's being set.
public class Requirement
{
public RequirementType Type { get; set; }
private object _value;
public object Value
{
get { return _value; }
set
{
if (Type == RequirementType.OS &&
value.GetType() == typeof(OSType))
{
_value = value;
}
else
{
throw new Exception("Value type is incorrect for Type provided");
}
}
}
}
This test will throw the exception:
var req = new Requirement();
req.Type = RequirementType.OS;
req.Value = RequirementType.Connection;
While this second test will properly set the value:
var req = new Requirement();
req.Type = RequirementType.OS;
req.Value = OSType.Win;
You can use normal properties (type propful and hit Tab):
private RequirementType _type;
public RequirementType Type
{
get { return _type; }
set
{
_type = value;
// do whatever logic you want here
}
}

forcing a variable to hold certain values only

I am using vs 2012. I have a simple string property
string _someString;
public string MyString
{
get
{
return _someString;
}
}
I want this property to hold only certain values. So that when the client uses this property only those certain values can be used.
It sounds like what you really want is an enum:
public enum MyValues //TODO rename all the things
{
SomeValue,
SomeOtherValue,
FinalValue,
}
Then your property can be:
private MyValues value;
public MyValues MyValue
{
get { return value; }
}
If you need to get a string representation of that value just call ToString on the enum value:
string stringValue = value.ToString();
Use an enum as in :
enum MyEnum
{
AllowableValue#1,
AllowableValue#2,
...
}
public MyEnum myEnum { get; set; }
Then populate some UI element with only the values of the enum.
I suppose you want to have some validation on the setter then:
public string MyString
{
get
{
return _someString;
}
set
{
if (value == "a" || value == "b" /* ... */)
_someString = value;
else
throw new InvalidArgumentException("Invalid value!");
}
}
Make sure to set it via the property, not the actual member variable.

How to add value to ComboBox's items, so I can choose it like this comboBox.SelectedIndex = myInt?

Let's say I have this items:
comboBox.Items.Add("Access"); // make it equal to 31
comboBox.Items.Add("Create"); // make it equal to 34
comboBox.Items.Add("Delete"); // make it equal to 36
comboBox.Items.Add("Modify"); // make it equal to 38
Now, I call
comboBox.SelectedIndex = 34; // want to "Create" item has been chosen
What is the easiest way to do that ?
It depends a lot on how your data is going to be managed.
If your items are not going to be modified over the course of your program you can simply use a dictionary as a mapping table.
comboBox.Items.Add("Access"); // make it equal to 31
comboBox.Items.Add("Create"); // make it equal to 34
comboBox.Items.Add("Delete"); // make it equal to 36
comboBox.Items.Add("Modify"); // make it equal to 38
Dictionary<int, int> mapTable = new Dictionary<int, int>();
mapTable.Add(31, 0);
mapTable.Add(34, 1);
mapTable.Add(36, 2);
mapTable.Add(38, 3);
Then simply use the following:
comboBox.SelectedIndex = mapTable[34];
You can even put this logic in a class that inherits from ComboBox for better abstraction.
Unfortunately, winforms doesn't have a ListItem class like ASP.NET does, so you'll have to write your own:
public class cbxItem
{
public string text {get;set;}
public int id {get;set;}
public override string ToString()
{
return text;
}
// you need this override, else your combobox items are gonna look like
// "Winforms.Form1 + cbxItem"
}
then add items to your combobox like this:
cbxItem item = new cbxItem();
item.text = "Access";
item.id = 31;
comboBox.Items.Add(item);
To get the "id" or "value" or however you wish to call it:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var cbxMember = comboBox1.Items[comboBox1.SelectedIndex] as cbxItem;
if (cbxMember != null) // safety check
{
var value = cbxMember.id;
}
}
You want to use SelectedValue instead of SelectedIndex. The index is just a count (0,1,2,3...). The value can be specified.
You'd need to add something more complex than a simple string to do what you want. If all you want is an int and an associated string then you could use a KeyValuePair, but any custom object would work. You'd then need to set DisplayMember and ValueMember on the comboBox so that it displays correctly. Oh, and use SelectedValue or SelectedMember instead of SelectedIndex.
Here's your add:
comboBox.Items.Add(new KeyValuePair<int, string>(){ Key = 34, Value = "Access"});
Yeah, a custom object would make that a simpler statement but the concept is the same.
comboBox.Items.Add(new WorkItem { Key = 31, Value = "Access" });
comboBox.Items.Add(new WorkItem { Key = 34, Value = "Create" });
comboBox.Items.Add(new WorkItem { Key = 36, Value = "Delete" });
comboBox.Items.Add(new WorkItem { Key = 38, Value = "Modify" });
selectItemByKey(34);
You need to add this method:
private void selectItemByKey(int key)
{
foreach (WorkItem item in comboBox.Items)
{
if (item.Key.Equals(key))
comboBox.SelectedItem = item;
}
}`
And a Class like this:
public class WorkItem
{
public int Key { get; set; }
public string Value { get; set; }
public WorkItem()
{
}
public override string ToString()
{
return Value;
}
}

GridView DataBinding

I have a problem with a GridView(I'm using Telerick but i think the .NET GridView is similar for this situation).
I have a List that contains some user define object with some properties that will be displayed in a GridView. This list is loaded from SQL.
My problem is that i have an int property that i want to be parsed and displayed in GridView with some strings.
public class Vehicles
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private string vehName;
public string VehName
{
get { return vehName; }
set { vehName = value; }
}
private int gpsInterval;
public int GpsInterval
{
get { return gpsInterval; }
set { gpsInterval = value; }
}
private int isStolen;
public int IsStolen
{
get { return isStolen; }
set { isStolen = value; }
}
...
}
...
List<Vehicles> vehs = DBveh.getAllVehicles();
GridViewUnitsList.DataSource = vehs;
Is stolen is curently displayed as an int in the GridView. So is there a method to parse "isStolen" value and replace it with somenting like "YES"/"NO" without using a foreach and iterating throw the hole GridView after the binding?
There are 2 easy options:
1) Add a property to your object and reference that property in the DataGrid:
public string IsStolenStr
{
get { return isStolen == 1? "Yes" : "No"; }
}
2) Or add the logic to a <asp:template> column in the DataGrid:
<%# Eval("IsStolen") == 1 ? "Yes" : "No" %>
I would modify the SQL statement so that it returned the Yes/No string based on the isStolen value.

Categories

Resources