edit: solved, sorry about this, was due to a typo.
This code.
List<Tuple<Int16, Int16>> a = new List<Tuple<Int16, Int16>>();
Tuple<UInt16, UInt16> b = Tuple.Create<UInt16, UInt16>(4, 2);
a.Add(b);
Produces the following error for a.Add(b)
The best overloaded method match for
'System.Collections.Generic.List<System.Tuple<short,short>>
.Add(System.Tuple<short,short>)'
has some invalid arguments.
In short
List<Tuple<short,short>>.Add(Tuple<short,short>)
has invalid arguments
I can't see how this is.
Tuple<Int16, Int16> and Tuple<UInt16, UInt16> are two different type of tuple.
You are trying to add an UInt16 pair to a list of Int16 pairs. That doesn't work.
You can add an Int16 pair to a list of Int16 pairs:
List<Tuple<Int16, Int16>> a = new List<Tuple<Int16, Int16>>();
Tuple<Int16, Int16> b = Tuple.Create<Int16, Int16>(4, 2);
a.Add(b);
UInt is not an Int
Reference: http://msdn.microsoft.com/en-us/library/yht2cx7b.aspx
It's telling you exactly the problem and the solution. Try the short instead of unsigned short
Related
I want to do the following but the only issue is that my input query is int not string and also I am searching from my RoomID which is also an integer and not a string. Let me simplify it, instead of _context.Customers.Name, I am comparing from _context.Room.Id which is an int type. This is an obligation and I have to do this. Guys ignore the .Select(Mapper.Map<>) Method, the primary focus is the int problem. I'd appreciate some help.
One way to solve it is to convert the integers to string and do a contain.
Ex.
var needle = 234;
var haystack = 79234826;
var contains = haystack.ToString().Contains(needle.ToString());
If you want to search as if both ints are strings, just let them be strings:
int source = 79234826;
int toFind = 234;
bool found = source.ToString().Contains(toFind.ToString());
I have two sets of dictionaries that each contain the same keys and have initialized values.
Using unsafe code, I would like to swap their addresses:
Dictionary<string, List<object>> d1 = ...
Dictionary<string, List<object>> d2 = ...
unsafe void SwapEntries(string index)
{
int* tmp = &d1[index];
&d1[index] = &d2[index]
&d2[index] = tmp;
}
Assuming I've recalled my pointer arithmetic properly, the output I'm looking for would be this:
d1 = new Dictionary<string, List<int>>() { "a", { 1, 2, 3 } };
d2 = new Dictionary<string, List<int>>() { "a", { 4, 5, 6 } };
SwapEntries("a");
Console.WriteLine(d1["a"]); //4,5,6
Console.WriteLine(d2["a"]); //1,2,3
However, when I try to write this, I get the compile error "Cannot take the address of the given expression."
1) Is there a faster way of performing the address swap that I've missed? Performance is the only priority here.
2) Is my pointer arithmetic correct?
3) Do I need to move to a wrapper or a different data structure entirely in order to be able to perform the address swap as described?
I agree with Martin Ullrich's answer.
The expression d1[index] is not a variable. It is an invocation of the get accessor of the indexer defined by Dictionary<,>. You cannot take a pointer to that with the & operator.
Besides, in this case, the type of it is List<object>. You can only take pointers to value types, and List<> is a class type.
Even if you did have the true storage location, and it was of type object[], it would still be impossible since the element type of the array is object. So arr[0] (corresponding to d1[index][0]) would be a class type again, and you cannot take the address of that.
Scott Chamberlain's comment to your question gives an easy approach. Just use
void SwapEntries(string index)
{
var tmp = d1[index];
d1[index] = d2[index];
d2[index] = tmp;
}
This just involves passing around references to the two existing List<object> instances in question.
Automatic pointers to dictionary members aren't supported - they only work for Arrays or data types that use C# 7's "ref return" feature for indexers, properties or methods.
If you wanted to actually take the ref addresses of the two locations, there is now an option for it
CollectionsMarshal.GetValueRefOrNullRef(d1, "a")
So if you had a Swap function which accepted pointers:
void Swap<T>(ref T a, ref T b)
{
var tmp = a;
a = b;
b = tmp;
}
You could call it like this
Swap(ref CollectionsMarshal.GetValueRefOrNullRef(d1, "x"),
ref CollectionsMarshal.GetValueRefOrNullRef(d2, "x"));
shaplab
The benefit of this over just using normal dictionary indexers is that you only look up each location once, rather than once for get and once for set.
I want to know whether we can give the index of the string as Long data type.
var i=long.Parse(Console.ReadLine());
var result = testString[i-1];
the second line giving me the error by saying that "The best overloaded method match for 'string.this[int]' has some invalid arguments."
No you can't use long for most collection types (you haven't specified what testString is).
One way to get around this would be to segregate the string into a multi-part / multi-dimension array then use a multiplier to get which part of the array to check.
For example:
Your index is 100,000 and you have an array of shorts (32,767 length)...
string[,] testString = new string[100, 32766]; //Replace this with your Initialisation / existing string
var arrayRank = (int)Math.Round((double) 100000 / 32767, 0);
var arrayIndex = (int)Math.Round((double)100000 % 32767, 0);
//Test this works.
//testString[arrayRank, arrayIndex] = "test"; - Test to see that the array range is assignable.
var result = testString[arrayRank, arrayIndex]; //Test value is what we expect
This may not be the most efficient way to go about things, but it is a workaround.
No, it cannot accept a long. The only overload accepts an int indexer. You would need to change your code to int.Parse() instead of long.Parse()
There is no way to pass long as an index of array, compiler doesn't allow that.
Workaround can be converting the long to int, this is called narrow conversion.
var result= testString[(int)i)];
I need to convert a string to an integer in windows Phone 7. I tried following code but my app crashed:
int val1 = Convert.ToInt16(str);
Is there another way to do this?
Why does it crash? Do you get an errormessage? What is the actual value of 'str'? Maybe the problem lies there. Also I would opt for ToInt32() as this returns an int, while ToInt16() returns a short. This will not give a problem but it looks more logic. And last, these methods will do the job:
int val1 = Convert.ToInt32("123");
int val2 = int.Parse("123");
int val3 = 0;
int.TryParse("123", out val3);
To convert string to integer, use
string text = "500";
int num = int.Parse(text);
Have you tried
string str = "155";
int retVal;
if(int.TryParse(str, out retVal))
{
// You can now code with it
}
try Convert.ToInt32(str)
Think this will help :)
I think int.TryParse is the way to go. This way you will get to know if the parsing was successful or not by checking the return value. The advantage is since it does not throw any exception you don't have to write any error handling code which makes it simpler and less error prone. I do not personally like code that throws a lot of exceptions as one might easily goof up whilst handling them.
If you are sure to use this on a 32 bit platform, you can try Int32.TryParse() , or if 64- bit platform you may use Int64.TryParse()
How can I convert from an ASP.NET Unit structure to int in c#? Or reverse?
The Unit type has a Value property. This is a double, but you can cast it to an int if you want. The casting may cause a loss of precision, but you are probably aware of that.
To create a Unit just use the constructor that takes an int.
If you mean the Unit class:
The Unit class can represent values
only between -32768 and 32767.
But it depends if you want the Pixel or Percentage value.
myUnit.Value will get the value as pointed out.
Use the constructor public Unit(int value) to convert back.
If you mean a uint: there's 2 possible obvious ways:
int n = Convert.ToInt32(myUint);
int n = (int)myUint;
For ASP.NET Unit:
unit.IsEmpty ? 0 : Convert.ToInt32(unit.Value);
Use Unit.Value property. It will return double and you can cast it to int
Something like (int)xyz.Value
WEhere xyz is the unit variable
To convert int to unit use new Unit(value)
Probably he need this:
int myInt = 1;
uint myUint = (uint)myInt;
uint myUint = 1;
int myInt = (int)myUint;
The Value property returns a dobule, that you can convert to an integer:
int h = (int)someControl.Height.Value;
However, the conversion might not make sense for some unit types. If you don't know for certain that the unit is of a specific type, you would want to check the Type property first.
Convert.Toint32( UInt );
I guess u meant UInt not Unit
EDIT : Ok thought you meant uint sorry