C # Convert T to Long

I have a general class (C #),

class MyClass<T> where T : struct, IComparable<T> { public T filelocation; } 

T can be either UInt32 or UInt64 (nothing more).

I need to convert filelocation to long to seek in a file ...

I tried the following

 long loc = (T)myclass.filelocation; long loc = (T)(object)myclass.filelocation; 

But nothing works ...

Any ideas?

+4
source share
4 answers

Call Convert.ToInt64 .

The (object)fileLocation creates a UInt32 box.
Boxed types can only be omitted from their original value types , so you cannot do this in one step to long .
You can write (long)(ulong)fileLocation , but for the same reason, this will result in an error.

+11
source

You can use TryParse:

 long lng; int testNum = 55; long.TryParse(testNum.ToString(),out lng); 
+1
source

Try Convert.ToInt64 .

 long loc = Convert.ToInt64(myclass.filelocation); 
+1
source

with your class definition, if I write something like

 public MyClass<long> myclass = new MyClass<long>(); public long returnLong() { return myclass.filelocation; } 

myclass.fileLocation returns long def def

0
source

All Articles