none
Why can't I use a variable in a Do While loop RRS feed

  • Question

  • Hello,

    I have an Asp.Net, Core 2.1, MVC, C# Web application in Visual Studio 2017 15.7.5.

    I have this code:

    var model = new AccessModel(); model.UserAccessModel = _context.UserAccessModels.Single(a => a.ID == 1); var accessToken = (model.UserAccessModel.AccessToken ?? TempData["accessToken"]).ToString(); var serverUrl = (model.UserAccessModel.ServerUrl ?? TempData["serverUrl"]).ToString(); var nopApiClient = new ApiClient(accessToken, serverUrl); MultipleProductModel multipleProductModel = new MultipleProductModel(); List<Category> categories = new List<Category>(); string jsonUrl = $"/api/products/count"; object productsCount = nopApiClient.Get(jsonUrl); var nopProductsCount = JsonConvert.DeserializeObject<ProductsCount>(productsCount.ToString()); int sinceID = 0; do { jsonUrl = $"/api/products?limit=250&since_id=" + sinceID.ToString() + $"&fields=id,sku,name,images,categories"; object productsData = nopApiClient.Get(jsonUrl); var productsRootObject = JsonConvert.DeserializeObject<ProductsRootObject>(productsData.ToString()); multipleProductModel.MProductsApi.AddRange(productsRootObject.Products); var last = multipleProductModel.MProductsApi.LastOrDefault(); sinceID = Convert.ToInt16(last.Id); } while (multipleProductModel.MProductsApi.Count < nopProductsCount);


              public class ProductsCount
              {
                   public int Count { get; set; }
              }


    Why won't it allow me to use nopProductsCount on the while line?

    It gives this error:

    error CS0019: Operator '<' cannot be applied to operands of type 'int' and 'ProductListViewComponent.ProductsCount'

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.


    • Edited by Carneno Tuesday, July 24, 2018 9:43 PM
    Tuesday, July 24, 2018 9:42 PM

Answers

  • Because nopProductCount is not an integer. You can't check if an integer is less than something that is not an integer. My guess is that ProductListViewComponent.ProductsCount (which is what nopProductCount is) has a property that you should be using instead.

    while (multipleProductModel.MProductsApi.Count < nopProductsCount.SomeProperty); // Where "SomeProperty" is the number you want

    But that's a guess, because I'm not familiar with those objects.

    • Marked as answer by Carneno Tuesday, July 24, 2018 11:42 PM
    Tuesday, July 24, 2018 11:20 PM

All replies

  • Because nopProductCount is not an integer. You can't check if an integer is less than something that is not an integer. My guess is that ProductListViewComponent.ProductsCount (which is what nopProductCount is) has a property that you should be using instead.

    while (multipleProductModel.MProductsApi.Count < nopProductsCount.SomeProperty); // Where "SomeProperty" is the number you want

    But that's a guess, because I'm not familiar with those objects.

    • Marked as answer by Carneno Tuesday, July 24, 2018 11:42 PM
    Tuesday, July 24, 2018 11:20 PM
  • Ante,

    Yes! that was it.  I used nopProductsCount.Count.  I could have swore that I tried that before, but apparently not.

    Thanks for your help.

    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Tuesday, July 24, 2018 11:42 PM