Unhandled exception in ToString overload

Hallo and thanks for your time.

I recently decided to try using Xamarin.Android to develop an idea that I had.

However, I ran into the strangest problem I have ever encountered.

public class Note : INote { public string Content { get; set; } public DateTime DateTime { get; set; } public List<ITag> Tags { get; set; } public override string ToString() { try { const int maxLength = 20; if (Content.Length > maxLength) { return Content.Substring(0, maxLength - 1); } return Content; } catch (Exception) { return Content; } } } 

In the above class, when I do a ToString operation for a note object with less than 20 characters, I get an unhandled exception. I thought it was rather strange, so I wrapped the part with a substring in try / catch.

However, I still get an unhandled exception. How can it be?

Unhandled Exception - ArgumentOutOfRangeException

EDIT: The exception window also looks werid. Usually they look like this. Intentional exception

ToString is called when the ListView is populated, which runs in this piece of code.

 [Activity(Label = "@string/ApplicationName")] public class ShowNotesActivity : Activity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.ShowNotes); var persistence = new Persistence(); var listView = FindViewById<ListView>(Resource.Id.listView1); var adapter = new ArrayAdapter<INote>(this, Android.Resource.Layout.SimpleListItem1, persistence.GetAllNotes()); listView.Adapter = adapter; } } 
+5
source share
1 answer

Here is the code I tested in Visual Studio and confirmed that it works:

 public class Card { public string Content { get; set; } public override string ToString() { if (Content == null) return null; const int maxLength = 20; if (Content.Length > maxLength) return Content.Substring(0, maxLength - 1); return Content; } } 

Here is what I used for testing (the utility method is one of my own methods from my isolated software):

 var card = new Card(); var str = Utility.GetRandomAlphaNumString(40); for (int i = 0; i < 40; i++) { card.Content = str.Substring(0, i); Console.WriteLine(card.ToString()); } 

Here is what he printed:

 // [Empty result] W W1 W1U W1UJ W1UJ2 W1UJ2X W1UJ2X4 W1UJ2X48 W1UJ2X48B W1UJ2X48BU W1UJ2X48BU9 W1UJ2X48BU9A W1UJ2X48BU9A4 W1UJ2X48BU9A46 W1UJ2X48BU9A46C W1UJ2X48BU9A46CY W1UJ2X48BU9A46CYV W1UJ2X48BU9A46CYVA W1UJ2X48BU9A46CYVA8 W1UJ2X48BU9A46CYVA8R W1UJ2X48BU9A46CYVA8 W1UJ2X48BU9A46CYVA8 W1UJ2X48BU9A46CYVA8 W1UJ2X48BU9A46CYVA8 W1UJ2X48BU9A46CYVA8 W1UJ2X48BU9A46CYVA8 W1UJ2X48BU9A46CYVA8 W1UJ2X48BU9A46CYVA8 W1UJ2X48BU9A46CYVA8 W1UJ2X48BU9A46CYVA8 W1UJ2X48BU9A46CYVA8 W1UJ2X48BU9A46CYVA8 W1UJ2X48BU9A46CYVA8 W1UJ2X48BU9A46CYVA8 W1UJ2X48BU9A46CYVA8 W1UJ2X48BU9A46CYVA8 W1UJ2X48BU9A46CYVA8 W1UJ2X48BU9A46CYVA8 W1UJ2X48BU9A46CYVA8 

(Note that he did not actually print “Empty result.” I just had to type something in there, because otherwise SO would not allow me to run a block of code using a string of spaces.)

Since the code above works just fine, I can say that regardless of your problem, it has nothing to do with the code. (Or at least this code.)

0
source

Source: https://habr.com/ru/post/1263042/


All Articles