Variable not returning actual values - c#

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.

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 :-)

Create a class or struct with members you need
public class Stat
{
public int Id { get; set; }
public long Clicks { get; set; }
...
}
Change the signature of your method from void GetAdsStats to IEnumberable<Stat> GetAdsStats and either return a collection of stats or use yield keyword to return the stat object.
Also if you do not want your method to return anything (return type void) do not use a name starting with Get.
Example:
public static IEnumerable<Stat> GetAdsStats(...)
{
...
var statList = new List<Stat>();
for (int i = 0; i < reklamos; i++)
{
var stat = new Stat();
statList.Add(stat);
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;
}
stat.Clicks = AllClicks;
stat.Impression = AllImpresions;
}
return statList;
}

Change your void to the type you want to return, say Campaign, and return the appropriate variable. The variables you define in your method, only live in your method and are not accessible from another method or class.

Related

C# - How to create a common method to update different properties in a object without creating multiple methods

This is my object
public class Totals {
public int Total1 { get; set; }
public int Total2 { get; set; }
public int Total3 { get; set; }
public int Total4 { get; set; }
}
Incrementing the values of Total1 and Total2 using calculateTotals method
private Totals calculateTotals(Totals t) {
if (//condition) {
t.Total1 += 1;
} else {
t.Total2 += 1;
}
return t;
}
**Incrementing value of Total3 and Total4 of the same object with same conditions at a different location using different method calculateOtherTotals, at this point I only need to update Total3 and Total4 **
private Totals calculateOtherTotals(Totals t) {
if (//condition) {
t.Total3 += 1;
} else {
t.Total4 += 1;
}
return t;
}
I am new to c# , I need to increment the values Total1,Total2 and Total3,Total4 separately and the code which I have is working fine
Is there a way to improve my code?, how can I avoid creating two different methods which pretty much does the same logic on different properties? is there a way to create only 1 method to achieve my functionality?
You could do it this way, but essentially the amount of code doesn't change.
This adds a judgment:
Totals calculateTotals(Totals t, bool Flag)
{
//function1:
if (Flag)
{
if (true)
{ //(condition) {
t.Total1++;
}
else
{
t.Total2++;
}
}
//function2:
else
{
if (true)
{ //(condition) {
t.Total3++;
}
else
{
t.Total4++;
}
}
return t;
}
Call it like this:
Totals totals = new Totals();
totals.Total1=0;
totals.Total2=0;
totals.Total3=0;
totals.Total4=0;
calculateTotals(totals,true);//function1:
calculateTotals(totals,false);//function2:
Reflection is one way, though its slow and not a Domain Specific Language:
Type totalsType = typeof(Totals);
var totalToIncrement = condition;
PropertyInfo prop = totalsType.GetProperty("Total" + totalToIncrement);
prop.SetValue(null, 76);
Or perhaps you want to abstract the properties you're incrementing:
private Totals calculateTotals(Totals t)
{
bool condition = true;
AbstractAdds(ref t.Total1, ref t.Total2, condition);
return t;
}
private void AbstractAdds(ref int a, ref int b, bool condition = false)
{
if (condition)
{
a++;
}
else
{
b++;
}
}
}
public class Totals
{
public int Total1;//{ get; set; }
public int Total2;//{ get; set; }
public int Total3;//{ get; set; }
public int Total4;//{ get; set; }
}
I'd personally have a List<int> or int[3] and make the condition calculate the index 0-3:
var index = calcCondition;
Totals[index]++;
This way its extensible for more totals and you get inbuilt functions like LINQ, eg Totals.Sum().
Is there a way to improve my code?, how can I avoid creating two different methods which pretty much does the same logic on different properties? is there a way to create only 1 method to achieve my functionality?
Then it depends on how you want your method (function) to be. (E.g., how you define what your function will do and how your class and properties are characteristic—which, currently, many who want to help you still wonder about.)
Let me give another clear example.
Assume that you answer your additional requirement are:
My object has only 4 properties of "Total"
I want these new function to increment value only 1 when call, no need to add more than 1
This function is called from another class to modify my object value
I want my cool function name calculateOtherTotals being private, because of some unexplained reason such as “I don't like others knowing it exists”.
Then
public OtherClass{
Public Totals ExposeThePrivateCalculateOtherTotals(Totals t, bool IncrementT1 , bool IncrementT2 , bool IncrementT3, bool IncrementT4)
{
calculateOtherTotals(t, IncrementT1 , IncrementT2 , IncrementT3, IncrementT4);
}
Private Totals calculateOtherTotals(Totals t, bool IncrementT1 , bool IncrementT2 , bool IncrementT3, bool IncrementT4) {
if( IncrementT1 ) t.Total1 += 1; //choose your style
if( IncrementT2==true ) ++t.Total2;//choose your style
if( IncrementT3!=false ) t.Total3++; //choose your style
t.Total4 += IncrementT4==true?1:0;//choose your style
return t;
}
}
//In main (how to use)
Totals t= new Totals();
OtherClass doMyFunc = new OtherClass();
t = doMyFunc.ExposeThePrivateCalculateOtherTotals(t, true, false,false,false); // result of operation => t.total1 += 1;
t = doMyFunc.ExposeThePrivateCalculateOtherTotals(t, false, true,false,false); // result of operation => t.total2 += 1;

