Problem with variables while trying to make a c# library(.dll) - c#

I want to make a library that sums 2 numbers(a and b) and then stores the value into a result variable inside a library(.dll).
I tried this:
public static void Sum(int number1, int number2, int result)
{
result = number1 + number2;
}
but I can't make it so that in a program that uses this library, you can get the value of the result value that this function calculates and that's what I couldn't figure out for the past days. If you need any more info I will gladly provide it to you! Hope someone can help me!

In your case you do not return any result, consider using some of the following:
public static void Main()
{
var sumRes = SumResut(1, 2);
Console.WriteLine($"SumResult = {sumRes}");
OutSum(1, 2, out int outRes);
Console.WriteLine($"OutSum = {outRes}");
int refRes = 0;
RefSum(1, 2, ref refRes);
Console.WriteLine($"RefSum = {refRes}");
}
public static int SumResut(int number1, int number2)
{
return number1 + number2;
}
public static void OutSum(int number1, int number2, out int result)
{
result = number1 + number2;
}
public static void RefSum(int number1, int number2, ref int result)
{
result = number1 + number2;
}

Related

Unit test takes more time to do a subtraction

I was trying Unit Tests by writing simple examples.
Tested code:
public class Operations
{
public static double Subtraction(double number1, double number2)
{
return number1 - number2;
}
public static double Addition(double number1, double number2)
{
return number1 + number2;
}
public static double Division(double number1, double number2)
{
if (number2 != 0)
{
return number1 / number2;
}
else
{
return number1;
}
}
public static double Multiplication(double number1, double number2)
{
return number1 * number2;
}
}
Test Methods:
[TestClass()]
public class OperationsTests
{
[TestMethod()]
public void SubtractionTest1()
{
double number1 = 0;
double number2 = 10;
double result = Operations.Subtraction(number1, number2);
Assert.AreEqual(-10, result);
}
[TestMethod()]
public void SubtractionTest2()
{
double number1 = 10;
double number2 = 0;
double result = Operations.Subtraction(number1, number2);
Assert.AreEqual(10, result);
}
[TestMethod()]
public void SubtractionTest3()
{
double number1 = 10;
double number2 = 10;
double result = Operations.Subtraction(number1, number2);
Assert.AreEqual(0, result);
}
[TestMethod()]
public void AdditionTest1()
{
double number1 = 0;
double number2 = 10;
double result = Operations.Addition(number1, number2);
Assert.AreEqual(10, result);
}
[TestMethod()]
public void AdditionTest2()
{
double number1 = 10;
double number2 = 0;
double result = Operations.Addition(number1, number2);
Assert.AreEqual(10, result);
}
[TestMethod()]
public void AdditionTest3()
{
double number1 = 10;
double number2 = 10;
double result = Operations.Addition(number1, number2);
Assert.AreEqual(20, result);
}
[TestMethod()]
public void DivisionTest1()
{
double number1 = 0;
double number2 = 10;
double result = Operations.Division(number1, number2);
Assert.AreEqual(0, result);
}
[TestMethod()]
public void DivisionTest2()
{
double number1 = 10;
double number2 = 0;
double result = Operations.Division(number1, number2);
Assert.AreEqual(10, result);
}
[TestMethod()]
public void DivisionTest3()
{
double number1 = 10;
double number2 = 10;
double result = Operations.Division(number1, number2);
Assert.AreEqual(1, result);
}
[TestMethod()]
public void MultiplicationTest1()
{
double number1 = 0;
double number2 = 10;
double result = Operations.Multiplication(number1, number2);
Assert.AreEqual(0, result);
}
[TestMethod()]
public void MultiplicationTest2()
{
double number1 = 10;
double number2 = 0;
double result = Operations.Multiplication(number1, number2);
Assert.AreEqual(0, result);
}
[TestMethod()]
public void MultiplicationTest3()
{
double number1 = 10;
double number2 = 10;
double result = Operations.Multiplication(number1, number2);
Assert.AreEqual(100, result);
}
}
Every test is executing without any error but i noticed that SubtractionTest1 took always 7-8-9ms while the others take less than 1ms, can you explain me why?
Tests result:
MSTest will randomise the order that unit tests run in, as this can sometimes expose bugs in timing or shared state.
Which ever test you run first will take longer because it will be the one required to both JIT compile each of your methods and types and if any of those types have static initialisation to perform then this will also trigger.
I had a unit test suite where the first test would always take approximately 0.5s because I was initialising both the dependency injection and mocking frameworks. Every test post initialisation would take approximately 1ms to 2ms. So the behaviour you see here is totally normal.
For performance testing you must average your results and discard your first result to remove intialisation time from them. You should also test your initialisation speed if that is relevant to your application, but it's usually irrelevant for a server that boots once then stays online and ready.

My C# function will not work [duplicate]

