Parsing the value of an integer using a string? - c#

I have Script2 which has int restaurant1Current, along with several other variables with similar names. On another script, I'm trying to parse the value of restaurant1Current using this code. category is a string with the value of "restaurant1".
string currentStringBuffer = "Script2." + category + "Current";
int currentNumber = int.Parse(currentStringBuffer);
I want to be able to use the same code as I change the value of category hence the need to parse it using a string instead of referencing the variable directly. Is this the correct syntax or am I missing something? As is the code tells me "The input string is not in the correct format" but when debugging it shows that the string contains the value "Script2.restaurant1Current" which again is an int. So why wouldn't I be able to parse the value to currentNumber in the next line?

public Enum Categories{
Restauraunt,
Grocer,
Bank
}
Enum stands for enumeration, it is a numbered list. with this particular enum,
Categories.Bank =2; so you can call or assign.
something.category=2;
or
int rando = Categories.Bank

Related

How to change text dynamic in unity

at line 161,I want to insert my text in parameter t,but it won't change when i debug it.although the parameter tmp had alredy changed.
I want to change this Text in UI,when my parameter t changes.
With respect to your specific issue, Insert is defined as:
public string Insert (int startIndex, string value);
and returns a new string. In C#, strings aren't modified, new strings are created. In this way, they act like a value type, even though they're a reference type. In other words, once a string is created, it is never modified - it's 'immutable'. So, you need to store your newly created string.
In cases like this, I like to use the string interpolation, as it allows me to get a slightly clearer representation of what the final string will look like.
var tmp = System.Text.Encoding.UTF8.GetString ( e.Message );
t.text = $"{tmp}\n{t.text}"; // Note that a newline is represented as \n
Or, if you add the System.Text namespace; you could reduce it down to:
using System.Text;
...
t.text = $"{Encoding.UTF8.GetString ( e.Message )}\n{t.text}";
The string type in c# is immutable, therefore Insert returns a new string instead of modifying the current one.
Do:
t = t.text.Insert(0, tmp + "//n");
See also
How to modify string contents in C#

Regex to validate price (type of decimal)

I have looked through stackoverflow trying to find a solution to this but with no luck so hence why I have resulted in asking the question..
I have a field on my form which is price, type of decimal this is optional depending on what they have selected from a dropdown, So I cant use the [Required] attribute.
When the form is submitted if they have chosen a value from the dropdown which requires the user to enter a postage price I then need to check this field to make sure its a valid decimal so to do this I have the following
public static bool IsValid(decimal postagePrice)
{
var regex = new Regex(#"^\d+.\d{0,2}$");
return regex.IsMatch(postagePrice);
}
But it complains and says "Argument type decimal is not assignable to parameter type string" which I understand, I also can't use Decimal.TryParse as that expects a string.
How can I resolve this (I'm not in a position to change the type from decimal to string either)
If all you want is to verify that the value has at most two decimal positions, you could use a modulo:
public static bool IsValid(decimal postagePrice)
{
return postagePrice % 0.01m == 0m;
}
Regular expressions work on strings - it's that simple.
So in one way or another you'll need to covert the decimal to a string before using a regex to validate it.

String to Int for new patient ID

I need to convert a string in the database of patients to a int to create a new patient ID.
In the Hospital database, the patient ID is a STRING, not an integer. It is of the form p99. To create a new ID, I need to take out the p, convert to an integer, add 1, then put a 0 in if the value is less than 10, then add back the p.
I am using Microsoft visual studio and C#.
How would I go about this? Any help would be greatly appreciated!
You can use string.Substring Method and Int32.TryParse method.
String.Substring Method (Int32)
Retrieves a substring from this instance. The substring starts at a
specified character position.
Int32.TryParse Method (String, Int32)
Converts the string representation of a number to its 32-bit signed
integer equivalent. A return value indicates whether the conversion
succeeded.
string patientId = "p99";
int id;
if (Int32.TryParse(patientId.Substring(1), out id))
{
patientId = string.Format("p{0}{1}", id < 10 ? "0" : string.Empty, id);
MessageBox.Show("Patient Id : " + patientId);
}
else
{
MessageBox.Show("Error while retrieving the patient id.");
}
You can use int.Parse() (MSDN) to convert a string to an integer.
You can write a simple routine to do this.
Assuming there is always a leading 'p' you can just do sID = sID.substring(1) to remove the first character.
Then you can use Int ID = Int16.Parse(sID) to convert to an Int (16-bit in this case). If you need 32-bit, use Int32.Parse
then ID++ or ID = ID+1 to increment by one.
Next, you need to convert back to a string with sID = ID.ToString()
Finally, do some string manipulation to test the length, add the leading '0' if length = 1, and the leading 'p'.

Converting Dropdown list to int

hello mates i am trying to store value from dropdown list to an integer but i am getting an exception Input string was not in a correct format.
int experienceYears = Convert.ToInt32("DropDownList1.SelectedValue");
please help.
Remove the quotes; the code as it stands is trying to convert the literal string "DropDownList1.SelectedValue" to an integer, which it can't.
int experienceYears = Convert.ToInt32(DropDownList1.SelectedValue);
Try it without the quotes:
int experienceYears = Convert.ToInt32(DropDownList1.SelectedValue);

Why is my enum.Parse method failing?

I'm trying to dynamically set an enum based on the value in a string so far so good I don't know what I've been doing wrong. I have the following code:
public enum TagLabels : long
{
TurnLeft = 0x0000000000000000000030A38DB1,
TurnRight = 0x00000000000000000000307346CC,
LiftApproach = 0x0000000000000000000012107A8D
}
TagLabels IDs;
string someID = "0x0000000000000000000012107A8D";
IDs = (TagLabels)Enum.Parse(typeof(TagLabels), someID ); //<== I get runtime error on this line
I cannot see what's wrong with what I'm doing.
Enum.Parse is intended to convert a string representation of the symbolic name into an enum val, as in Enum.Parse("TurnLeft"). If what you have is a string giving the numeric value, then you should just parse the string as the corresponding integer type and cast it to the Enum val.
IDs = (TagLabels)long.Parse("0x0000000000000000000012107A8D");
IDs = (TagLabels)Convert.ToInt64(someID, 16);
EDIT: You have a string that is in hex format and not a direct number. So, it needs conversion to int first.
If the Enum value exists, you can cast an int value to Enum type.
EDIT2: Changed after Marc's suggestion from Convert.ToInt32 to Convert.ToInt64
SomeID is a string and your enum is a long.
Try using TurnLeft instead of "0x0000000000000000000012107A8D"
Where is the string you're parsing? Aren't you trying to turn a string like "TurnLeft" into TagLabels.TurnLeft?
MSDN

Categories