Solidity casting from "uint256" to "address" - type-conversion

I would like to know how I can convert a uint256 data type to address on Solidity latest versions.
Here's an example of the code that I'm developing.
function setDetails(string memory _name) public onlyAuthCaller returns(address){
uint256 tmpData = uint256(keccak256(abi.encodePacked(msg.sender, block.timestamp)));
address batchNo = address(tmpData);
detailsData.name = _name;
batchDetails[batchNo] = detailsData;
nextAction[batchNo] = 'NEXT';
return batchNo;
}
On Remix, I'm having a TypeError: Explicit type conversion not allowed from "uint256" to "address" on line: address batchNo = address(tmpData);
If someone could help me solve this error for casting these data types.
Regards

Might be because uint256 is too large, and needs to be truncated.
Taken from the docs:

Related

In Solidity Language, what happens when a function with given parameters on different contracts but different types of data stored for Interface?

For Solidity Programing Language,
While defining Interface, we name it as per our needs and then call it as needed with the same types of inputs.
What happens when there are the same function names with the same parameter names but different types of data stored?
Eg. Here are two different contracts stored on Ethereum Blockchain separately. What will happen if while writing a 3rd smart contract, I create an interface and call function with name "favoriteNumber". Which value will it return? 10 or 15?
contract A {
uint randomnumber = 10;
function favoriteNumber (uint _number) public view returns(uint randomnumber) {
}
}
contract B {
uint randomnumber = 15;
function favoriteNumber (uint _number) public view returns(uint randomnumber) {
}
}
It will depends on what contract you call, each contract have it unique address with each own storage

Object of type 'MyClass' cannot be converted to type 'System.Object[]' C#

I've been looking into this for a couple of hours but so far haven't gotten any luck.
Here's my C# code:
myClassInstance = new MyClass("MyParam", 1);
object[] args = new object[1] { myClassInstance };
MethodInfo methodInfo = GetType().GetMethod(myMethod, BindingFlags.NonPublic | BindingFlags.Instance);
string method = (string)methodInfo.Invoke(this, args);
I have MethodInfo and System.Reflection imported. The Unity error is this:
ArgumentException: Object of type 'SystemController' cannot be converted to type 'System.Object[]'
It doesn't point to a specific line in the code, but from what I can tell it seems to be an issue with converting the myClassInstance variable to an object, which doesn't make sense to me, as I believed everything in C# inherited from System.Object.
Here is MyClass:
public class MyClass
{
public string var1;
public int var2;
public MyClass(string param1, int param2)
{
var1 = param1;
var2 = param2;
}
}
Clearly, I'm not showing the entire class, but the only difference is that there are more variables and parameters to store. Those shouldn't change anything, so I won't bore you with them. It's just a class with a constructor, not inheriting from anything.
Any help I could get with this would be greatly appreciated. Let me know if you need more info.
The error here was me trying to pass the entire object[] array into my method as a parameter when I should have only passed the contents of the array. See here:
I was doing this:
void MyMethod(object[] args) {
MyClass instance = (MyClass)args[0];
...
}
But should've done this:
void MyMethod(MyClass myClassInstance) {
...
}
After reading some more documentation and reviewing the comments above I discovered that the .Invoke() method passes what's inside the args array instead of the entire array. At least, that's my current understanding, and it's what made my code work.
Thanks for the help.

How to file a bug against Salesforce Apex?

Apex Workbench reports compile error on the code
double d = null;
system.debug(d instanceof double);
COMPILE ERROR: Operation instanceof is always true since an instance of Double is always an instance of Double
This is clearly wrong, because null is not an instance of double:
object d = null;
system.debug(d instanceof double);
19:32:24.3 (4343472)|USER_DEBUG|[2]|DEBUG|false
Where can I file a bug report?
When we use Primitive data type we always know what type we declared, either we create a variable or we pass to any function (in both scenario).
So salesforce is not allowing you to check for what you have manually declared, You can only check when data type is assigned dynamically, Below example will clear the thought :
decimal doubleVariable = 0;
Object obj = doubleVariable;
system.debug(obj instanceof decimal);
String stringVariable = 'teststring';
obj = stringVariable;
system.debug(obj instanceof decimal);
system.debug(obj instanceof string);
Output :
true
false
true

Allowing nulls on a record type F#

I'm trying to make a type extension for MongoDB C# Driver that will return an option type instead of null when trying to execute a query that yields 0 results.
I've run into a few issues on the way, but right now there is just one thing in the way.
Here is the code
[<Extension>]
type Utils () =
[<Extension>]
static member inline tryFindOne(x: MongoCollection<'T>, query) =
match x.FindOne(query) with
| null -> None
| value -> Some value
[<CLIMutable>]
type Entity =
{ Id : ObjectId; Name : string }
static member Create(name) =
{ Id = ObjectId(); Name = name }
The problem of course is that the record type Entity to the F# compiler does not conform to the type constraint of the extension method('T : null) but I have to have the contraint to be able to pattern match against nulls. Of course it's sort of a nonsensical thing because the type Entity is very much "nullable" for interop purposes and will be returned as null whenever you try to query a MongoDB collection which yields 0 results. I tried to set the attribute [<AllowNullLiteral>] but unfortunately it only works with classes. So alas I'm stuck, I could make Entity into a class instead but I think records are more idiomatic F#.
I think the following should work:
[<Extension>]
type Utils () =
[<Extension>]
static member inline tryFindOne(x: MongoCollection<'T>, query) =
let theOne = x.FindOne(query);
if (box theOne = null) None else Some(theOne)
I have borrowed the idea from Sergey Tihon's post here:
https://sergeytihon.wordpress.com/2013/04/10/f-null-trick/
Alternative way to do the null check without boxing:
[<Extension>]
type Utils () =
[<Extension>]
static member inline tryFindOne(x: MongoCollection<Entity>, query) =
let theOne = x.FindOne(query)
if obj.ReferenceEquals(theOne, null) then None else Some(theOne)

Ext-gwt (gxt) TextField getFieldValue() problem

I have an TextField with datatype Integer, so I am trying to getFieldValue() and write it to Integer field. So in runtime I have an error here:
TextField<Integer> priceField = new TextField<Integer>();
Integer newPriceFieldValue = priceField.getValue(); //here is an error in runtime
So I cant understand whats the problem - proceField.getValue() should be Integer, why string? Maybe I should another type of Field?
java.lang.ClassCastException: java.lang.String cannot be cast to
java.lang.Integer
at
ru.braginini.client.ProductForm$2.componentSelected(ProductForm.java:64)
at
ru.braginini.client.ProductForm$2.componentSelected(ProductForm.java:1)
If you are expecting only numbers to be used in this field NumberField may be the better choice.
NumberField field = new NumberField();
field.setPropertyEditorType(Integer.class);
It will ensure only numbers are entered, and save you some casting & error handling on the getValue() call.
getValue returns a String!
You want to assign this String to an Integer which causes an CastException (like it would in any Type oriented Programming language.
Try
Integer newPriceFieldValue = Integer.parseInt(priceField.getValue());
Regards,
Stefan