C# Indexers with Ref Return Gets that also Support Sets - ref

Am I doing something wrong here, or as of C# 7.2 Indexers that return by ref and allow set are not supported?
Works:
public ref byte this[int index] {
get {
return ref bytes[index];
}
}
Works too:
public byte this[int index] {
get {
return bytes[index];
}
set {
bytes[index] = value;
}
}
Fails:
public ref byte this[int index] {
get {
return ref bytes[index];
}
set { //<-- CS8147 Properties which return by reference cannot have set accessors
bytes[index] = value;
}
}
Fails too:
public ref byte this[int index] {
get {
return ref bytes[index];
}
}
public byte this[int index] { //<-- CS0111 Type already defines a member called 'this' with the same parameter types
set {
bytes[index] = value;
}
}
So, is there no way to have a ref return yet allow the indexer also support Set?

As #IvanStoev correctly pointed out, there is no need for set, since the value is returned by reference. Therefore the caller of the indexer has complete control over the returned value and can therefore can assign it a new value, with the changes being reflected in the underlying data structure (whose indexer was being called) since the value was returned by reference and not by value.

Related

launchDate2 is also a DateTime? type. Why the heck it could be passed to a function that requires a DateTime argument?

We couldn't pass a DateTime? value to a function that requires a DateTime argument.
The code below results in an error.
class Spacecraft {
String name;
DateTime? launchDate;
// Read-only non-final property
int? get launchYear => launchDate?.year;
// Constructor, with syntactic sugar for assignment to members.
Spacecraft(this.name, this.launchDate) {
// Initialization code go here.
}
// Named constructor that forwards to the default one.
Spacecraft.unlaunched(String name) : this(name, null);
// Method
void describe() {
print('Spacecraft: $name');
if (launchDate != null) {
int years = DateTime.now().difference(launchDate).inDays ~/ 365;
print('Launched: $launchYear ($years years ago)');
} else {
print('Unlaunched');
}
}
}
The argument type 'DateTime?' can't be assigned to the parameter type
'DateTime'.
However, this worked:
class Spacecraft {
String name;
DateTime? launchDate;
// Read-only non-final property
int? get launchYear => launchDate?.year;
// Constructor, with syntactic sugar for assignment to members.
Spacecraft(this.name, this.launchDate) {
// Initialization code go here.
}
// Named constructor that forwards to the default one.
Spacecraft.unlaunched(String name) : this(name, null);
// Method
void describe() {
print('Spacecraft: $name');
// Type promotion doesn't work on getters
var launchDate2 = launchDate;
if (launchDate2 != null) {
int years = DateTime.now().difference(launchDate2).inDays ~/ 365;
print('Launched: $launchYear ($years years ago)');
} else {
print('Unlaunched');
}
}
}
And heck, launchDate2 is also a DateTime? type. Why the heck it could be passed to a function that requires a DateTime argument?
launchDate refers to a property so it couldn't be promoted or cast. See http://dart.dev/go/non-promo-property and also https://dart.dev/null-safety/understanding-null-safety

dart nullability checking method [duplicate]