Best way to write multiple constructor overloads in C#

I am learning C# and made a simple "Player" class. But I struggle having multiple overload.
Here's my best solution but I feel like it could be done simpler/better.
class Player : Entity
{
public Player() {
Name = "Player";
XP = 0;
LVL = 1;
XPToLvlUp = 10;
XpRank = 10;
}
public Player(string name) : this() {
Name = name;
}
public Player(string name, int _Hp, int _Mp) : this(name) {
HP = _Hp;
MP = _Mp;
}
public Player(string name, int _Hp, int _Mp, int _Xp, int _Lvl) : this(name, _Hp, _Mp) {
XP = _Xp;
LVL = _Lvl;
}
public Player(string name, int _Hp, int _Mp, int _Xp, int _Lvl, int XpByRank) : this(name, _Hp, _Mp, _Xp, _Lvl) {
XpRank = XpByRank;
}
//deleted code for better reading
private int XPToLvlUp;
private int XpRank;
public int XP;
public int LVL;
public string Name;
}
Is it good and if not please tell me why.
Thanks for your responses!
I think it's fine as is. One question to ask yourself: Are each of those methods actually likely to be called?
One option is to just let the programmer set those values after they've instantiated the class:
var myPlayer = new Player();
myPlayer.XP = 5;
However, there are situations where you really want all the info up front, so that may not be suitable.
Another option could be an options class that is passed to the ctor:
public class PlayerSettings
{
public Name = "Player";
public XP = 0;
public LVL = 1;
public XPToLvlUp = 10;
public XpRank = 10;
}
Then your ctors looks like this:
public Player() : this(new PlayerSettings())
{
}
public Player(PlayerSettings settings)
{
//Fill in appropriate variables here
}
That option would be called in this way:
var playerSettings = new PlayerSettings() { XP = 5 };
var myPlayer = new Player(playerSettings());
In the end, I'm not sure one is "better" than the other, it largely depends on your needs.
Your class is almost good and acceptable.
Short story: use Properties.
Long story:
First of all make or follow the naming rules, it will make your code more friendly to read. It's up to you, just a suggestion. For complex names consisting of multiple words you may use CamelCasedNames. And avoid shorten names for all types of data where it maybe useful. For example you may expand Lvl to Level but Xp to Experience will look as something odd. It's up to you too.
string name; // local Variable, first character lower cased
private string _name; // private Field, first character is lower cased with leading "_"
public string Name { get; set; } // public Property, first character is upper cased
I'll show you alternatives to overriden constructors and will follow the naming rules.
1) Default values for constructor (with a part of your class to keep it simple)
class Player
{
public Player(string name = "Player", int xp = 0, int level = 1)
{
Name = name;
Xp = xp;
Level = level;
}
// Properties instead of Fields
public int Xp { get; private set; } // restrict modification of the property outside of a class but reading is available
public int Level { get; private set; }
public string Name { get; set; }
}
2) Properties without constructor with default values
First Property purpose is restrict access to data to keep internal object data consistent. Even you make mistakes in the code. Good way to avoid some bugs.
Second property purpose is executing code while you're getting or setting one. For example, making properties dependent on each other to store less and only unique data.
class Player
{
public int Xp { get; private set; } = 0;
public int Level { get; private set; } = 1;
public string Name { get; set; } = "Player";
}
Usage
Player player = new Player() { Name = "KillerPWNZ", Level = 100, Xp = 999999 };
Bonus: Another Property feature
You can execute any code in get or set clause.
Let's assume that each next player's level require doubled amount of xp from previous but 2nd level requre 100 XP. And you decided to invoice to the 1st leveled player 1000 XP. Obviously you'll need to bump the Level few times. Assuming that Xp contains relative to Level value.
The invoice
player.Xp += 1000;
The Property with code
private int _xp = 0;
public int Level { get; private set; } = 1;
public int Xp
{
get => _xp; // same as: get { return _xp; }
set
{
_xp = value; // here value is keyword containing data you want to set
while (_xp >= GetXpPerLevel(Level))
{
_xp -= GetXpPerLevel(Level);
Level++;
}
while (_xp < 0 && Level > 1)
{
_xp += GetXpPerLevel(Level - 1);
Level--;
}
}
}
// helper method
private int GetXpPerLevel(int level)
{
if (level < 1) return 0;
// int result = 100;
// for (int i = 1; i < level; i++) result *= 2;
// return result;
// or the same with some binary shift magic :)
return 100 << (level - 1);
}

