Retrieve integer value of enum - c#-4.0

Given:
public enum myType
{
Val1 = 1,
Val2 = 2,
Val3 = 3
}
and code elsewhere in the app where a value :
...
row.myType // resolves to Val1
...
I need to translate row.myType to 1

Simply cast to an int:
int enumValue = (int)row.MyTime;

cast it to int
(int)row.myTime

You can simply cast it to an integer:
(int)row.myType;

myType someEnumVal = myType.Val1;
int intValOfEnum = (int)someEnumVal;

Related

UVM indexing into array by get_type_name

Is this possible? Get_type_name is a string. Can't I have an int array and use the name to index in? I get index expression type of illegal.
Obj n1;
int number[100];
n1 = new();
number[n1.get_type_name] = 1;
Long day. Should have declared int number[string]

Can I distinguish Integer and Double type in rapidjson

When I ask type of rapidjson::Value using GetType() method, it returns only belows Type:
//! Type of JSON value
enum Type {
kNullType = 0, //!< null
kFalseType = 1, //!< false
kTrueType = 2, //!< true
kObjectType = 3, //!< object
kArrayType = 4, //!< array
kStringType = 5, //!< string
kNumberType = 6 //!< number
};
As you can see, there are no such kIntType nor kDoubleType (even kUintType, kInt64Type) Therefore, I can't get actual value of rapidjson::Value.
For example:
if (value.GetType() == rapidjson::kNumberType)
{
double v = value.GetDouble() // this?
unsigned long v = value.GetUInt64() // or this??
int v = value.GetInt() // or this?
}
Is there anyway to distinguish actual numeric type?
Thanks.
There are:
bool Value::IsInt() const
bool Value::IsUint() const
bool Value::IsInt64() const
bool Value::IsUint64() const
bool Value::IsDouble() const
Note that, 1-4 are not exclusive to each other. For example, the value 123 will make 1-4 return true but 5 will return false. And calling GetDouble() is always OK when IsNumber() or 1-5 is true, though precision loss is possible when the value is actually a 64-bit (unsigned) integer.
http://miloyip.github.io/rapidjson/md_doc_tutorial.html#QueryNumber

Convert string representation of an array of int to a groovy list

If I am given an string which is the repsentation of an array of int like below
String d = "[2,3,4,5]"
How I convert to an array of string?
String[] f = convert d to array of String
Also how I convert to an array of int?
int[] f = convert d to array of int
I was looking for other solutions, since my values contained strings including dots in them and found this.
This code should work for you:
"[1,2,3]".tokenize(',[]')*.toInteger()
You can use Eval.me like so:
String[] f = Eval.me( d )*.toString()
Or
int[] i = Eval.me( d )
Be careful though, as if this String is entered by a third party, it could do nasty things and is a huge security risk... To get round that, you'd need to parse it yourself with something like:
def simplisticParse( String input, Class requiredType ) {
input.dropWhile { it != '[' }
.drop( 1 )
.takeWhile { it != ']' }
.split( ',' )*.asType( requiredType )
}
String[] s = simplisticParse( d, String )
int[] i = simplisticParse( d, Integer )

How do I parse a string into a number with Dart?

