Search code examples
c#asp.net

Variable not returning actual values


I want correctly return some variables (arrays)

kazkas.Ads[n]; (n = how many ads are)
kazkas.Ads[n].id;
kazkas.Ads[n].Days[m].Stats.Clicks;  // every day have his own clicks
kazkas.Ads[n].Days[m].Stats.Impresons; // every day have his own impresions

from this method and use these variables in other class.

 public static void GetAdsStats(string Ticket, DateTime start, DateTime end, int CamId)
    {
        var client = new CampaignStatsServiceClient();
        var id = new CampaignIdFilter();
        id.CampaignId = CamId;
        var statsdata = new GetAdStatsData();
        var kazkas = new Campaign();


        kazkas = client.GetAdStats(Ticket, new GetAdStatsData
        {
            IdFilter = id,
            StartDate = start,
            EndDate = end
        });
        long AllClicks = 0;
        long AllImpresions = 0;
        int reklamos = kazkas.Ads.Length;
        long[] statistikaClikai = new long[reklamos];
        long[] statistikaImpresions = new long[reklamos];
        for (int i = 0; i < reklamos; i++)
        {
            int dienos = kazkas.Ads[i].Days.Length;
            for (int lop = 0; lop < dienos; lop++)
            {

                AllClicks = AllClicks + kazkas.Ads[i].Days[lop].Stats.Clicks;
                AllImpresions = AllImpresions + kazkas.Ads[i].Days[lop].Stats.Impressions;

            }
            statistikaClikai[i] = AllClicks;
            statistikaImpresions[i] = AllImpresions;

        }

    }

I know that void type can't return anything, but this how I know that my method works ( from debugging). Like you see I was trying do that with for loop. Here i have 9 Ads and every ad have one day.

Like I says I want return every Ads id[in array], and every days.stats.impresions and days.stats.click

how can I do that ? Ore how return more variables/arrays from method to other class, I am using webservises, so i cant use database ore something like that.


Solution

  • As can be seen by the downvotes of the question, you need to design the return value and then code against it.

    Your query almost does it (now):

    kazkas.Ads[n]; (n = how many ads are)
    kazkas.Ads[n].id;
    kazkas.Ads[n].Days[m].Stats.Clicks;  // every day have his own clicks
    kazkas.Ads[n].Days[m].Stats.Impressions; // every day have his own impressions
    

    Your existing code show this should be expanded to include:

    kazkas.Ads[n].Total.Clicks;
    kazkas.Ads[n].Total.Impressions;
    

    So now you're ready to design. First you want a Stat Class that just contains CLicks and Impressions:

    public class Stat
    {
       public long Impressions { get; set; }
       public long Clicks { get; set; }
    }
    

    An optimisation here may be to use a struct, but I won't go into that.

    As you currently have defined it each Day has just a Stats property:

    public class DayStat
    {
       public Stat Stats { get; set; }
    }
    

    Now finally we can define the top level AdStat:

    public class AdStat
    {
       public int id { get; set; }
       public DayStat Day[];
       public Stat Total { get; set; }
    }
    

    Etc... There's further issues here, such as ensuring arrays are created and Stat instances are never null (which is why making some of these classes structs is an option). But I'm really a VB programmer so I'll stop here before I get caught typing crap into the SO IDE :-)