How can I get value in `combobox` with function - c#

public string year(ComboBox cbYears)
{
string value = null;
value= cbYears.SelectedText;
return value;
}
This is my function but i get always null value, where is my mistake can anyone help me ?

You can use the GetItemText method to get the text of the selected combobox item :
public string year(ComboBox cbYears)
{
return cbYears.GetItemText(cbYears.SelectedItem);
}

You should do it like this:
string value = cbYears.SelectedItem.Text;
return value;

I think it would work
public string year(ComboBox cbYears)
{
var result = cbYears.SelectedItem as string;
return result;
}

Related

Get value from string in IEnumerable

I have the following code
public const string boy = "B";
public const string girl = "G";
private gender(string description, string value)
{
Description = description;
Value = value;
}
public static IEnumerable<gender> GetAll()
{
yield return new gender("Boy", boy);
yield return new gender("Girl", girl);
}
I want to find a way to give my program the string "Boy" and get as a result the string "B" as it should. How is this possible?
var param = "Boy";
var someBoy = GetAll().Where(g => g.Description == param).Select(g => g.Value).Single();
Almost the same as in prevois answer but with check for wrong value received :)
var rez = GetAll().FirstOrDefault(g=>g.Description==string_received);
if(rez==null) throw new ArgumentException();
return rez.Value;
Why do you even want to use an IEnumerable method and a Gender class? You should use an Enum in this situation. Define your Enum like this:
public Enum Gender { Boy, Girl };
Then, you can do this:
Gender gender = Gender.Boy;
string description = gender.ToString();
// If you want to use 'B' as value...
string value = description[0];
Read more about enums here: http://www.dotnetperls.com/enum

C# NullReferenceException in class validation method

I am trying to add a helper method to scrub out any non-alphanumeric characters in my class. However, I keep getting the error
NullReferenceException: Object reference not set to an instance of an object.
Not sure what I'm doing wrong here since I thought this was the proper way to set up any kind of validation within a class. Any suggestions would be appreciated.
private string agentId;
public string AgentId
{
get { return agentId; }
set { agentId = this.scrubAgentId(); }
}
private string scrubAgentId()
{
char[] arr = this.AgentId.ToCharArray();
arr = Array.FindAll<char>(arr, (c => (char.IsLetterOrDigit(c))));
return new string(arr);
}
This isn't really right at all. You're discarding the value when performing your set. It should probably look something more like this:
private string agentId;
public string AgentId
{
get { return agentId; }
set { agentId = this.scrubAgentId(value); }
}
private string scrubAgentId(string value)
{
if(value == null)
return value;
char[] arr = value.ToCharArray();
arr = Array.FindAll<char>(arr, (c => (char.IsLetterOrDigit(c))));
return new string(arr);
}
In the set part of the property you have an implicit object, value which holds the value you want to set... Use this value as your base
private string agentId;
public string AgentId
{
get { return agentId; }
set { agentId = value; } // manipulate value here using your method
}
Are you initialising agentid anywhere first?
Its failing on char[] arr = this.AgentId.ToCharArray();
You never reference the value in your setter. You want to do something like this:
private string agentId;
public string AgentId
{
get
{
return agentId ;
}
set
{
agentId = new string( value.ToCharArray().Where( c => c.IsLetterOrDigit(c) ) ) ;
}
}

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.

add two value to combobox

In c# we can add a text and a value to each items of combobox, I want to know is it possible in Java also? if not please advice.
I Solved my problem by create a class like
public class ItemInfo {
public String Name;
public String Value;
public ItemInfo(String Name , String Value) {
this.Name = Name;
this.Value = Value;
}
public String toString() {
return Name ;
}
public String getValue() {
return Value ;
}
}
than I just create a new object from this class & pass it to my combobox
combbox1.addItem(new ItemInfo(item[0],item[1]));
job done :)!
You should learn how to use ComboBox in java.

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