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
Related
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).
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 have 2 projects (libraries)
For Ass1.cs i have:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ass1
{
public class Class1
{
public void print()
{
Console.WriteLine("Hello");
}
}
}
Ass2 is another library which contains a reference of Ass1. Here i am getting
the error message for cd is a field but is used like a type ?
Ass2.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/* Class1 is in project Ass1 , import project Ass1 */
using Ass1;
namespace Ass2
{
public class Class2
{
Class1 cd = new Class1(); /* gives error: is afield but is used like a type*/
cd.print();
}
}
Try to put your code inside a function or method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/* Class1 is in project Ass1 , import project Ass1 */
using Ass1;
namespace Ass2
{
public class Class2
{
public void foo()
{
Class1 cd = new Class1(); /* gives error: is afield but is used like a type*/
cd.print();
}
}
}
put your code in a method or constructor
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ass1;
namespace Ass2
{
public class Class2
{
public Class2
{
Class1 cd = new Class1();
cd.print();
}
}
}
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)
{
}
}
}
}
}
--ConsoleApplication 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
public class MsgService
{
private static CreateConnectionToA _instanceA;
private static CreateConnectionToB _instanceB;
protected MsgService()
{
}
public static MsgService GetInstanceA(string paramA, string paramB)
{
if (_instanceA != null)
{
return _instanceA;
}
return _instanceA = new CreateConnectionToA("p1","p2");
}
public static MsgService GetInstanceB(string paramA, string paramB)
{
if (_instanceB != null)
{
return _instanceB;
}
return _instanceB = new CreateConnectionToB("p1", "p2");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class CreateConnectionToB : MsgService
{
public CreateConnectionToB(string param1, string Param2)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class CreateConnectionToA : MsgService
{
public CreateConnectionToA(string param1, string Param2)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
MsgService.GetInstanceA("p1", "p2");
Console.Read();
}
}
}
--ConsoleApplication 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press");
Console.Read();
ConsoleApplication2.MsgService.GetInstanceA("p1", "p2");
Console.Read();
}
}
}
I am trying to Make simgleton implementation but something is wrong with my approach. It always creates new instance of _instanceA and _instanceB from each console application.
Can someone please point me out what needs to be done here?
You would need named Mutexes for inter-process synchronization.
Sharing an object instance between two applications is kinda hard, since they run in separate appdomains, by default. To accomplish what I think you're trying to do, you'll need to either
marshal across appdomain boundaries with, or
run the two processes in a shared appdomain. Write a 3rd process — a shell — that's responsible for spawning/hosting the other two processes in a shared appdomain.
http://www.codeproject.com/KB/dotnet/AppDomainMemImprovement.aspx
Sharing data between AppDomains