I would like to parse strings like 1 or 32.23 into integers and doubles. How can I do this with Dart?
You can parse a string into an integer with int.parse(). For example:
var myInt = int.parse('12345');
assert(myInt is int);
print(myInt); // 12345
Note that int.parse() accepts 0x prefixed strings. Otherwise the input is treated as base-10.
You can parse a string into a double with double.parse(). For example:
var myDouble = double.parse('123.45');
assert(myDouble is double);
print(myDouble); // 123.45
parse() will throw FormatException if it cannot parse the input.
In Dart 2 int.tryParse is available.
It returns null for invalid inputs instead of throwing. You can use it like this:
int val = int.tryParse(text) ?? defaultValue;
Convert String to Int
var myInt = int.parse('12345');
assert(myInt is int);
print(myInt); // 12345
print(myInt.runtimeType);
Convert String to Double
var myDouble = double.parse('123.45');
assert(myInt is double);
print(myDouble); // 123.45
print(myDouble.runtimeType);
Example in DartPad
As per dart 2.6
The optional onError parameter of int.parse is deprecated. Therefore, you should use int.tryParse instead.
Note:
The same applies to double.parse. Therefore, use double.tryParse instead.
/**
* ...
*
* The [onError] parameter is deprecated and will be removed.
* Instead of `int.parse(string, onError: (string) => ...)`,
* you should use `int.tryParse(string) ?? (...)`.
*
* ...
*/
external static int parse(String source, {int radix, #deprecated int onError(String source)});
The difference is that int.tryParse returns null if the source string is invalid.
/**
* Parse [source] as a, possibly signed, integer literal and return its value.
*
* Like [parse] except that this function returns `null` where a
* similar call to [parse] would throw a [FormatException],
* and the [source] must still not be `null`.
*/
external static int tryParse(String source, {int radix});
So, in your case it should look like:
// Valid source value
int parsedValue1 = int.tryParse('12345');
print(parsedValue1); // 12345
// Error handling
int parsedValue2 = int.tryParse('');
if (parsedValue2 == null) {
print(parsedValue2); // null
//
// handle the error here ...
//
}
void main(){
var x = "4";
int number = int.parse(x);//STRING to INT
var y = "4.6";
double doubleNum = double.parse(y);//STRING to DOUBLE
var z = 55;
String myStr = z.toString();//INT to STRING
}
int.parse() and double.parse() can throw an error when it couldn't parse the String
Above solutions will not work for String like:
String str = '123 km';
So, the answer in a single line, that works in every situation for me will be:
int r = int.tryParse(str.replaceAll(RegExp(r'[^0-9]'), '')) ?? defaultValue;
or
int? r = int.tryParse(str.replaceAll(RegExp(r'[^0-9]'), ''));
But be warned that it will not work for the below kind of string
String problemString = 'I am a fraction 123.45';
String moreProblem = '20 and 30 is friend';
If you want to extract double which will work in every kind then use:
double d = double.tryParse(str.replaceAll(RegExp(r'[^0-9\.]'), '')) ?? defaultValue;
or
double? d = double.tryParse(str.replaceAll(RegExp(r'[^0-9\.]'), ''));
This will work for problemString but not for moreProblem.
you can parse string with int.parse('your string value');.
Example:- int num = int.parse('110011'); print(num); // prints 110011 ;
If you don't know whether your type is string or int you can do like this:
int parseInt(dynamic s){
if(s.runtimeType==String) return int.parse(s);
return s as int;
}
For double:
double parseDouble(dynamic s){
if(s.runtimeType==String) return double.parse(s);
return s as double;
}
Therefore you can do parseInt('1') or parseInt(1)
void main(){
String myString ='111';
int data = int.parse(myString);
print(data);
}
String age = stdin.readLineSync()!; // first take the input from user in string form
int.parse(age); // then parse it to integer that's it
You can do this for easy conversion like this
Example Code Here
void main() {
var myInt = int.parse('12345');
var number = myInt.toInt();
print(number); // 12345
print(number.runtimeType); // int
var myDouble = double.parse('123.45');
var double_int = myDouble.toDouble();
print(double_int); // 123.45
print(double_int.runtimeType);
}

How can I get values of enum variable?

My question is how can I get values of enum variable?
Please look at the attached screenshot... "hatas" is a flag-enum. And I want to
get "HasError" - "NameOrDisplayNameTooShort" errors to show them.
using System;
namespace CampaignManager.Enums
{
[Flags]
public enum CampaignCreaterUpdaterErrorMessage
{
NoError = 0,
HasError = 1,
NameOrDisplaynameTooShort = 2,
InvalidFirstName = 3,
}
}
I tried simply;
Messagebox.Show(hatas); // it's showing InvalidFirstName somehow...
Thank you very much for any help...
First thing: If you want to use the FlagsAttribute on your enum you need to define the values in powers of two like this:
[Flags]
public enum CampaignCreaterUpdaterErrorMessage
{
NoError = 0,
HasError = 1,
NameOrDisplaynameTooShort = 2,
InvalidFirstName = 4,
}
To get parts of a flagged enum, try something like:
var hatas = CampaignCreaterUpdaterErrorMessage.HasError | CampaignCreaterUpdaterErrorMessage.NameOrDisplaynameTooShort;
var x = (int)hatas;
for (int i=0; i<Enum.GetNames(typeof(CampaignCreaterUpdaterErrorMessage)).Length; i++)
{
int z = 1 << i; // create bit mask
if ((x & z) == z) // test mask against flags enum
{
Console.WriteLine(((CampaignCreaterUpdaterErrorMessage)z).ToString());
}
}
For getting the underlying value try casting:
Messagebox.Show(((int)hatas)ToString());
In your example, ToString is getting called by default against the CampaignCreaterUpdaterErrorMessage enum which return the string representation of the enum.
By casting to an int, the underlying default type for enums, you get ToString on the integer value.
You need to cast/unbox the enum into an int as follows.
(int)CampaignCreaterUpdaterErrorMessage.NoError
(int)CampaignCreaterUpdaterErrorMessage.HasError
Try this:
Messagebox.Show(CampaignCreaterUpdaterErrorMessage.NameOrDisplaynameTooShort);

Resources