I am printing out the sequence numbers and it jumps by 2 even though I am incrementing it by only 1.
I have a object TradeSequenceNo with a static variable.
I am assuming that a1 and b2 will have values 1,2 but I see that it has values 2,4. how can I make sure that I can get only increments by 1 for everytime I call TradeSequenceNo.NextSequanceNo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class TradeSequenceNo
{
private static int sequenceno;
public string id
{
get
{
return "NextTradeID";
}
}
public static int NextSequanceNo
{
get
{
sequenceno++;
return sequenceno;
}
}
}
public class Program
{
static void Main(string[] args)
{
int a1 = TradeSequenceNo.NextSequanceNo;
int b2 = TradeSequenceNo.NextSequanceNo;
}
}
}
The behaviour you posted is because of the debugger. Every time you check for property value in debugger it will be called resulting in increment of the value. Thus if you visit the same value multiple times in debugger you will get higher number depending upon how many times you checked the variable value. Just execute the following code (without debugging) and you will realize that code is correct and is behaving correctly:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class TradeSequenceNo
{
private static int sequenceno;
public string id
{
get
{
return "NextTradeID";
}
}
public static int NextSequanceNo
{
get
{
sequenceno++;
return sequenceno;
}
}
}
public class Program
{
static void Main(string[] args)
{
int a1 = TradeSequenceNo.NextSequanceNo;
int b2 = TradeSequenceNo.NextSequanceNo;
System.Console.WriteLine(a1);
System.Console.WriteLine(b2);
System.Console.ReadKey();
}
}
}
Related
I'm new to C#.
I'm creating a small program that input number by a user returns to the number and display it.
It works but a user is asked the same questions twice.
I just need one.
I'd appreciate if anyone help me with that.
Here is output of the program.
Here is my programming.
Class1
using System;
using System.Numerics;
using System.Reflection.Emit;
using static System.Console;
using System.IO;
namespace aaaa
{
public class Program
{
public static void Main(string[] args)
{
TryReturn tryreturn = new TryReturn();
tryreturn.template();
}
}
}
Class2
using System;
using System.Numerics;
using System.Reflection.Emit;
using static System.Console;
using System.IO;
namespace aaaa
{
public class TryReturn
{
public int entryNum;
public void template()
{
returnyNumber();
DisplayNumber();
}
public void DisplayNumber()
{
entryNum = returnyNumber();
WriteLine("number is " + entryNum);
Read();
}
public int returnyNumber()
{
int entryNum;
Write("Choose 1 or 2 >> ");
do
{
int.TryParse(ReadLine(), out entryNum);
if (entryNum == 1 || entryNum == 2)
{
break;
}
else
{
Write("Invalid entry! Enter either '1' or '2' >> ");
}
}
while (true);
return entryNum;
}
}
}
//remove the call of returnyNumber from template as you are calling it from DisplayNumber
public void template()
{
// returnyNumber();
DisplayNumber();
}
public void DisplayNumber()
{
entryNum = returnyNumber();
WriteLine("number is " + entryNum);
Read();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class Namespaces
{
public Namespaces()
{
}
}
namespace NameSpace1
{
public class MyClass
{
public static int add(int a, int b)
{
return a + b;
}
}
namespace NameSpace2
{
public class MyClass
{ public static int add (int a, int b, int c)
{ return a + b + c; }
}
}
}
}`
```
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
int sum1 = NameSpace1.MyClass.add(1, 2);
int sum2 = NameSpace2.MyClass.add(1, 2, 3); //Attempting to call Namespace 2 from here
}
}
Previously, I tried rebuilding, checking for spelling mistakes or differences between my namespaces(didn't find any), checking for formatting mistakes or differences between the two original Namespace methods(the only one I could find really didn't make any difference).
NameSpace2 is within NameSpace1. Change it to int sum2 = NameSpace1.NameSpace2.MyClass.add(1, 2, 3); and it should work:
namespace ConsoleApp1
{
public class Program
{
public static void Main()
{
int sum1 = NameSpace1.MyClass.add(1, 2);
int sum2 = NameSpace1.NameSpace2.MyClass.add(1, 2, 3);
}
}
}
(Fiddle).
A quick brief of the program that i have already done.
Golfer: A Golfer can be equipped with a Golf Club using the PickUp method; the golf club is
then stored in the Golfer’s club field. The Golfer’s holding property is true when its golf club
field is not null. When the Golfer is told to Swing, the following occurs:
If the Golfer is holding a golf club:
“Breathe and focus” is printed to the console. (b) The Golfer tells its club to swing.
Else…
“Where is my caddy?” is printed to the console.
GolfClub: Any golf club that can be swung; a GolfClub can also be held by a Golfer. GolfClub
is an abstract base class.
Putter: A Putter is a GolfClub that, when swung, prints “putt putt putt”.
SandWedge: A SandWedge is a GolfClub that can be swung 5 times before it is thrown in
frustration. (The constructor sets the swing counter to 5 and calling the Throw method resets
the play counter to 5.) When a SandWedge is swung, it does one of two things: if the remaining swing count is larger than zero, it prints “I am in my happy place” and the swing count is
decremented by one; otherwise it prints “hand me my hockey stick”.
My problem:
Code that uses a Golfer object may, therefore, ask for a specific golf club to be operated by referring
to that GolfClub by name. I'm not expected to get input from the user, just embedding it directly in Program.cs is totally fine. So basically a golfer wants to play, either select putter or sandwedge.
My code:
Program.cs
namespace Test
{
public class Program
{
private enum GolfKind
{
Putter, SandWedge
}
public static void Main(string[] args)
{
Golfer golfer = new Golfer();
Putter putt = new Putter();
SandWedge sandwedge = new SandWedge();
golfer.PickUp(putt);
golfer.PickUp(sandwedge);
if (golfer.Holding == true)
{
Console.WriteLine("Breate and Focus");
golfer.Swing(putt);
golfer.Swing(sandwedge);
}
else
{
Console.WriteLine("Where is my Caddy?");
}
Console.ReadLine();
}
}
}
GolfClub.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
public abstract class GolfClub
{
public GolfClub() { }
public abstract void Swing();
}
}
Golfer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
public class Golfer
{
private List<GolfClub> _clubs;
public Golfer()
{
_clubs = new List<GolfClub>();
}
public bool Holding
{
get
{
if(_clubs.Count() != 0)
{
return true;
}
else
{
return false;
}
}
}
public void PickUp(GolfClub club)
{
_clubs.Add(club);
}
public void Swing(GolfClub club)
{
foreach (GolfClub gc in _clubs)
{
if (gc == club)
{
gc.Swing();
}
}
}
}
}
SandWedge.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
public class SandWedge : GolfClub
{
private int _count;
public SandWedge()
{
_count = 5;
}
public override void Swing()
{
while(_count > 0)
{
Console.WriteLine("I am in my happy place");
_count--;
}
if (_count == 0)
{
Console.WriteLine("hand me my hockey stick");
}
}
public void Throw()
{
_count = 5;
}
}
}
Putter.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
public class Putter : GolfClub
{
public Putter () {}
public override void Swing()
{
Console.WriteLine("putt putt putt");
}
}
}
You can use a IDictionary<string, GolfClub> to associate a name with a golf club. Provide the name as a new argument for the PickUp() method. Then use a string argument in the Swing() method to find the right club.
public class Golfer
{
private readonly IDictionary<string, GolfClub> _clubs = new Dictionary<string, GolfClub>();
// [...]
public void PickUp(GolfClub club, string name) {
_clubs.Add(name, club);
}
public void Swing(string name) {
_clubs[name].Swing();
}
}
How to make the cycle, running up for 30 seconds? After 30 seconds, the cycle must be repeated with changing one variable(i++).
P.S. I've never used a timer and I need a detailed solution.
the microsoft example seems pretty good. https://msdn.microsoft.com/en-us/library/system.threading.timer%28v=vs.110%29.aspx
here is a cut down version, ive set it for 3 sec rather than 30 so you can verify the result
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
example e = new example();
e.timer();
Console.Read();
}
public class example
{
public volatile int i;
public void timer()
{
TimerCallback tcb = callback;
Timer t = new Timer(tcb, i, 3000, 3000);
}
public void callback(object state)
{
Console.WriteLine(this.i);
this.i++;
}
}
}
}
I'm making a simple application to simulate the bankteller problem.
What I'm trying to simulate is:
You have 4 counters in a store. 1 counter is open. Customers start coming in and enter the line for the first counter.
When the fourth customer enters the line for the first counter, another counter should open. The line should be equally divided between the 2 counters.When the customer at the second counter is helped and no new customers enter the line, the counter should close. Basically 4 is too many.
I can't seem to figure it out. I know I need to use a queue. But how? Could someone give me an example in console application? Preferable C#.
Thanks in advance.
Here is what I tried so far,
register class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RegisterCounter
{
class Register
{
private int customerCount;
public Queue<Customer> Line = new Queue<Customer>();
public Register()
{
customerCount = 2;
}
public Register(int customerCount)
{
this.customerCount = customerCount;
}
public int getCustomers()
{
return customerCount;
}
}
}
Customer class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RegisterCounter
{
class Customer
{
private int checkoutTime;
public Customer()
{
checkoutTime = 3;
}
public Customer(int checkoutTime)
{
this.checkoutTime = checkoutTime;
}
public int GetCheckoutTime()
{
return checkoutTime;
}
}
}
Register manager:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RegisterCounter
{
class RegisterManager
{
public List<Register> registers = new List<Register>();
Register r1 = new Register();
Customer c1 = new Customer();
public RegisterManager()
{
registers.Add(r1);
}
public void ManageCustomers()
{
for (int i = 0; i < registers.Count; i++)
{
registers.Insert(i, new Register());
if (i / 4 <= registers..Line.Count)
{
}
}
}
}
}