type-conversion

C# Convert string to nullable type (int, double, etc...)

I am attempting to do some data conversion. Unfortunately, much of the data is in strings, where it should be int's or double, etc... So what I've got is something like: double? amount = Convert.ToDouble(strAmount); The problem with this approach is if strAmount is empty, if it's empty I want it to amount to be null, so when I add i...

How do I convert a String to an InputStream in Java?

Given a string: String exampleString = "example"; How do I convert this to an InputStream? ...

cannot convert type 'string' to 'int?' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion or null type conversion

Hi, I'm going to try to make myself clearer. Why does C# accept the following: object o = "hello"; int? i = o as int?; if (i == null) { // o was not a boxed int } else { // Can use i.Value to recover the original boxed value } and not String = "hello"; int? i = o as int?; if (i == null) { // o was not a boxed int } else { // Can u...

C# implicit conversions and == operator

Some code for context: class a { } class b { public a a{get;set;} public static implicit operator a(b b) { return b.a; } } a a=null; b b=null; a = b; //compiler: cannot apply operator '==' to operands of type tralala... bool c = a == b; Is it possible to use == operator on different type instanc...

How to convert from System.Enum to base integer?

I'd like to create a generic method for converting any System.Enum derived type to its corresponding integer value, without casting and preferably without parsing a string. Eg, what I want is something like this: // Trivial example, not actually what I'm doing. class Converter { int ToInteger(System.Enum anEnum) { (int)...

How do I convert a CString to a double in C++?

How do I convert a CString to a double in C++? Unicode support would be nice also. Thanks! ...

Is there an API to convert from "YYYYMMDDHHMMSS.UUUUUU-TZO" format to C# DateTime?

Example: "20080807144334.410187-180" (-180 means GMT minus three hours. Rio de Janeiro in this case.) That string format is returned when I query file creation/change/access times via WMI (that is not totally working; see here). I guess I could parse it the idiot way, extracting year, month etc. from the string positions. But I'd like n...

How to lookup and invoke a .Net TypeConverter for a particular type?

I would like to implement a general purpose runtime type conversion function that makes use .Net TypeConverters to do the conversion. Does anyone know how to how to look up and invoke a TypeConverter for a particular type? Consider this C# example: // // Convert obj to the type specified by 'toType'. // object ConvertTo(object obj,...

Converting between types in Objective-C

Being a starting Objective-C developer, and not having the patience to actually do some studying, rather than just diving into things, I ran into the following: I have a CGFloat, and I want to divide it by something, and use the result as an NSInteger. Example: CGPoint p = scrollView.contentOffset; //where p is a CGFloat by nature NSI...

Swig typecast to derived class?

I notice that Swig provides a whole host of functions to allow for typecasting objects to their parent classes. However, in C++ one can produce a function like the following: A * getAnObject() { if(someBoolean) return (A *) new B; else return (A *) new C; } Where "A" is the parent of classes "B" and "C". One can then typec...

Is accepting Object as a parameter acceptable?

Let us say I have a textbox or any other form of input that asks for a social security number. I do want to note that the SSN is a pure example I simply thought of as of right now. This input will naturally be stored as a string initially. string s = Console.ReadLine(); Let us say I want to have a method that validates an SSN and it m...

Get the numerical value of an SPFieldCurrency field

I am using SPFieldCurrency column in one of my lists. My custom code receives a string value as a parameter, which contains the field's value as returned by the GetFormattedValue() method. Now my problem is that the value received by my method contain currency symbols in them, Eg, 10$, 10¥, 10€ etc. Because of the presence of the curr...

How to convert DateTime to a number with a precision greater than days in T-SQL?

Both queries below translates to the same number SELECT CONVERT(bigint,CONVERT(datetime,'2009-06-15 15:00:00')) SELECT CAST(CONVERT(datetime,'2009-06-15 23:01:00') as bigint) Result 39978 39978 The generated number will be different only if the days are different. There is any way to convert the DateTime to a more precise number, a...

How to transfer UINT64 to 2 DWORDS?

Is there efficient way to do this? ...

array<Byte>^ TO unsigned char* :: Marshall class - Interop Issue

Hi, I wanted to convert array< Byte>^ to unsigned char*. I have tried to explain what i have done. I donot know how to proceed further. Please show me the right approach. I am using MS VC 2005. //Managed array array<Byte>^ vPublicKey = vX509->GetPublicKey(); //Unmanaged array unsigned char vUnmanagedPublicKey[MAX_PUBLIC_KE...

How to Get the length of a TypeCode

I'm sure this is an easy question, but I don't have an answer for. Here's the senario and the question. I have an array that was stored using in a particular format. The format contains a Header record with muntiple detail records following it. The header of the record tells me what TypeCode was used to store the data, for instance I...

Reference-type conversion operators: asking for trouble?

When I compile the following code using g++ class A {}; void foo(A&) {} int main() { foo(A()); return 0; } I get the following error messages: > g++ test.cpp -o test test.cpp: In function ‘int main()’: test.cpp:10: error: invalid initialization of non-const reference of type ‘A&’ from a temporary of type ‘A’ test.cpp:6: er...

Managing Implicit Type Conversion in C++

I'm working on code that does nearest-neighbor queries. There are two simple ideas that underlie how a user might query for data in a search: closest N points to a given point in space. all points within a given distance. In my code, Points are put into a PointList, and the PointList is a container has the job of keeping track of the...

Select Max in Linq to Entities / Entity SQL with type conversion

Hey everyone, I'm trying to create some CRUD functionality for a legacy database and using the Entity Framework as an ORM tool. One table, 'Sites', has a Guid type 'Id' column which is never seen by the users and an integer type 'SiteId' which the users use to identitfy a Site. For god knows what reason the numeric SiteId column is of ...

Conversion of the type of a Template class?

Hi folks, I have a class named "baseClass". From this class I inherit a class names "inheritedClass" (public class inheritedClass: baseClass) The baseClass contains a public function that returns a HashSet<baseClass>. When called from the inheritedClass, the return type is obviously still HashSet<baseClass>, but I need a HashSet<inheri...