This question already has answers here:
C# error: "An object reference is required for the non-static field, method, or property"
(3 answers)
Error: "an object reference is required for the non-static field, method or property..." [duplicate]
(6 answers)
An object reference is required for the non-static field, method, or property
(3 answers)
CS0120 error under vs2010 beta 2 - object reference is required
(2 answers)
Closed 5 years ago.
I have made a function called "AddNumbers" but when I call it, I get an error that says "An object reference is needed". I have placed this function outside of the Main() entry point, but I am sure it has nothing to do with placement because I tried moving it and it would still not work.
namespace FunctionPractice
{
class Program
{
public int AddNumbers(int number1, int number2)
{
int result = number1 + number2;
return result;
}
static void Main(string[] args)
{
Console.WriteLine("Enter 2 numbers: ");
int firstNumber = Convert.ToInt16(Console.ReadLine());
int secondNumber = Convert.ToInt16(Console.ReadLine());
int result = AddNumbers(firstNumber, secondNumber);
Console.WriteLine(result);
}
}
}
Change the method declaration to
public static int AddNumbers(int number1, int number2)
Or create an instance of Program class in main method and then access the add numbers method.
namespace FunctionPractice
{
class Program
{
public int AddNumbers(int number1, int number2)
{
int result = number1 + number2;
return result;
}
static void Main(string[] args)
{
Console.WriteLine("Enter 2 numbers: ");
int firstNumber = Convert.ToInt16(Console.ReadLine());
int secondNumber = Convert.ToInt16(Console.ReadLine());
Program program = new Program();
int result = program.AddNumbers(firstNumber, secondNumber);
Console.WriteLine(result);
}
}
}
Si main is static, AddNumbers must be static as well.
Hey your method works fine you just need to make your "AddNumbers" method static. You can't call a non-static method from a static one. Also you may want to add a read line to the end so the program doesn't just shut off once it writes to the screen. Here would be the fixed code.
class Program
{
public static int AddNumbers(int number1, int number2)
{
int result = number1 + number2;
return result;
}
static void Main(string[] args)
{
Console.WriteLine("Enter 2 numbers: ");
int firstNumber = Convert.ToInt16(Console.ReadLine());
int secondNumber = Convert.ToInt16(Console.ReadLine());
int result = AddNumbers(firstNumber, secondNumber);
Console.WriteLine(result);
Console.ReadLine();
}
}
Make your function as
public static int AddNumbers(int number1, int number2).
This is because, you are calling your function from a static function which belongs to the class itself. Your function belongs to the objects of the class. So when you call static function main(), it doesn't know of which instance's AddNumbers to call. Hence the error. Please read more about static keyword.

METHOD concept program in c# is not working

using System;
class Methodcalling
{
public int Values(int num1, int num2)
{
if (num1 > num2)
{
Console.WriteLine("num2 is large ");
}
else
{
Console.WriteLine("num1 is big");
}
}
static void Main(string[] args)
{
int a, b;
Methodcalling m = new Methodcalling();
Console.WriteLine("enter a no.:");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter a no.:");
b = Convert.ToInt32(Console.ReadLine());
int result = m.Values(a, b);
}
}
ERROR:'Methodcalling.Values(int, int)': not all code paths return a value
In this way its showing error in VISUAL STUDIO 2013.
This error is because you have declared the function to return an int, but your function does not return anything.
Have a look at this link here for a reference on the function declaration in c#.
For a relief from the error, try adding return 0; to the end of the function.
public int Values(int num1, int num2)
{
if (num1 > num2)
{
Console.WriteLine("num2 is large ");
}
else
{
Console.WriteLine("num1 is big");
}
return 0; // Return zero to the calling function.
}
Now you could call this function like this to capture the return value.
int TheReturnValue = Values(num1 , num2); //You will get 0 as value for TheReturnValue variable.
or change the int to void as in public void Values(int num1, int num2) if you don't want to return any values.
here is the usage:
public void Values(int num1, int num2)
{
if (num1 > num2)
{
Console.WriteLine("num2 is large ");
}
else
{
Console.WriteLine("num1 is big");
}
}
and you could call the function(void Return Type) like this:
m.Values(a, b); //without assigning it to a variable
Since you are only writing to console using this function, void datatype would be the better option.
I hope this helps.
You have to return an int value from the method as the method's signature defines a return type of int, and you are also calling
int result = m.Values(a, b);
which suggest that the method would return an integer but you have no return statement in your method.
You can modify the method like this if you don't want any integer to be returned.
using System;
class Methodcalling {
public void Values(int num1, int num2) {
if (num1 > num2)
{
Console.WriteLine("num2 is large ");
}
else
{
Console.WriteLine("num1 is big");
}
}
static void Main(string[] args) {
int a, b;
Methodcalling m = new Methodcalling();
Console.WriteLine("enter a no.:");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter a no.:");
b = Convert.ToInt32(Console.ReadLine());
m.Values(a, b);
}
}

