casting

Average a time value in SQL Sever 2005

I've got a varchar field in SQL Sever 2005 that's storing a time value in the format "hh:mm"ss.mmmm". What I really want to do is take the average using the built in aggregate function of those time values. However, this: SELECT AVG(TimeField) FROM TableWithTimeValues doesn't work, since (of course) SQL won't average varchars. Howe...

Casting Java Objects From JRuby

I'm working with a Java library in JRuby. I'm reading an object from a file, and I need to pass it as a different object type to a second constructor: @hmm_model = ObjectInputStream.new(FileInputStream.new(LINGPIPE_MODEL_PATH)) @tagger = HmmDecoder.new(@hmm_model) @hmm_model is of type ObjectInputStream, and needs to be cast to (Hidd...

What happens when AnsiString is cast to PAnsiString?

I have the method (Delphi 2009): procedure TAnsiStringType.SetData(const Value: TBuffer; IsNull: boolean = False); begin if not IsNull then FValue:= PAnsiString(Value)^; inherited; end; This is an abstract method on the base class, where "Value: Pointer" expects the pointer of the corresponding data, as: String = PString Ansi...

How to read in specific sizes and store data of an unknown type in c++?

I'm trying to read data in from a binary file and then store in a data structure for later use. The issue is I don't want to have to identify exactly what type it is when I'm just reading it in and storing it. I just want to store the information regarding what type of data it is and how much data of this certain type there is (informati...

Cast with GetType()

Is it possible to cast an object to the type returned from GetType()? I'd like a generic method that can accept an object (for anonymous types) but then return an object cast as the anonymous type. I've been thinking of using the LCG DynamicMethod to build a method on a container class, but I can't exactly figure out what that would look...

Problem casting a list retrieved from data cache

var listings = new List<FPListing>(); if (Cache["Listings"] == null) { listings = GetFPListings(Industry); Cache["Listings"] = listings; } else { listings = (List<FPListing>)Cache["Listings"]; } The cast throws this exception Unable to cast object of type 'System.Collections.Generic.List1[Listings+FPListing]' to ty...

Why can't dotnet 1.1 cast down after ArrayList.GetRange?

I'd like to create an array from range of values within an ArrayList but am getting the error "At least one element in the source array could not be cast down to the destination array type". Why should the following fail, and what would you do instead? int[] ints = new int[] { 1, 2, 3 }; ArrayList list = ArrayList.Adapter(ints); int[]...

Are there compelling reasons AGAINST using the C# keyword "as"?

I find that using the following: TreeViewItem i = sender as TreeViewItem; if(i != null){ ... } is easier to write and understand than: if(sender.GetType() == typeof(TreeViewItem)){ TreeViewItem i = (TreeViewItem)sender; ... } Are there compelling reasons not to use the first construct? ...

Why casting from a stack to int is not running in Java?

I'm trying to cast a char that is stored in stack to an interger and this is what I did. operands = new StackLinked(); if ( (a == '0') || (a == '1') || (a == '2') || (a == '3') || (a == '4') || (a == '5') || (a == '6') || (a == '7') || (a == '8') || (a == '9') ) { operands.push(a); /*Stor operands in the stack operands.*/ } ...

Can I avoid casting an enum value when I try to use or return it?

If I have the following enum: public enum ReturnValue{ Success = 0, FailReason1 = 1, FailReason2 = 2 //Etc... } Can I avoid casting when I return, like this: public static int main(string[] args){ return (int)ReturnValue.Success; } If not, why isn't an enum value treated as an int by default? ...

Get bools out of byte and the other way

Simple question: How do i tell which bits in the byte are set to 0 and which are set to 1 for example: //That code would obviously wont work, but how do i make something similar that would work? byte myByte = 0X32; foreach(bool bit in myByte) { Console.WriteLine(bit); } //Part 2 revert bool[] bits = new bool[8]; bits[0] = 0 bits[...

Effect of casting to int in C#

Could someone take the time to explain me the language underpinnings here : int foo = myObject.SomeList.Count; for (int i = 0 ; i < foo ; i++) { myObject.SomeList.Add(bar); } goes into an infinite loop because foo references a value that keeps being incremented. Modifying the first line to: int foo = (int)myObject.SomeList.Count; ...

How to copy between generic declared types with value type constraints

I have a generic method that copies values between value types. The following approaches give a design time error, even with the struct constraint. Any idea how I can copy or cast between the values? private Ttgt MyMethod<Tsrc,Ttgt>(Tsrc SourceObject) where Tsrc:struct where Ttgt:struct { //Error:cannot ...

Why does "int[] is uint[] == true" in C#

Can somebody clarify the C# is keyword please. In particular these 2 questions: Q1) line 5; Why does this return true? Q2) line 7; Why no cast exception? public void Test() { object intArray = new int[] { -100, -200 }; if (intArray is uint[]) //why does this return true? { uint[] uintArray = (uint[])in...

how to use your same Domain entities throug a .Net webservice?

if you have an entity which is reference in the client and a webservice like this public class Post { public int ID {get; set;} string Data {get; set;} } public class MyService: System.Web.Services.WebService { [WebMethod] public int Write (Post post) { //Do stuff ...

What's up with static_cast with multiple arguments???

Can anyone tell me what this cast has for effect (besides setting happyNumber to 1337), if any at all, and if it has no other effect, how come I can write code like this??? Is this a compiler bug, or some "hidden away feature" of C++? int happyNumber = static_cast<int>(123.456, TRUE, "WTF" , false , "IS" , NULL , "GOING" , 0xff , "ON???...

Simple subtraction and cast question

Why won't this simple subtraction work? int MyPageNumber = Convert.ToInt32(cboPageNumber.SelectedItem); MyPageNumber += (MyPageNumber - 1); //does not work int MyNewPageNumber = MyPageNumber - 1; /works I was also hoping someone could tell me why this gives me a "red line" for not being able to do a cast: short MyPageNumber = Convert...

Do I cast the result of malloc?

In this question, someone suggested in a comment that I should not cast the results of malloc. I.e. int *sieve = malloc(sizeof(int)*length); rather than int *sieve = (int *)malloc(sizeof(int)*length); Why would this be the case? ...

List<T> Casting algorithms and performance considerations.

I have the following code class Program { static void Main(string[] args) { List<A> aList = new List<A>(); var aObj = new A(); aObj.Go(aList.Cast<IB>()); } } class A : IB { public void Go(IEnumerable<IB> interfaceList) { foreach (I...

What is fastest: (int), Convert.ToInt32(x) or Int32.Parse(x)?

Which of the following code is fastest/best practice for converting some object x? int myInt = (int)x; or int myInt = Convert.ToInt32(x); or int myInt = Int32.Parse(x); or in the case of a string 's' int myInt; Int32.TryParse(s, out myInt); I'm curious on which performs the fastest for datatypes which have a method in Convert...