run a FOR loop to create 4 plumbers from the default constructor c#

As you see in the title above, i need to run a FOR loop to create 4 plumbers from the default constructor. After they are created (within the FOR loop), change their names, add them to the employee list and display in listbox. So basically, plumbers is actually a list declared in another class called EmployeeList. Wehn i tried changing their names to random ones, i get like an error msg saying 'Index is out of range'. Can someone help me with this?
Form Code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//create class-level employee list
EmployeeList plumbers = new EmployeeList();
private void Form1_Load(object sender, EventArgs e)
{
//run a for loop to create 4 plumbers from the default constructor.
for (int i = 0; i < plumbers.Count; i++)
plumbers[i] = new Employee();
//After they are created (within the FOR loop), change their names,
//plumbers[0].Name = "Nicole Fernandez";
//add them to the employee list
//display in listbox
foreach (Employee item in plumbers.Employees)
{
lstDisplay.Items.Add(item.DisplayData());
}
}
Employee List Class Code
class EmployeeList
{
//use private access modifier to create a list of employee
private List<Employee> employees = new List<Employee>();
public List<Employee> Employees
{
get { return employees; }
set { employees = value; }
}
//return the count of the employee list, use lambda operator
public int Count => employees.Count();
//create default constructor
public EmployeeList() { }
//create a method that adds employee sent to the list. No return value
public void AddEmp(Employee emp)
{
employees.Add(emp);//add employee to the list
}
//create employee from data sent. No return value
public void AddEmp(string inName, int inID, decimal inHourlyWage)
{
//declare a variable
Employee emp = new Employee(inName, inID, inHourlyWage);
//call the other AddEmp
AddEmp(emp);
}
//create a method that deletes employee from the list. No return value
public void DeleteEmp(Employee emp) => employees.Remove(emp);
//insert employee at the index
public void InsertEmp(Employee emp, int index) => employees.Insert(index, emp);
//create an indexer
public Employee this[int i]
{ //q12 - indexer property with exception
get
{
if (i < 0 || i >= Count)
throw new ArgumentOutOfRangeException(i.ToString());
return employees[i];
}
set { employees[i] = value; }
}
}
Employee Class Code
class Employee
{
//use auto-implemented property
public int ID { get; set; }
public string Name { get; set; }
public decimal HourlyWage { get; set; }
public decimal TotalPay { get; set; }
//create static integer that starts at 1
private static int NextID = 1;
//create a default constructor with default values
public Employee()
{
ID = NextID++;
Name = "John Doe";
HourlyWage = 15.25m;
TotalPay = 0.0m;
}
//create the custom constructor sending in 3 parameters
public Employee(string inName, int inID, decimal inHourlyWage)
{
Name = inName;//set name, no validation is required
//validate ID is between 1 and 250. if not, set to nextID available
if (inID <= 1 && inID >= 250)
NextID = inID;
//validate hourly wage is between 12.50 and 20. If not, set to 15.25
if (inHourlyWage <= 12.50m && inHourlyWage >= 20.0m)
inHourlyWage = 15.25m;
TotalPay = 0;//set total pay to 0
}
public string DisplayData() => ID + "\t" + Name + "\t" + HourlyWage.ToString("c") + "\t" + TotalPay.ToString("c");
}
First, if you want to create a list of 4 plumbers using a loop, then you need the loop to iterate 4 times. This is normally done by setting the initial counter value to 0, then looping while it is less than the number you want:
for (int i = 0; i < 4; i++)
Also, your plumbers object is of type EmployeeList, but you're trying to access it with an indexer as if it's a List or an Array. Instead, you should use the method you created to add new employees:
// Run a for loop to create 4 plumbers from the default constructor.
for (int i = 0; i < 4; i++)
{
plumbers.AddEmp(new Employee());
}
Since there doesn't appear to be any public method to update an existing plumber, we can just access the Employees list directly to rename the plumbers. There are a couple of ways to do it. One would be to use a loop and use part of the loop counter in the name:
// After they are created (within the FOR loop), change their names
for(int i = 0; i < plumbers.Employees.Count; i++)
{
plumbers.Employees[i].Name = string.Format("Plumber #{0}", i);
}
Another way is to do it by hand, giving each plumber a normal name:
plumbers.Employees[0].Name = "Nicole";
plumbers.Employees[1].Name = "Rufus";
plumbers.Employees[2].Name = "Mark";
plumbers.Employees[3].Name = "John";
Or, if you want to be really fancy, you can generate a list of names (I pulled these from the top baby names of 2016), then for each plumber grab a random name from the list and assign it (and remove it from the list so the plumbers all have unique names):
//After they are created (within the FOR loop), change their names
// Generate a list names
var nameCandidates = new List<string>
{
"Olivia", "Amelia", "Charlotte", "Ava", "Isla", "Arabella", "Aurora",
"Adeline", "Penelope", "Eleanor", "Ezra", "Asher", "Atticus",
"Declan", "Oliver", "Silas", "Milo", "Levi", "Henry", "Wyatt"
};
// Loop through each plumber and choose a random name
var rnd = new Random();
foreach(var plumber in plumbers.Employees)
{
// Choose a random name, assign it, and remove it from candidates
var nameIndex = rnd.Next(nameCandidates.Count);
plumber.Name = nameCandidates[nameIndex];
nameCandidates.RemoveAt(nameIndex);
}

