For instance, I was thinking of replacing this:
var.StringAttribute = input.ReadString();
With something like this:
var.EnumAttribute = input.ReadExternalReference<EnumName>();
However this doesn't work quite right. And ideas on how to get input to read a custom enumeration?
ReadExternalReference Reads a link to an external file - that's not what you want to do.
If I understand you correctly, you want to read a string, and parse it as an enum.
Try this:
string value = input.ReadString();
var.EnumAttribute = Enum.Parse(typeof(EnumName), value);
Note that this will work for both numbers (anything within the range of the enum's underlying type - typically Int32) and string values, but will throw an exception for invalid values.
Related
I have used Splits in the past, but this one is a bit different for some reason, and I am not sure why...
Code:
string responceuptime = scripting.ReadUntilPrompt();
string[] suptime = responceuptime.Split('s');
UpTime.Text = suptime;
Error:
cannot implicitly convert type string[] to string
That is very basic thing and is very easy to figure out from the error message what is wrong actually.
The following line is the culprit by the way here:
UpTime.Text = suptime;
As suptime is of type string[] which is array while Text property is of type String. When assigning references to and from the type should be same otherwise we will see this error message which you just facing.
It's unclear from the above lines of code that what you are trying to achieve here, but you would need to assign single String object to Text, you cannot assign array or collection to single String object.
Hope it helps.
Your variable suptime is a string[] - an array of strings. While I don't know what Uptime.Text is, I'm guessing that it's looking for a single string, and that's why you're getting the compiler error that you are.
If you want to get the first string out of the array, then you could set it like so:
UpTime.Text = suptime[0];
The output of a call to String.Split is an array of strings (String[]). What your code does, here, is attempting to assign a String[] to a String variable, therefore the application is throwing an exception.
Hence, you must identify, within your array, the value you are looking for and picking the index that points to it (from 0 to suptime.Length - 1). For example:
UpTime.Text = suptime[0]; // first value of the array
UpTime.Text = suptime[2]; // third value of the array
UpTime.Text = suptime[suptime.Length - 1]; // last value of the array
If the result of your split is:
{"A" "Z" "11:57"}
and you want your UpTime.Text to be filled with something that looks like a time value, it's kinda obvious that the value you must pick is the third one.
I have a string variable that will be set to something like Add or Subtract and was wondering if it would be possible to convert it to the System.Linq.Expressions.ExpressionType of Add or Subtract so that I can then use it in my Expression.MakeBinary().
Right now I have something that looks similar to this:
var operands = "Subtract"
Expression.MakeBinary(operands, System.Linq.Expressions.Expression.Constant(50),System.Linq.Expressions.Expression.Constant(20));
I realize that this may not be the best code, I am really just trying to limit my room for error right now and see if it is possible to convert a string to it's ExpressionType.
Note: It has crossed my mind that it would be possible to do this with a switch, but my end result is to try and make calculations as dynamic as possible.
Yes, ExpressionType is just an enum. Here is how you can parse your string into an ExpressionType:
ExpressionType t = (ExpressionType)Enum.Parse(typeof(ExpressionType), "Subtract");
Use Enum.Parse (or TryParse, depending on which is appropriate in context) to parse a string representing a value in an enumeration into an appropriate instance of that enumeration.
I have an XML file that contains a "script" of items to check and validate. What it does is reads in a value to check, and if that check is true, it does something. I originally wrote this to work with just integers, but I realize I need to make it work with more data types.
A sample check is like this...It sees if SomeValue is greater than 20.
<If field="SomeValue" test="#gt" value="20" />
*The field is just some string value. So for a double, the field would be something like 55.7.
All I do is do a int.TryParse on the value to see if I can cast the string (SomeValue) to an integer. If I can, I check to see if it is greater than 20. If not, I just assume false on the check.
Does anyone have any suggestions on how I could this with any data type? (i.e. string, double, DateTime)
Would Generics work? I have never used them so I dont know if they would be the best solution. Thanks.
The tricky bit is a parse when you don't know the types, but this can be done with TypeDescriptor.GetConverter:
object knownVal = 21; //perhaps obtained from reflection
Type type = typeof(int);
string text = "20";
object val = TypeDescriptor.GetConverter(type)
.ConvertFromInvariantString(text);
int rel = Comparer.Default.Compare(knownVal, val);
Generics is an option (especially with Comparer<T>.Default.Compare), but generics doesn't mix well with Type values only known at runtime. It can be done (MakeGenericType/MakeGenericMethod), but it is ugly and a bit slow.
To be honest, though: if it was me I would assume there is a small number of types that need handling here, and special-case them.
check this out
Type configPropType = configurableProp.getPropertyType();
string attValue = xmlelement.GetAttribute(configurableProp.getName());
configProps[configurableProp.getName()] = attValue;
At the point where I am setting the value that got read in from XML it turns out the assigning object needs to be parsed to the correct type for it to work. I need something like.
configProps[configurableProp.getName()] = configPropType.ParseToThisType(attValue);
Looked around on msdn but its a very confusing place.
Looks like what you are trying to do is accomplished with something along these lines:
configProps[configurableProp.getName()] =
Convert.ChangeType(attValue, configPropType);
The problem is with the convert of the txt box value, but why?
string strChar = strTest.Substring(0, Convert.ToInt16(txtBoxValue.Text));
Error is: Input string was not in a correct format.
Thanks all.
txtBoxValue.Text probably does not contain a valid int16.
A good way to avoid that error is to use .tryParse (.net 2.0 and up)
int subLength;
if(!int.TryParse(txtBoxValue.Text,out subLength)
subLength= 0;
string strChar = strTest.Substring(0, subLength);
This way, if txtBoxValue.Textdoes not contain a valid number then subLength will be set to 0;
One thing you may want to try is using TryParse
Int16 myInt16;
if(Int16.TryParse(myString, out myInt16)
{
string strChar = strTest.Substring(0, myInt16);
}
else
{
MessageBox.Show("Hey this isn't an Int16!");
}
A couple reasons the code could be faulty.
To really nail it down, put your short conversion on a new line, like this:
short val = Convert.ToInt16(txtBoxValue.Text);
string strChar = strTest.Substring(0, val);
Likely the value in txtBoxValue.Text is not a short (it might be too big, or have alpha characters in it). If it is valid and val gets assigned, then strTest might not have enough characters in it for substring to work, although this normally returns a different error. Also, the second parameter of substring might require an int (not sure, can't test right now) so you may need to actually convert to int32 instead of 16.
What is the value of txtBoxValue.Text during your tests?
ASP.NET offers several validation controls for checking user input. You should use something like a CompareValidator or RegularExpressionValiditor in your WebForm if you're expecting a specific type of input, eg, an Integer.