How to limit scope of a constant to method/block

Is there any way to limit the scope of a compile-time constant to a method/block of code that avoids hardcoding the value?
Something like the following:
public int SomeMethod(int a) {
const int SomeCompileTimeConstant = 10; // obviously this doesn't exist
return a + SomeCompileTimeConstant;
}
opposed to hardcoding the value:
public int SomeMethod(int a) {
return a + 10;
}
or making it a class-level constant:
public class A {
private const int SomeCompileTimeConstant = 10;
public int SomeMethod(int a) {
return a + SomeCompileTimeConstant;
}
}
You can declare a constant inside a function.
When you wrote
public int SomeMethod(int a) {
const int SomeCompileTimeConstant = 10; // obviously this doesn't exist
return a + SomeCompileTimeConstant;
}
you were wrong.

Result is getting 0 when using Operator Overloading in C#

Hi I have the below program and i am new to C#,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UnaryOperatorOverLoad
{
public class UnaryOperator
{
private int Number1;
private int Number2;
private int Result;
public UnaryOperator() { }
public UnaryOperator(int number1, int number2)
{
Number1 = number1;
Number2 = number2;
}
public static UnaryOperator operator +(UnaryOperator opr)
{
UnaryOperator obj = new UnaryOperator();
obj.Result = obj.Number1 + obj.Number2;
return obj;
}
public void showdata()
{
Console.WriteLine("The Sum of Two Numbers is : " + Result);
}
}
public class Program
{
static void Main(string[] args)
{
UnaryOperator opr = new UnaryOperator(20, 30);
opr.showdata();
Console.ReadLine();
}
}
}
When i execute the code , i am getting the result is 0. I am not sure where i went wrong.Please help me to rectify the code.
Your mistakes:
You are creating new UnaryOperator obj = new UnaryOperator(); in operator overloading function. So obj.Number1 and obj.Number2 's value is 0, because you have just created obj instance.
You are never calling the + operator. Add opr = +opr; statement in to the Main method.
Change your code as:
public class UnaryOperator
{
private int Number1;
private int Number2;
private int Result;
public UnaryOperator() { }
public UnaryOperator(int number1, int number2)
{
Number1 = number1;
Number2 = number2;
}
public static UnaryOperator operator +(UnaryOperator opr)
{
opr.Result = opr.Number1 + opr.Number2; // Change this line
return opr;
}
public void showdata()
{
Console.WriteLine("The Sum of Two Numbers is : " + Result);
}
}
public class Program
{
static void Main(string[] args)
{
UnaryOperator opr = new UnaryOperator(20, 30);
opr = +opr; // Add this statement
opr.showdata();
Console.ReadLine();
}
}
This line of code
UnaryOperator opr = new UnaryOperator(20, 30);
Creates a new UnaryOPerator objcet and sets the Number1 and Number2 equal to 20 and 30 correspondingly.
Then you call opr.showdata();, which will print the value of the Result variable to the console. However, since you haven't performed any addition, the value of the variable would be the default, 0. That's the reason why you got 0.
Side note: I can't get why you are tyring to overload the addition the way you do it. It's reasonable to want to overload the addition of two custom objects, like UnaryOperator. However it's seems to me a bit strange that you are trying to overload the addition using the way you do it. Also, since the + operator has two operands, you have to define them like below (that's may not the overload you thought about, but this is the proper form in general terms):
public static UnaryOperator operator +(UnaryOperator unaryOperator1, UnaryOperator unaryOperator2)
{
return new UnaryOperator(unaryOperator1.Number1+unaryOperator2.Number1,
unaryOperator1.Number2+UnaryOperator2.Number2);
}
In order for this work, you would have to change the definition of your class to the following one:
public class UnaryOperator
{
public int Number1 { get; set; }
public int Number2 { get; set; }
public UnaryOperator(int number1, int number2)
{
Number1 = number1;
Number2 = number2;
}
public static UnaryOperator operator +(UnaryOperator unaryOperator1, UnaryOperator unaryOperator2)
{
return new UnaryOperator(unaryOperator1.Number1+unaryOperator2.Number1,
unaryOperator1.Number2+unaryOperator2.Number2);
}
public void ShowData()
{
Console.WriteLine("Number1: {0}, Number2: {1}", Number1, Number2);
}
}
The the Main method should change to the following one:
public class Program
{
static void Main(string[] args)
{
// Declare the first UnaryOperator object.
UnaryOperator opr1 = new UnaryOperator(20, 30);
// Declare the second UnaryOperator object.
UnaryOperator opr2 = new UnaryOperator(10, 40);
// Add them using the overloaded version of the + operator
UnaryOperator result = opr1+opr2;
// Show the results
result.ShowData();
Console.ReadLine();
}
}
As I stated above, this may not be exactly the version of your + operator overload, but I wrote an implementation of this overload to see it how it works. For further documentation, please look here.

Categories

Resources