Все вопросы: [casting]

501 вопросов

2
голосов
3ответов
226 просмотров

Casting problems with connected Generic classes

Here's a thinned out version of the classes I have. public abstract class BaseParent { } public abstract class ChildCollectionItem<T> where T : BaseParent { // References a third-party object that acts as the parent to both the collection // items and the collection itself. publi...

0
голосов
1ответов
462 просмотров

get RibbonCommand Name from sender Object that is executed

How to get the Label of Ribbon command that is executed. Information is present in sender object but how to cast it in RibbonCommand and then I can get that command name private void RibbonCommand_Executed(object sender, ExecutedRoutedEventArgs e) { RibbonCommand rbnCmd = sender as RibbonCo...

24
голосов
9ответов
26949 просмотров

.NET: Unable to cast object to interface it implements

I have a class (TabControlH60) that both inherits from a base class (UserControl) and implements an interface (IFrameworkClient). I instantiate the object using the .NET Activator class. With the returned instance, I can cast to the UserControl base class, but not to the interface. The exception ...

2
голосов
2ответов
10006 просмотров

Casting in objective-c

I have a dictionary object that I am pulling data out of. The field is supposed to be a string field but sometime all that it contains is a number. I get the info using: NSString *post = [[temp objectAtIndex:i] valueForKey:@"POSTDESCRIPTION"]; So it is going into a string object. However, when...

3
голосов
3ответов
3289 просмотров

How does function-style cast syntax work?

I guess I am a bit puzzled by the syntax. What does the following mean? typedef char *PChar; hopeItWorks = PChar( 0x00ff0000 );

495
голосов
10ответов
579943 просмотров

Safely casting long to int in Java

What's the most idiomatic way in Java to verify that a cast from long to int does not lose any information? This is my current implementation: public static int safeLongToInt(long l) { int i = (int)l; if ((long)i != l) { throw new IllegalArgumentException(l + " cannot be cast to...

1
голосов
1ответов
383 просмотров

DirectCast not accepting valid casts at compiletime (VB.NET)

How come this is not a valid DirectCast: Public Sub FB(OF T0 As IGH_Goo, T1 As IGH_Goo) _ (ByVal A As DataTree(Of T0), _ ByVal B As DataTree(Of T1)) Dim val_A As T1 = DirectCast(A.FirstItem, T1) End Sub whereas this is: Public Sub FB(OF T0 As IGH_Goo, T1 As IG...

40
голосов
4ответов
43268 просмотров

Efficient way to convert strings from split function to ints in Python

I have a string of data with the following format: xpos-ypos-zoom (i.e. 8743-12083-15) that I want to split up and store in the variables xpos, ypos, and zoom. Since I need to do some calculations with these number I'd like to convert them to integers right from the beginning. Currently, the way ...

6
голосов
7ответов
3749 просмотров

C++: how to deal with const object that needs to be modified?

I have a place in the code that used to say const myType & myVar = someMethod(); The problem is that: someMethod() returns const myType I need to be able to change myVar later on, by assigning a default value if the object is in an invalid state. So I need to make myVar to be non-const...

0
голосов
2ответов
603 просмотров

C# ILMerge Unable to cast object of type 'ClassY' to type 'ClassX'

Here's the situation: I am using ILMerge to merge an assembly & all it's references into 1 .dll file using this method (custom.dll). I have an application which dynamically loads this assembly at runtime (program.exe). Both application & assembly use a common library (common.dll). Abs...

0
голосов
3ответов
405 просмотров

Doubt with List<generic> Explicit Cast</generic>

I don't understand why in some cases I can make an explicit cast and in other cases I can not. Thanks to all! //DAreaLabel extends Message //This Code Works List&lt;Message&gt; list1 = (List&lt;Message&gt;) Arrays.asList((Message[]) getPageRecords(getClasspath(), methodName, object));...

1
голосов
2ответов
136 просмотров

Is there a PHP calculation which could result in a -0?

I am having trouble with a complex script which sometimes (About 2 or 3 times while calulating about 90'000 values), generates a '-0' and writes it into the database. I suspect it's a string (The values which are calulated can result in integers, floats or strings.)* Is there any PHP calculation...

0
голосов
4ответов
987 просмотров

Why can't I set object derived from ObservableCollection<proddata> equal to object returned as ObservableCollection<proddata>?</proddata></proddata>

I get a compile error, "Cannot implicitly conver type 'System.Collections.ObjectModel.ObservableCollection to ProddataRecsObservable'. An explicit conversion exists" See comments in following code segments. //I created a custom class called ProddataRecsObservable derived from //ObservableCollect...

0
голосов
4ответов
103 просмотров

Typecasting based on a variable

How would I go about the following... I have a control that can be bound to different data types... String, Int, Int32, DateTime, etc... but generically the result is stored into a generic "object" data type. So, I use another field to identify the EXPECTED type such as.. String BoundDataType ...

5
голосов
1ответов
3029 просмотров

Casting Object to a specific class in IL?

I discovered the reason I was getting "Operation could destabilize the runtime" in a DynamicMethod I'm producing, and though I easily fixed it, it left me with a seemingly simple question: How do I cast an object reference of type "Object" into a specific type, so that I can call methods from t...

3
голосов
8ответов
279 просмотров

Is it possible to cast a graph of objects?

I have a object graph that is setup like this Clients string Name List[Address] Addresses I would like to cast this to MyClients: Clients string Name List[MyAddress] Addresses MyAddress: Address String City I know that I can cast this by walking the whole object graph...

4
голосов
1ответов
1329 просмотров

Linq to Entities and Xml Fields

I have this scenario: A SQL Server table myTable with field1, xmlField (nvarchar(50) and xml sql server data type) Linq to entities Now I'd like to get a query like this: SELECT Field1, XmlField FROM MyTable WHERE CAST(XmlField AS nvarchar(4000)) = '&lt;myXml /&gt;' Obviously this is a cor...

6
голосов
2ответов
5322 просмотров

Warning when type-casting between pointer and pointer-to-function

I am porting some C code to a TI DSP chip environment. I'm grappling with the C compiler. I have a data structure that includes a pointer to a function. I have a function that initializes the data structure. Something like this: typedef void (*PFN_FOO)(int x, int y); struct my_struct { ...

1
голосов
3ответов
14299 просмотров

Cast Date in Informix

I have never used Informix before and I'm trying to write a query that will return records over the last 365 days. Here is the query I have been trying to use: Select * from Visit where vis_mod_dt between today-365 and today; That returns no records even though I know that there is data for t...

107
голосов
8ответов
167739 просмотров

Java Class.cast() vs. cast operator

Having being taught during my C++ days about evils of the C-style cast operator I was pleased at first to find that in Java 5 java.lang.Class had acquired a cast method. I thought that finally we have an OO way of dealing with casting. Turns out Class.cast is not the same as static_cast in C++....

4
голосов
3ответов
1360 просмотров

AS3, loading in a SWF as a custom type

Fundamental question here. Typically in AS3 you load in a SWF via the Loader, and what you get is some sort of pseudo MovieClip that is of type "Loader". Is there any holy way under the sun to cast this loaded SWF to a custom type that extends MovieClip and not Loader, assuming the SWF was publi...

0
голосов
2ответов
1011 просмотров

Loading external SWF, cannot cast document class to shared base class

I have a parent SWF file that defines a Widget base class. I then load an external SWF into the parent. The external SWF's document class derives from the Widget base class -- let's call it DerivedWidget for example. The problem is that when I load the external SWF, I cannot cast the Loader.co...

32
голосов
5ответов
10661 просмотров

Action<object eventargs> could not be cast to EventHandler?</object>

I was wiring up an event to use a lambda which needed to remove itself after triggering. I couldn't do it by inlining the lambda to the += event (no accessable variable to use to remove the event) so i set up an Action&lt;object, EventArgs&gt; variable and moved the lambda there. The main error w...

5
голосов
2ответов
11685 просмотров

How to cast an int's address to char pointer in C?

Currently the below code gives me a warning when i try to compile it: int z; char *w; w = &amp;z; How can i cast &amp;z properly so that w stores the pointer to z's address?

0
голосов
3ответов
1621 просмотров

Cast a variable to a type represented by another Type variable?

I'm aware that questions like this have been asked before and I doubt it's possible, but I just wanted to make 100% sure that it isn't. In VB.net or C# (either language, it doesn't matter), I want to cast a variable to a type represented by another Type variable. Here's an example of the kind o...

5
голосов
2ответов
196 просмотров

Why do i need to wrap this code in a cast to short?

If i have some code like the following: short myShortA = 54; short myShortB = 12; short myShortC = (short)(myShortA - myShortB); Both operands are shorts and it's going into a short so why do i have to cast it?

6
голосов
5ответов
1198 просмотров

Is using const_cast for read-only access to a const object allowed?

In C++ I have a function that only requires read-only access to an array but is mistakenly declared as receiving a non-const pointer: size_t countZeroes( int* array, size_t count ) { size_t result = 0; for( size_t i = 0; i &lt; count; i++ ) { if( array[i] == 0 ) { ...

6
голосов
4ответов
1083 просмотров

Is there ever an excuse for throwing an Exception from an implicit conversion?

From MSDN: By eliminating unnecessary casts, implicit conversions can improve source code readability. However, because implicit conversions can occur without the programmer's specifying them, care must be taken to prevent unpleasant surprises. In general, implicit conversion operators should...

4
голосов
10ответов
7619 просмотров

C++ How to convert string to char*

I need to convert a string to a char * for use in strtok_s and have been unable to figure it out. c_str() converts to a const char *, which is incompatible. Also, if someone could explain to me why the second strtok_s function (inside the loop) is necessary, it'd be a great help. Why do i need ...

2
голосов
2ответов
1014 просмотров

How to upcast an object of type IEnumerable<foo> to IEnumerable<ifoo> if passed to the function as object?</ifoo></foo>

I'm trying to upcast a parameter passed as an object (which is an upcast of an object of type IEnumerable) to IEnumerable where Foo implements IFoo. Here is an example of what I'd like to do but it does not work. public void F(object o) { //I know the object o is of type IEnumerable&lt;Foo&...