How can I add/subtract CSL Value? - cardano

I created a value object from CSL:
const value = Loader.Cardano.Value.new(
Loader.Cardano.BigNum.from_str('0')
);
I want to increase the value of the coin, how can I do it?

Related

Issue with passing an int field in as a parameter argument, but all others work perfectlyy how is this even possible?

ScreenShot of CodeCould someone please explain to me how everything works in this script except a simple int counter that I pass in as a parameter? However if I directly pass in the int counter field into the method instead of using/ref. the para, it works just fine, how is this even possible? HELP!
By default parameters you give to the function, are evaluated and its value is passed (eg. not int xy is passed but the value of int xy, so 5).
So if you change the value directly eg. CounterAi -= 1; you are just changing the value you've passed on not the underlying variable. So if you want to use Pass by Reference in these cases you must use out or ref.
If you change a parameter of the passed value however it's value will be changed without needing to use ref or out.
Example:
public void Example1(int myValue) {
// This won't change the actual variable, just the value of the parameter,
// that has been passed
myValue -= 1;
}
public void Example2(ref int myValue) {
// This will change the actual variable,
// it's changing just the value of the parameter again,
// but we're using pass by reference
myValue -= 1;
}
public void Example3(Transform finishLine) {
// This will change the actual variable,
// because it's changing the data within the object,
// that the parameter value refers to.
finishLine.position = flSpts[Random.Range(0, flSpots.Count)].position;
}

Does Dart reuse DateTime instances?

