Forum General

Two Functions behaves differently

VuyiswaMasekoVuyiswaMaseko USMember ✭✭✭
edited January 3 in General

Good Day

i have two functions which are the same but behave differently

    ```

public async static Task Retrieve_Token()
{
TOKEN_MODEL token = null ;

        try
        {
            try
            {
                token =  await BlobCache.LocalMachine.GetObject<TOKEN_MODEL>("TOKEN1"); 
            }
            catch (KeyNotFoundException)
            {
            } 
        }
        catch (Exception ex)
        {
            if (ex.Message == "Sequence contains no elements.")
            {
                return null;
            }
            else
            {
                return null;
            }
        }
        return token;
    }

public async static Task RetrieveUserInfo()
{
USERS_Model Userinfo = null;
try
{
try
{
Userinfo =await BlobCache.LocalMachine.GetObject("USERDAT");
}
catch (KeyNotFoundException)
{
}
}
catch (KeyNotFoundException ex)
{
if (ex.Message == "Sequence contains no elements.")
{
return null;
}
else
{
return null;
}
}
return Userinfo;
}
```
and i am calling these two function in App.cs like this

TOKEN_MODEL token_model = await GenericMethods.Retrieve_Token();

and if there is no value , it will return Null and assign the object "token_model " with null which is ok

and the second one which is a problem

USERS_Model userinfo= await GenericMethods.RetrieveUserInfo();

The above line directly jumps to my exception handling code section instead of also assigning the object "userinfo" with null with an exception

System.InvalidOperationException: Sequence contains no elements.

Thanks

Best Answer

  • VuyiswaMasekoVuyiswaMaseko USMember ✭✭✭
    Accepted Answer

    Good Day

    Thanks for the reply . i changed my code to this

      public async static Task<TOKEN_MODEL> Retrieve_Token()
            { 
                TOKEN_MODEL Userinfo = new TOKEN_MODEL();
                try
                {
                    try
                    {
                       Userinfo = await BlobCache.LocalMachine.GetObject<TOKEN_MODEL>("USERDAT"); 
                    }
                    catch (KeyNotFoundException)
                    { 
                    }
                }
                catch (Exception )
                {
                }
                return Userinfo;
            }
    

    and

      public   static void Store_Token(TOKEN_MODEL Token)
            {
                try
                { 
                    BlobCache.LocalMachine.InsertObject("USERDAT", Token);
                }
                catch (KeyNotFoundException)
                { 
                } 
            }
    

    My other issue was logical errors , So i had to do database logging to find the problem code. and i finally found it.

Answers

  • JohnHJohnH GBMember ✭✭✭✭✭

    i have two functions which are the same but behave differently

    The methods are not the same.

    The first uses the generic GetObject<> the other doesn't, and they are fetching different types of data. Maybe the data for the second method doesn't exist?

  • VuyiswaMasekoVuyiswaMaseko USMember ✭✭✭
    Accepted Answer

    Good Day

    Thanks for the reply . i changed my code to this

      public async static Task<TOKEN_MODEL> Retrieve_Token()
            { 
                TOKEN_MODEL Userinfo = new TOKEN_MODEL();
                try
                {
                    try
                    {
                       Userinfo = await BlobCache.LocalMachine.GetObject<TOKEN_MODEL>("USERDAT"); 
                    }
                    catch (KeyNotFoundException)
                    { 
                    }
                }
                catch (Exception )
                {
                }
                return Userinfo;
            }
    

    and

      public   static void Store_Token(TOKEN_MODEL Token)
            {
                try
                { 
                    BlobCache.LocalMachine.InsertObject("USERDAT", Token);
                }
                catch (KeyNotFoundException)
                { 
                } 
            }
    

    My other issue was logical errors , So i had to do database logging to find the problem code. and i finally found it.

Sign In or Register to comment.