check connection

Sep 4, 2013 at 3:14 PM
hi today i found some issue with this adrotator... when i tried to play my game without internet connection it just getting error nullreference... i initialize the ads in splashscreen and hide it and only showing it in some scene of my game that fit with the ads... i'm using
        private void hideAds()//this is to hide my ads
        {
            AdRotatorXNAComponent foundComponent = null;
            foreach (var component in ScreenManager.Game.Components)
            {
                foundComponent = component as AdRotatorXNAComponent;
                if (foundComponent != null)
                {
                    foundComponent.IsEnabled = false;
                    foundComponent.Visible = false;
                    break;
                }
            }
        }

        private void showAds()//this is to showing ads
        {
            AdRotatorXNAComponent foundComponent = null;

            foreach (var component in ScreenManager.Game.Components)
            {
                foundComponent = component as AdRotatorXNAComponent;

                if (foundComponent != null)
                {
                    foundComponent.IsEnabled = true;
                    foundComponent.Visible = true;
                    foundComponent.Invalidate();
                    break;
                }
            }
        }
so anytime i get into scene/class that execute one of those code it just come with null reference code... any way to fix it? or it is okay to left it out? because most people will have their internet connection turn on in their smartphone?
Coordinator
Sep 4, 2013 at 4:45 PM
At which point are you getting a Null Reference exception?

We test AdRotator extensively in most connection experiences including "no network", so I'm curious where the exception is being raised.

Is it when you are casting as adrotator or when you call invalidate.

in any case, the searching through components seems a bit redundant. just keep a static reference to the AdRotator instance you have in your game class and control that, you can also just remove it from the Game.Components collection and add it when you need it rather than just enabling / disabling it.
Sep 4, 2013 at 11:34 PM
it's when i call one of those two procedure (showAds or hideAds)...
what do you mean by "keep a static reference to the adrotator instance?" i don't get it
so do you have a much better idea to adjust the ads just showing in some selected class only?
Coordinator
Sep 6, 2013 at 8:05 AM
If you put a break point inside the two functions, which line does the error occur on?

As for a static reference, just create a property in your game class and mark it as static, e.g.
public class game() : game
{
    public static AdRotatorXNAComponent myXNAComponent;

    public game()
    {
        myXNAComponent = new myXNAComponent();
        //Rest of my XNA component initialisation
    }
}

 private void hideAds()//this is to hide my ads
{
    if(ScreenManager.Game.Components.Contains(myXNAComponent)
    {
        ScreenManager.Game.Components.Remove(ScreenManager.Game.myXNAComponent);
     }
}

private void showAds()//this is to showing ads
{
    if(!ScreenManager.Game.Components.Contains(myXNAComponent)
    {
        ScreenManager.Game.Components.Add(ScreenManager.Game.myXNAComponent);
    }
}