Loop Through List

Yusuf 691 Reputation points
2022-07-11T02:57:32.223+00:00

Hi
I am try to loop through generic list, I get different errors

This works with static text

namespace MauiApp  
{  
    public class GraphicsDrawable : IDrawable  
    {  
        public void Draw(ICanvas canvas, RectF dirtyRect)  
        {  
            for (var i = 0; i < 3; i++)  
            {  
                canvas.DrawString("Static Text".ToString(), 50, 50 + 20 * i, HorizontalAlignment.Center);  
            }  
        }  
    }  
}  

This gives the runtime error when using Async and await

using MauiApp.Helper;  
  
namespace MauiApp  
{  
    public class GraphicsDrawable : IDrawable  
    {  
        FirebaseHelper firebaseHelper = new FirebaseHelper();  
  
        public long DepartmentId { get; set; }  
        public async void Draw(ICanvas canvas, RectF dirtyRect)  
        {  
            var employee = await firebaseHelper.GetDepartmentEmployees(DepartmentId); // System.Collections.Generic.List<>  
            for (var i = 0; i < employee.Count; i++)  
            {  
                canvas.DrawString(employee[i].FirstName.ToString(), 50, 50 + 20 * i, HorizontalAlignment.Center); //System.ObjectDisposedException: 'The object has been closed. (0x80000013)'  
  
            }  
        }  
    }  
}  

compile error when not using Async and await,

using MauiApp.Helper;  
namespace MauiApp  
{  
    public class GraphicsDrawable : IDrawable  
    {  
        FirebaseHelper firebaseHelper = new FirebaseHelper();  
  
        public long DepartmentId { get; set; }  
        public  void Draw(ICanvas canvas, RectF dirtyRect)  
        {  
            var employee =  firebaseHelper.GetDepartmentEmployees(DepartmentId); // System.Collections.Generic.List<>  
            for (var i = 0; i < employee.Count; i++)   // CS0019  Operator '<' cannot be applied to operands of type 'int' and 'method group'   
  
            {  
canvas.DrawString(employee[i].FirstName.ToString(), 50, 50 + 20 * i, HorizontalAlignment.Center);    // CS0021  Cannot apply indexing with[] to an expression of type 'Task<List<Table>>'     
  
       }  
     }  
   }  
}  
  

How to solve the error?
Thanks in advance.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,927 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,309 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,126 questions
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 56,931 Reputation points
    2022-07-20T13:58:48.217+00:00

    I looked at you code. The Draw method of IDrawable does not support being async (not very modern design), so you can not use async operations inside the method. Extract the async operations to be external to the method, and trigger a redraw when complete.


1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,931 Reputation points
    2022-07-12T14:42:27.713+00:00

    Most likely the code that calls Draw() is not using await, and the canvas variable is disposed before Draw() can use it.

    that is you must convert the calling module to async:

    var gd = new GraphicsDrawable();
    ....
    await gd.Draw(canvas, dirtyRect);

    0 comments No comments