C# class libraries and console application - c#

I have this class library I created. And I am running into the issue of getting the library to work with a console application. This is just a basic assignment I'm working on but I do not know where I am going wrong. Here are my files:
Class Library:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test2
{
public class Test
{
public static string GetData(String prompt)
{
Console.WriteLine(prompt);
return Console.ReadLine();
}
public static int GetInt()
{
return Convert.ToInt32(GetData("Enter an interger"));
}
public static void put(String output)
{
Console.WriteLine(output);
}
public static void put(String output, String heading)
{
put(heading + '\n' + output);
}
public static void Main(String[] args)
{
int x = GetInt();
int y = GetInt();
if (x > 0 && y > 0) put(String.Format("Sum:{0}", x + y));
}
}
}
Console Application:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Test2;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Test myTest = new Test();
Test.GetData(prompt);
}
}
}
My main issue I guess I have is calling the methods to run in the console application. No matter how much reading I do I can't seem to know what I am doing wrong.

You don't need a Main method in your class library. The Main method in your console application is the entry point to your application.
If the functionality in the Main method you've defined in your class library is what you're trying to achieve for the overall application then consider moving that functionality into the Main method in your console application.
You've made the methods in your class library static so you don't need to instantiate an object of type Test
So my solution for you would be to have this in your Main method of your console application:
int x = Test.GetInt();
int y = Test.GetInt();
if (x > 0 && y > 0) Test.put(String.Format("Sum:{0}", x + y));
Console.Read();
I would suggest reading up on Static Classes and Static Class members here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members
When deciding to make a Static class I always come back to this quote from the above link:
A static class can be used as a convenient container for sets of
methods that just operate on input parameters and do not have to get
or set any internal instance fields.
I would also consider changing the name of your Test class. Even in small test applications like this I find it is useful to use well defined names with meaning. It's good practice so that you will do it automatically in larger applications.
Finally, good luck with all your learning!

Related

how to add multiple names spaces and Classes within those for one solution

As a first simple exercise, I ask you to create a new C# Console App in Visual Studio.
This app must have 2 different namespaces, and each namespace contain at least one class with a method to display the word "Hello, this is XXX class" in console, where XXX is the parent class. You can code it on the same file or create different files for different namespaces.
In the Main method of the default class called Program, you must be able to show all the different "Hello..." messages in console, being able to call different classes and methods you just have created.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace First
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello this is The FIRST Class.");
Console.ReadLine();
}
}
}
namespace Second
{
internal class Program
{
static void Main1(string[] args)
{
Console.WriteLine("Hello, this is the SECOND Class.");
Console.ReadLine();
}
}
}
I was hoping both Name spaces would show the individual classes. Not sure what I'm doing wrong here
You should be creating two classes and should call respective methods from Main method.
using First;
using Second;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication16
{
class Program
{
static void Main(string[] args)
{
FirstClass fc = new FirstClass();
fc.DisplayHello();
SecondClass sc = new SecondClass();
sc.DisplayHello();
}
}
}
namespace First
{
internal class FirstClass
{
public void DisplayHello()
{
Console.WriteLine("Hello this is The FIRST Class.");
}
}
}
namespace Second
{
internal class SecondClass
{
public void DisplayHello()
{
Console.WriteLine("Hello this is The SECOND Class.");
}
}
}

Can I use C# OrderBy of linq in CodeHunt