Use a static or a non static class

I got a class that I made static, but is it a bad option? Should it be a non static class?
I want to set two values in my class.
Is there any chance when you give properties values that there will be some kind of conflict when setting them and when getting them? If another user have the same target.
I got a page that calling this class.
One user hits the page and this happens.
Set the properties for the calculation
Run the void to calculate two properties
"Maybe some other functions runs and take some time"
Get the value of the two properties
But what if another user hits the page and sets other values and make either the first user's value incorrect. I guess that's possible?
Some other options I thought of is to either
Send all properties into the void as arguments and return a new class with my two values I need. (Not store them as a static property that could be changed by another user before it got used).
Create a new class with the properties (perhaps called BreakTime). Send that into the void as one argument. Return it, calculated.
Or you tell me what the best option is! :)
Here how it looks:
public static class BreakTimeCalculator
{
public static int BreakFromSec { get; private set; }
public static int BreakUntilSec { get; private set; }
public static int CustomBreakSec { get; set; }
public static int FromSec { get; set; }
public static int UntilSec { get; set; }
public static int Equlizer { get; set; }
public static void CalculateBreakFromAndBreakeUntil()
{
var taskLength = UntilSec - FromSec;
var middleOfTask = FromSec + (taskLength / 2);
var secondsToMoveLeft = middleOfTask % 300;
var amountEqualizers = CustomBreakSec / Equlizer;
var fiftyFifty = amountEqualizers % 2 == 0;
var leftSideEqualizers = fiftyFifty ? amountEqualizers / 2 : (amountEqualizers / 2) + 1;
BreakFromSec = middleOfTask - secondsToMoveLeft - (leftSideEqualizers * Equlizer);
BreakUntilSec = BreakFromSec + CustomBreakSec;
}
}
Never create static state unless you really, really have to as you'll set yourself up for a fall if you do. You make testing harder and you make the likelihood of a thread conflict (as you describe) happening much higher.
If you must set state in a class and then invoke methods, rather than just passing the values as parameters to the method, make it a non-static class. Also, you should preferably pass the values in via the constructor, rather than using properties.
Having said that, my approach to your problem would be to create a POCO to hold the result data and have a static method to do the calculation. Using C# 6 syntax, this would look like:
public class BreakTimeResult
{
public BreakTimeResult(int breakFromSec, int breakUntilSec)
{
BreakFromSec = breakFromSec;
BreakUntilSec = breakUntilSec;
}
public int BreakFromSec { get; }
public int BreakUntilSec { get; }
}
public static class BreakTimeCalculator
{
public static BreakTimeResult CalculateBreakFromAndBreakeUntil(int customBreakSec,
int fromSec,
int untilSec,
int equlizer)
{
var taskLength = untilSec - fromSec;
var middleOfTask = fromSec + (taskLength / 2);
var secondsToMoveLeft = middleOfTask % 300;
var amountEqualizers = customBreakSec / equlizer;
var fiftyFifty = amountEqualizers % 2 == 0;
var leftSideEqualizers = fiftyFifty
? amountEqualizers / 2
: (amountEqualizers / 2) + 1;
var breakFromSec = middleOfTask - secondsToMoveLeft - (leftSideEqualizers * equlizer);
var breakUntilSec = breakFromSec + customBreakSec;
return new BreakTimeResult(breakFromSec, breakUntilSec);
}
}

