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).
Related
I'm getting an error on Console.WriteLine();
HelperLibrary Project I already reference it to my main project
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MathClasses
{
public class MyMaths
{
public void Divisibility(int number, int divby)
{
int divisibility = number % divby;
if (divisibility == 0)
{
Console.WriteLine($"{number} is divisible by {divby}");
}
else
{
Console.WriteLine($"{number} is not divisible by {divby}");
}
}
}
}
and here is my main project
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MathClasses;
using MiscClasses;
using PrinterClasses;
namespace ChallengeOne
{
class Program
{
static void Main(string[] args)
{
MyMaths myMaths = new MyMaths();
int number = Convert.ToInt32(Console.ReadLine());
int divby = Convert.ToInt32(Console.ReadLine());
myMaths.Divisibility(number, divby);
}
}
}
Note:
my instructor says that my 2nd project doesn't recognize my using System;
the error says
The name 'Console' does not exist in the current context
I have following question, right now like 2 minutes ago a guy posted his code.
He couldn't use his instance obj , and I don't know why he deleted his question.
He had also some usings, I think he had using static N.Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
int Y(int a)
{
a = 10;
Console.WriteLine(a);
return a;
}
}
}
}
namespace WindowsFormsApp1
{
class Class1
{
N.Form1 obj = new N.Form1();
public void X(int a)
{
var v = obj.Y(a);
Console.WriteLine(v);
}
}
}
So first I've seperate the method, and made it public so I can create instance and use it in another class.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int Y(int a)
{
a = 10;
Console.WriteLine(a);
return a;
}
}
And then instead of using N.From1, I used the namespace for declaring instance WindowsFormsApp1.Form1 obj = new WindowsFormsApp1.Form1();
My Class1 looks like this now:
class Class1
{
WindowsFormsApp1.Form1 obj = new WindowsFormsApp1.Form1();
public void X(int a)
{
var v = obj.Y(a);
Console.WriteLine(v);
}
}
So there isn't any error, and my question is, is this the right way to declare and instance and use it later? Also are you allowed to use the namespace ?
Thanks,
This question already has answers here:
Why is the console window closing immediately once displayed my output?
(15 answers)
Closed 5 years ago.
When I run this simple application it closes itself without reason. How to prevent that?
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 20;
int c = a * b;
Console.WriteLine(c);
}
}
}
Just add Console.ReadKey(); after printing the value.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 20;
int c = a * b;
Console.WriteLine(c);
Console.ReadKey();
}
}
}
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();
}
}
}
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)
{
}
}
}
}
}