If you have an instance of a Collection, say something like:
Collection<String> addresses = new ArrayList<String>();
Which were to then be populated with a bunch of values, which is the "best" way, if any, to make use of the toArray() method without requiring a type cast?
String[] addressesArray = addresses.toArray(new String[] {});
...
Is there a way to cast an instance of a class using a Type variable rather then an explicitly provided type?
For example in my method below "this" is a derived type of "Node". I want the method to repeatedly try to get a value from GetNodeIntrinsicProperty() after which if it get's a null value it should cast itself as it's base type an...
If I execute the following code in C:
#include <stdint.h>
uint16_t a = 4000;
uint16_t b = 8000;
int32_t c = a - b;
printf("%d", c);
It correctly prints '-4000' as the result. However, I'm a little confused: shouldn't there be an arithmetic overflow when subtracting a larger unsigned integer from the other? What casting rules are ...
Is there extra overhead in using the object.ReferenceEquals method verses using ((object)obj1 == (object)obj2)?
In the first case, there would be a static method call involved, and in both cases some form of casting to an object would be involved.
Even if the compiler balances out those methods, what about inequality?
(object)obj != n...
I have created a dynamic typing system in C in order to create a dictionary that can contain values of different bit widths. The structure of the dynamic object is:
typedef struct
{
void* Pointer;
unsigned char Size;
} Dynamic;
I need to compare two of these Dynamics that hold A2D readings and then compare the difference a...
I'm going through the "Head First C#" book and in one of the chapters I created a program and uses variables declared as ints and decimals. Visual Studio got cranky with me a couple of times about mixing and matching the two. For example:
dinnerParty.NumberOfPeople = (int) numericUpDown1.Value;
NumberOfPeople is declared as an int an...
Hi All,
I am having problems getting the values of a dropdownlist from the FIndControl function.
Here is the code I am trying to use:
currentCategoryID = Convert.ToInt32(((DropDownList)r.FindControl("DDLCategories")));
The value of the currentCategoryID in the database is an "int"
When I execute the above code I get this error mes...
I have a collection class that inherits from List<>. I've set up a function to sort the list by a certain property like so:
public PlaylistCollection SortByName(IEnumerable<Playlist> playlists)
{
return (PlaylistCollection)playlists.OrderBy(p => p.Name);
}
When I try to use the sorted results in my code like this:
artistSource.Pl...
Hi All,
I am trying to use a SqlDataReader to count the amount of categories I use.
Here is my Business Logic Code:
// Return count of main categories for homepage
[DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
public int GetMainCatCount(int intCategoryID)
{
intCategoryID = SQLInject(intCategoryID...
I'm a C# programmer who is forced to use VB (eh!!!!). I want to check multiple controls state in one method, in C# this would be accomplished like so:
if (((CheckBox)sender).Checked == true)
{
// Do something...
}
else
{
// Do something else...
}
So how can I accomplish this in VB?
...
I'm trying to write a validation to check that an Object instance can be cast to a variable Type. I have a Type instance for the type of object they need to provide. But the Type can vary. This is basically what I want to do.
Object obj = new object();
Type typ = typeof(string); //just a sample, really typ is a variable
...
There's the following declarations:
void qsort(void *lineptr[], int left, int right, int (*comp)(void *, void *));
int numcmp(char *, char *);
int strcmp(char *s, char *t);
Then, somewhere in the program there is the following call:
qsort((void**) lineptr, 0, nlines-1,
(int (*)(void*,void*))(numeric ? numcmp : ...
int foo(char *c) {...}
main() {
int (*thud)(void *);
thud = (int (*)(void *))(foo);
}
What actually happens during the evaluation of the assignment?
There is a difference between the cast type and foo; the cast type is a pointer and foo is a function. So, does the compiler convert what's in '(foo)' into a pointer to foo ...
Is it true that const_cast is just a way to tell the compiler "stop moaning, treat this as a non-const pointer"? Are there any cases when const_cast itself is translated into actual machine code?
...
I have code like this:
NSData *data = [NSData dataWithContentsOfURL:objURL];
const void *buffer = [data bytes];
[self _loadData:buffer];
[data release];
the "_loadData" function takes an argument like:
- (void)_loadData:(const char *)data;
How do I convert "const void " to a "const char" on Objective-C?
...
I have a bit of code I'm running to test multithreading in MATLAB mex functions (I know MATLAB isn't thread safe... I'm just playing around to see what happens). The entry point to MATLAB C code functions has the signature of the mexFunction function in the code snipped at the bottom of the post. Since I want to essentially pass the ar...
I'm wondering if it's possible to cast an object to a Type... I've just started using Reflection, so maybe I'm doing it all wrong but here's what I would like to do:
...
Type type = ...;
Type interfaceType = someOtherType.GetInterface("IConverter`2");
return (Cast to interfaceType)Activator.CreateInstance(type);
Is the cast to the in...
I'm working through Josh Smith's CommandSink code obviously do not understand something about the "as" keyword in C#.
I don't understand why he wrote the line:
IsValid = _fe != null || _fce != null;
since he only needed to write:
IsValid = depObj != null;
Since it would never be the case the _fe would be null and _fce not null, o...
I have code that stores values in the range 0..255 in a Java byte to save space in large data collections (10^9 records spread over a couple hundred arrays).
Without additional measures on recovery, the larger values are interpreted as being negative (because the Java integer types use two's complement representation).
I got this helpf...
Hello guys, I'm trying to write some code to convert data from a object type field (come from a DataSet) into it's destination (typed) fields. I'm doing (trying at least) it using
dynamic conversion. It seems to work fine for strings, int, DateTime.
But it doesn't work for unsigned types (ulong, uint). Below there's a simple code that ...