Creating list/array of class instances?

I'm fairly new to C# and I have just learned about creating custom classes. The problem is, I can't figure out how to take the 40~65 instances of this class and put them in a list/array (whichever one I need) where I can locate and choose one based on an attribute defined in it.
Here's the class I have created right now:
public class Team
{
protected int teamNum;
protected double averageMatchPoints;
protected string location;
protected int matchesPlayed;
protected int matchesPending;
protected int blowouts;
//Team Number
public void SetNumber(int num)
{
teamNum = num;
}
public int GetNumber()
{
return teamNum;
}
//Average Points per match
public void AverageMatchPoints(double p)
{
averageMatchPoints = p;
}
public double GetAverageMatchPoints()
{
return averageMatchPoints;
}
//location information
public void SetLocation(string l)
{
location = l;
}
public string GetLocation()
{
return location;
}
//Number of Played Matches
public void PlayedMatches(int mat)
{
matchesPlayed = mat;
}
public int GetPlayedMatches()
{
return matchesPlayed;
}
//Number of matches pending (not played)
public void PendingMatches(int pen)
{
matchesPending = pen;
}
public int GetPendingMatches()
{
return matchesPending;
}
//Number of Blowouts (matches where the robot was disbaled for any number of reasons)
public void SetBlowouts(int b)
{
blowouts = b;
}
public int GetBlowouts()
{
return blowouts;
}
}
Now, if I had 40~65 of these teams competing at an event and I made an instance of this class for each one, how would I populate a combobox with each team number (teamNum) and then locate one specific team out of all the instances in the program by their team numbers?
I recommend a dictionary!
// Declared somewhere
private Dictionary<int, Team> _teamDictionary = new Dictionary<int, Team>();
.
.
.
//Initialization code - I assume you have gotten your teams from a database or somewhere?
foreach (var team in myTeamsList)
{
_teamDictionary.Add(team.teamNum, team);
}
.
.
.
// Later when you want to locate a team:
var team = _teamDictionary[selectedTeamNum];
Have you tried creating a List yet?
List<Team> Teams { get; set; }
You can then bind your combobox to the list/collection/IEnumerable of all the teams that you have. To initialize the teams up to 40/60 do the following?
for(int i = 0; i < 60; i++)
{
Team t = new Team();
t.Name = "Team 1";
t.TeamNumber = i + 1;
Teams.Add(t);
}
List<Team> allTheTeams = new List<Team>();
for(var i = 0; i < 65; i++){
allTheTeams.Add(new Team { teamNum = i });
}
And to get the team with number 34:
allTheTeams.FirstOrDefault(x => x.teamNum == 34);
Like this:
Add a constructor to your class that takes the teamnumber:
(this is the best solution if every team needs to have a number. So you can not forget to set the team number as you can not create an object of type team without setting the number in the constructor)
public class Team
{
protected int _teamNum;
public Team(int teamNum)
{
_teamNum = teamNum;
}
public int getTeamNum()
{
return _teamNum;
}
//more logic
}
Populate a dictionary, the comboBox and get a team for its number:
Dictionary<int, Team> dictionary = new Dictionary<int, Team>();
int teamNum = 1;
// Add your Teams to a dictionary (example)
dictionary.Add(teamNum ,new Team(teamNum++));
dictionary.Add(teamNum, new Team(teamNum++));
dictionary.Add(teamNum, new Team(teamNum++));
// Populate a comboBox
foreach(KeyValuePair<int,Team> kvp in dictionary)
{
comboBox1.Items.Add(kvp.Value.getTeamNum().ToString());
}
// get a team for a given teamNumer
int targetTeamNumber = 2;
if (dictionary.ContainsKey(targetTeamNumber))
{
Team team = dictionary[targetTeamNumber];
// do something with the team
}

Categories