I'm fiddling around with GlobalObjectKey, where my objects are DateTime instances. I noticed that if I create new instances of the same date and time, I get the same instance "ID":
DateTime newInstance = new DateTime.fromMicrosecondsSinceEpoch(datetime.microsecondsSinceEpoch);
My debug prints say that there are two separate keys with the same id:
[GlobalObjectKey DateTime#f056e] // Key assigned to Widget1
[GlobalObjectKey DateTime#f056e] // Key assigned to Widget2
However, even though the keys seem to be duplicates, I don't seem to get any widget/build errors.
Here's a more complete example of what I'm doing:
class DateTimeKey{
GlobalObjectKey key;
DateTimeKey(DateTime datetime){
DateTime newInstance = new DateTime.fromMicrosecondsSinceEpoch(datetime.microsecondsSinceEpoch);
key = new GlobalObjectKey(newInstance);
}
}
...
List<DateTimeKey> _bookingListMonthKeys = [];
List<DateTimeKey> _bookingListDayKeys = [];
DateTimeKey _monthKey = new DateTimeKey(theDate);
_bookingListMonthKeys.add(_monthKey);
DateTimeKey _dayKey = new DateTimeKey(theDate); // theDate here refers to the same DateTime instance as above
_bookingListDayKeys.add(_dayKey);
Even if I loop through both lists and cross reference them like this
_bookingListDayKeys.forEach((dayKey){
_bookingListMonthKeys.forEach((monthKey){
if( identical(dayKey, monthKey) )
print('Identical DateTimeKeys found: $dayKey, $monthKey');
if( identical(dayKey.key, monthKey.key) )
print('Identical GlobalObjectKeys found: ${dayKey.key}, ${monthKey.key}');
});
});
it doesn't show any duplicates, yet the printouts above clearly have the same "id" (#f056e). Can someone explain what's going on here?
So I think I figured out what's going on here.
First, the hash number in the question comes from the hashCode property of the DateTime instance. This is used for comparisons by the == operator, and hence will be the same for two DateTime instances that represent the same moment in time.
Sidenote: The actual string being printed (#f056e) seems to be the last 5 characters of the hexadecimal representation of the hashCode, which is an int.
However, hashCode is not used by GlobalObjectKey. Instead, it uses the method identityhashCode() from dart:core:
Returns the identity hash code of object.
Returns the same value as object.hashCode if object has not overridden Object.hashCode. > Returns the value that Object.hashCode would return on this object, even if hashCode has > been overridden.
This hash code is compatible with identical.
When creating two different DateTime instances from the same moment in time, their hashCode properties will be equal but the result of identityHashCode() will be different. So basically it seems like identityHashCode() is the closest substitute for an actual "memory address" of an object we will get.
DateTime instance1 = DateTime.now();
DateTime instance2 = DateTime.fromMicrosecondsSinceEpoch(instance1.microsecondsSinceEpoch);
print('hashCode: ${instance1.hashCode}, ${instance2.hashCode} identityHashCode: ${identityHashCode(instance1)}, ${identityHashCode(instance2)}');
will result in:
I/flutter (23321): hashCode: 89064814, 89064814 identityHashCode: 383428552, 594747591
Now it's clear to see that the hashCode is equal for both instances, but identityHashCode(DateTime) is different, meaning that they are in fact separate instances but equal when comparing their hashCode property or doing a == operation.
Why printing a GlobalObjectKey would print the hashCode of its value is still beyond me though, that's what threw me off from the start.

Flutter - Add decimals on a dynamic value field

In Flutter i have a dropdown (we can call maxim field) to select total length. Then in index field we can add values with the same length of dropdown. When i add the value in index field i need to add '.000'.
I try to use masked package but i can't concatenate the index field value.
The code is similar to
int inputMaxValue=1;
var inputIndexValue = new MaskedTextController(mask: inputMaxValue.text+'.000');
Return
Only static members can be accessed in initializers
¿Anybody know can i fix this problem?
I think MaskedTextController is unnecessary in your case, simply change int inputMaxValue to double inputMaxValue and possibly add .toStringAsFixed(3) to achieve 3 decimals after the dot.
e.g.
double inputMaxValue = 1.0;
String inputIndexValue = "${inputMaxValue.toStringAsFixed(3)}";
However, if you want to keep the flutter_masked_text package - you should avoid using dynamic strings for the mask property. Error Only static members can be accessed in initializers appears supposedly because you declare the masked controller variable within the class, not in it's enclosing methods (e.g. initState).
In case of MaskedTextController, only static strings should be used for the mask property.
e.g. this is what I use to format phone number field:
MaskedTextController(mask: "(000) 000-0000")
maybe NumberFormat can help
import 'package:intl/intl.dart';
final price = 123;
final formater = NumberFormat("#,##0.000");
print(formater.format(price));
output: 123.00

C++Builder - cannot cast from 'AnsiString' to 'TObject'

I have a problem with converting a string variable to TObject.
I have a query that returns two columns to me. In the first column I have varchar values that I translate into strings, and in the second column I have int values.
I want to fill a ComboBox in this way with these values:
cbx1-> AddItem (DataSet1->DataSet->Fields->Field[0]->AsString, (TObject *) (int) DataSet1->DataSet->Fields->Field[1];
As I refer to the second value which is int type, I receive some bushes, e.g., xD, etc.
By trying to convert this value to string, eg:
String temp = IntToStr (DataSet1->DataSet->Fields->Field[1]);
cbx1-> AddItem (DataSet1->DataSet->Fields->Field[0]->AsString, (TObject *) temp;
I receive an error message:
cannot cast from 'AnsiString' to 'TObject'
I do not know what further I can do to convert this value.
You cannot cast an AnsiString value to a TObject* pointer. You can only cast an integer value, or a pointer value, to a TObject* pointer. AnsiString is neither of those.
You are not retrieving the int value from the 2nd field correctly anyway. Field[1] is a pointer to an actual TField object in the Fields collection. That pointer is what you are trying to store in your ComboBox, NOT the int value that the TField represents.
You need to call Fields[1]->AsInteger to get the int value of the 2nd field, similar to how you use Fields[0]->AsString to get the string value of the 1st field:
cbx1->AddItem(
DataSet1->DataSet->Fields->Field[0]->AsString,
(TObject*) DataSet1->DataSet->Fields->Field[1]->AsInteger
// in C++, using reinterpret_cast is preferred over C-style casting:
// reinterpret_cast<TObject*>(DataSet1->DataSet->Fields->Field[1]->AsInteger)
);
This is no different than the code in your previous question:
cbx1->AddItem("one",(TObject*)1);
You are now just placing the literals "one" and 1 with runtime variables of equivalent types.

Trouble understanding private attributes in classes and the class property method in Python 3

This class example was taken from here.
class Celsius:
def __init__(self, temperature = 0):
self.temperature = temperature
def to_fahrenheit(self):
return (self.temperature * 1.8) + 32
def get_temperature(self):
print("Getting value")
return self._temperature
def set_temperature(self, value):
if value < -273:
raise ValueError("Temperature below -273 is not possible")
print("Setting value")
self._temperature = value
temperature = property(get_temperature, set_temperature)
The idea here is that when we create an instance of Celsius and set the temperature attribute (e.g. foo = Celsus (-1000) ), we want to make sure that the attribute is not less than -273 BEFORE setting the temperature attribute.
I don't understand how it seems to bypass self.temperature = temperature and go straight to the last line. It seems to me like there are three attributes/properties created here: the Class attribute, temperature; the Instance attribute, temperature; and the set_temperature function which sets the attribute _temperature.
What I DO understand is that the last line (the assignment statement) must run the code property(get_temperature, set_temperature) which runs the functions get_temperature and set_temperature and intern sets the private attribute/property _temperature.
Moreover, if I run: foo = Celsius(100) and then foo.temperature, how is the result of foo.temperature coming from temperature = property(get_temperature, set_temperature) and thus _temperature AND NOT self.temperature = temperature? Why even have self.temperature = temperature if temperature = property(get_temperature, set_temperature) gets ran every time the foo.temperature call is made?
More questions...
Why do we have two attributes with the same name (e.g. temperature) and how does the code know to retrieve the value of _temperature when foo.temperature is called?
Why do we need private attributes/properties an not just temperature?
How does set_temperature(self, value) obtain the attribute for parameter value (e.g. the argument that replaces value)?
In short, please explain this to me like a three year old since I have only been programming a few months. Thank you in advance!
When we are first taught about classes/objects/attributes we are often told something like this:
When you look up an attribute like x.foo it first looks to see if
'foo' is an instance variable and returns it, if not it checks if
'foo' is defined in the class of x and returns that, otherwise an
AttributeError is raised.
This describes what happens most of the time but does not leave room for descriptors. So if you currently think the above is all there is about attribute lookup property and other descriptors will seem like an exception to these rules.
A descriptor basically defines what to do when looking up / setting an attribute of some instance, property is an implementation that lets you define your own functions to call when getting / setting / deleting an attribute.
When you do temperature = property(get_temperature, set_temperature) you are specifying that when x.temperature is retrieved it should call x.get_temperature() and the return value of that call will be what x.temperature evaluates to.
by specifying set_temperature as the setter of the property it states that when ever x.temperature is assigned to something it should call set_temperature with the value assigned as an argument.
I'd recommend you try stepping through your code in pythontutor, it will show you exactly when get_temerature and set_temperature are called after which statements.