Problem Description
using System;
using System.Linq;
public static class Program {
public static int[] Puzzle(int[] a) {
return a.OrderBy(s => s).ToArray();
}
}
My code and wrong information
Bad Dependency
[System.Core]System.Linq.IOrderedEnumerable`1
Why I am wrong?Whether or not the platform can support OrderBy?Do I need a custom sort function?The code can run successful in VS2015, I have tried.
www.codehunt.com
Try follow.
using System;
using System.Linq;
public static class Program {
public static int[] Puzzle(int[] a)
{
return Array.Sort(a);
}
}

Create and Using DLL in same Project in c#

I am having a DLL file. With the use of DLL, I have to call the methods and add some more methods in my project. Now, I need to migrate the older DLL to Make that project as a new DLL. I done this But the problem is The C# code is converted to net module it shows two errors. I am not clear about that. kindly help me over it.
DLL Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace mcMath
{
public class mcMathComp
{
private bool bTest = false;
public mcMathComp()
{
// TODO: Add constructor logic here
}
/// <summary>
/// //This is a test method
/// </summary>
public void mcTestMethod()
{ }
public long Add(long val1, long val2)
{
return val1 - val2;
}
/// <summary>
/// //This is a test property
/// </summary>
public bool Extra
{
get
{
return bTest;
}
set
{
bTest = Extra;
}
}
}
}
CS Project:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using mcMath;
namespace mcClient
{
class Program
{
static void Main(string[] args)
{
mcMathComp cls = new mcMathComp();
long lRes = cls.Add(23, 40);
cls.Extra = false;
Console.WriteLine(lRes.ToString());
Console.ReadKey();
}
}
}
Errors:
Program.cs(5,7): error CS0246: The type or namespace name 'mcMath' could >not be found (are you missing a using directive or an assembly reference?)
Tried Methods:
I will add the reference via Project-> Add Reference.
The using Reference also used.
Put the DLL into the current project debug/release folder
I'm guessing you used to have the code side by side, i.e.
public int Add(int a, int b)
{
return a + b;
}
public void SomeMethod()
{
var result = Add(2,3);
}
This works because the scope (this.) is applied implicitly, and takes you to the Add method on the current instance. However, if you move the method out, the scope is no longer implicit.
You will need one of:
the type name if it is a static method
or a static using if using C# 6
a reference to the instance if it is an instance method
Then you would use one of (respectively):
var result = YourType.Add(2,3); (plus using YourNamespace; at the top)
using static YourNamespace.YourType; at the top
var result = someObj.Add(2,3);
Checking the compiler message, it sounds like you've done something like (line 7):
using YourNamespace.YourType.Add;
which is simply wrong; you don't use using to bring methods into scope - only namespaces and (in C# 6) types.
Likewise, I suspect you have (line 22):
var result = YourNamespace.YourType.Add(x,y);
which is not valid as this is not a static method.
Create and Using DLL in same Project in c#
DLL or Class Library is a separate project that can be part of same solution.
As you already know, adding a reference to that dll/project will make it available in your app project.
However if function Add in dll is in different namespace (which would be normal) u would need to add using clause at the beginning of your class

Extension Methods with Custom Classes

I'm attempting to extend my custom classes and running into a problem where it cannot find the extension method.. I have and can extend any built in classes or even ones contained within DLL's. I don't know if this is a compilation error or if I'm doing something wrong. Threw together a small program for an example, won't compile..
Here's the extension:
namespace ExtensionMethodTesting.Extension
{
public static class Extension
{
public static void DoSomething(this ExtensionMethodTesting.Blah.CustomClass r)
{
}
}
}
Here's the Custom Class:
namespace ExtensionMethodTesting.Blah
{
public class CustomClass
{
public static void DoNothing()
{
}
}
}
Here's the code calling it:
using ExtensionMethodTesting.Blah;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExtensionMethodTesting.Extension;
namespace ExtensionMethodTesting
{
class Program
{
static void Main(string[] args)
{
CustomClass.DoNothing();
CustomClass.DoSomething();
}
}
}
I must be missing something... Anyways the exact error just for clarification is:
Error 1 'ExtensionMethodTesting.Blah.CustomClass' does not contain a definition for 'DoSomething' c:\users\damon\documents\visual studio 2013\Projects\ExtensionMethodTesting\ExtensionMethodTesting\Program.cs 16 25 ExtensionMethodTesting
Extension methods require an instance of an object. You'll have to new up a CustomClass to use it.
var custom = new CustomClass();
custom.DoSomething();
See this answer as to why that is.
You need to instantiate an object of the CustomClass to use its extension method.
CustomClass obj = new CustomClass();
obj.DoSomething();

Why do extension methods not work with namespace aliasing?

This may be an ignorant question, but I'm unsure why I can not use namespace aliasing and extension methods together.
The following example works just fine:
Program.cs
using System;
using ExtensionMethodTest.Domain;
namespace ExtensionMethodTest
{
class Program
{
static void Main(string[] args)
{
var m = new Domain.MyClass();
var result = m.UpperCaseName();
}
}
}
MyClass.cs
using System;
namespace ExtensionMethodTest.Domain
{
public class MyClass
{
public string Name { get; set; }
}
}
MyClassExtensions.cs
using System;
namespace ExtensionMethodTest.Domain
{
public static class MyClassExtensions
{
public static string UpperCaseName (this MyClass myClass)
{
return myClass.Name.ToUpper();
}
}
}
However, when I alias domain as follows in Program.cs:
using Domain = ExtensionMethodTest.Domain;
The extension method no longer works..
This can be rather frustrating when I'm dealing with converting various domain objects to contract objects (let's say I have 4 domain assemblies and 4 contract assemblies) for use in a web service. Using aliasing would be very handy as I could alias as follows and continue to use the various extension methods (such as ToContract, etc.):
using BillingContracts = Namespace.Billing.Contracts;
using IssuingContracts = Namespace.Issuing.Contracts;
etc...
I look forward to the answer.. I'm sure it's straight forward, but I, for the life of me, can't figure out why it doesn't work.
Thanks!
Make sure to still add a non-aliased using statement:
Program.cs
using System;
using ExtensionMethodTest.Domain; //DON'T FORGET A NON-ALIASED USING
using MyDomain = ExtensionMethodTest.Domain;
namespace ExtensionMethodTest
{
class Program
{
static void Main(string[] args)
{
var m = new MyDomain.MyClass();
var result = m.UpperCaseName();
}
}
}
MyClass.cs
using System;
namespace ExtensionMethodTest.Domain
{
public class MyClass
{
public string Name { get; set; }
}
}
MyClassExtensions.cs
using System;
namespace ExtensionMethodTest.Domain
{
public static class MyClassExtensions
{
public static string UpperCaseName (this MyClass myClass)
{
return myClass.Name.ToUpper();
}
}
}
I also love to use namespace aliasing but its not working in case of Extension methods. So one thing that i did is, I changed the namespace of extension class to same namespace that my main project has (although my extension class resides in sub folder of main project).
Suppose I have a project myFirstProj which surely has namespace myFirstProj for root classes. My extension class is present in myFirstProj/Common/myExtensionClass with contains namespace myFirstProj.Common { //myExtensionClass }.
So now what I did is, I changed the namespace of myExtensionClass from namespace myFirstProj.Common{ //myExtensionClass } to namespace myFirstProj{ //myExtensionClass } .
Now i can use my extension methods in my whole project myFirstProj event without specifying using statement for my extension class.
I know this isn't a standard way to that but I haven't found any other workaround for it expect this one because for my Project there is a requirement to go with namespace aliasing for project namespaces.

Categories