This question already has answers here:
"The operator can’t be unconditionally invoked because the receiver can be null" error after migrating to Dart null-safety
(3 answers)
Closed 12 months ago.
I have migrated my Dart code to NNBD / Null Safety. Some of it looks like this:
class Foo {
String? _a;
void foo() {
if (_a != null) {
_a += 'a';
}
}
}
class Bar {
Bar() {
_a = 'a';
}
String _a;
}
This causes two analysis errors. For _a += 'a';:
An expression whose value can be 'null' must be null-checked before it can be dereferenced.
Try checking that the value isn't 'null' before dereferencing it.
For Bar() {:
Non-nullable instance field '_a' must be initialized.
Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'.
In both cases I have already done exactly what the error suggests! What's up with that?
I'm using Dart 2.12.0-133.2.beta (Tue Dec 15).
Edit: I found this page which says:
The analyzer can’t model the flow of your whole application, so it can’t predict the values of global variables or class fields.
But that doesn't make sense to me - there's only one possible flow control path from if (_a != null) to _a += 'a'; in this case - there's no async code and Dart is single-threaded - so it doesn't matter that _a isn't local.
And the error message for Bar() explicitly states the possibility of initialising the field in the constructor.
The problem is that class fields can be overridden even if it is marked as final. The following example illustrates the problem:
class A {
final String? text = 'hello';
String? getText() {
if (text != null) {
return text;
} else {
return 'WAS NULL!';
}
}
}
class B extends A {
bool first = true;
#override
String? get text {
if (first) {
first = false;
return 'world';
} else {
return null;
}
}
}
void main() {
print(A().getText()); // hello
print(B().getText()); // null
}
The B class overrides the text final field so it returns a value the first time it is asked but returns null after this. You cannot write your A class in such a way that you can prevent this form of overrides from being allowed.
So we cannot change the return value of getText from String? to String even if it looks like we checks the text field for null before returning it.
An expression whose value can be 'null' must be null-checked before it can be dereferenced. Try checking that the value isn't 'null' before dereferencing it.
It seems like this really does only work for local variables. This code has no errors:
class Foo {
String? _a;
void foo() {
final a = _a;
if (a != null) {
a += 'a';
_a = a;
}
}
}
It kind of sucks though. My code is now filled with code that just copies class members to local variables and back again. :-/
Non-nullable instance field '_a' must be initialized. Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'.
Ah so it turns out a "field initializer" is actually like this:
class Bar {
Bar() : _a = 'a';
String _a;
}
There are few ways to deal with this situation. I've given a detailed answer here so I'm only writing the solutions from it:
Use local variable (Recommended)
void foo() {
var a = this.a; // <-- Local variable
if (a != null) {
a += 'a';
this.a = a;
}
}
Use ??
void foo() {
var a = (this.a ?? '') + 'a';
this.a = a;
}
Use Bang operator (!)
You should only use this solution when you're 100% sure that the variable (a) is not null at the time you're using it.
void foo() {
a = a! + 'a'; // <-- Bang operator
}
To answer your second question:
Non-nullable fields should always be initialized. There are generally three ways of initializing them:
In the declaration:
class Bar {
String a = 'a';
}
In the initializing formal
class Bar {
String a;
Bar({required this.a});
}
In the initializer list:
class Bar {
String a;
Bar(String b) : a = b;
}
You can create your classes in null-safety like this
class JobDoc {
File? docCam1;
File? docCam2;
File? docBarcode;
File? docSignature;
JobDoc({this.docCam1, this.docCam2, this.docBarcode, this.docSignature});
JobDoc.fromJson(Map<String, dynamic> json) {
docCam1 = json['docCam1'] ?? null;
docCam2 = json['docCam2'] ?? null;
docBarcode = json['docBarcode'] ?? null;
docSignature = json['docSignature'] ?? null;
}
}

error: Return value transfers ownership but method return type hasn't been declared to transfer ownership

The following code:
public string add_button_tooltip_markup {
get { return add_button.get_tooltip_markup (); }
set { add_button.tooltip_markup = value; }
}
Gives me the following error:
error: Return value transfers ownership but method return type hasn't been declared to transfer ownership
get { return add_button.get_tooltip_markup (); }
I'm using Vala 0.40.10.
What's the best way to solve this in Vala?
Mark the getter as owned, as follows:
public string add_button_tooltip_markup {
owned get { return add_button.get_tooltip_markup (); }
set { add_button.tooltip_markup = value; }
}
See the following page for more detail:
https://wiki.gnome.org/Projects/Vala/ReferenceHandling

Custom ValidationAttribute for multiple data types

I have a ValidationAttribute like below which validates that a certain amount of values have been entered on a form. Currently it is only being used on a property with type short?[]
public class RequiredArrayLength : ValidationAttribute
{
public int TotalRequired { get; set; }
public override bool IsValid(object value)
{
if(value != null)
{
var array = value as short?[];
return array.Where(v => v.HasValue).Count() >= TotalRequired;
}
return false;
}
}
Is there a way I can modify this ValidationAttribute so it will work with other numeric arrays such as int?[]
One option would be to cast to IEnumerable (System.Collections namespace) and enumerate the collection to determine the number of items in the collection.
IEnumerable collection = value as IEnumerable;
if (collection!= null)
{
IEnumerator enumerator = collection.GetEnumerator();
int count = 0;
while(enumerator.MoveNext())
{
count++;
}
return count >= TotalRequired;
}
return false;
If you only want to count non-null values, then modify the code to
while(enumerator.MoveNext())
{
if (enumerator.Current != null)
{
count++;
}
}
If you specifically wanted to limit this to only numeric data types, you can use the .GetType() method of IEnumerable to test the type (for example, C# - how to determine whether a Type is a number).

Entry value converter hangs converting and converting back again and again

I have an Entry which holds the Price and I wanted to format it as currency.
Here is the Entry tag
<Entry x:Name="Price" StyleId="Price"
Text="{Binding Model.Price, Converter={StaticResource CurrencyEntryFormatConverter}, Mode=TwoWay}"
Placeholder="{x:Static resx:Resources.PricePlaceholder}"
Style="{StaticResource DefaultEntry}" Keyboard="Numeric"/>
and here is the property in the Model
public decimal Price
{
get
{
return this.price;
}
set
{
if (this.price== value)
{
return;
}
this.price= value;
this.OnPropertyChanged();
}
}
Finally here is the converter:
public class CurrencyEntryFormatConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
{
return value;
}
string result = string.Format(Resources.CurrencyFormatString, (decimal)value);
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
{
return 0;
}
string result = value.ToString().Replace(" ", "").Replace("$", "").Replace(",", "");
return result;
}
}
Question: My problem is that when I run the project and try to enter values in the Price field, the code repeats executing between Convert and ConvertBack functions of the converter and application hangs!
Any advice?
In my case the problem was the property implementation
If we define a property of a class which is implementing INotifyPropertyChanged, in order to update the view when the property value changes, we need to call OnPropertyChanged method in the set block:
public decimal Amount
{
get
{
return this.amount;
}
set
{
this.amount = value;
this.OnPropertyChanged(); // like this line
}
}
But just this code like so makes a loop with your bindings. So we need to be checking if the value is different than the current property's value and then if its new, update it. Look at this code:
public decimal Amount
{
get
{
return this.amount;
}
set
{
if (this.amount == value)
{
return;
}
this.amount = value;
this.OnPropertyChanged();
}
}
That if block helps you to stop looping between get and set.
I hope it helps someone.

Resources