Methods not working? [duplicate] - c#

This question already has an answer here:
C# - Why is this variable not being changed after being put through a method [duplicate]
(1 answer)
Closed 5 years ago.
Here is the block of code that I am having a bit of trouble with:
using System;
namespace TestProgram {
class Test {
static void Main() {
int number = 10;
MultiplyByTen(number);
Console.WriteLine(number);
Console.ReadKey(true);
}
static public void MultiplyByTen(int num) {
num *= 10;
}
}
}
When I run this block of code, I got 10 as the output instead of 100. My question is: Why does this happen and how to solve it?
Thanks for the help.

The problem is that when the variable number enters into the method MultiplyByTen the value is copied and the variable you are modifying inside it's in fact the copy, so, the original was not changed.
Try this instead:
public static void MultiplyByTen(ref int num)
{
num *= 10;
}
But remember you will have to call it with the ref keyword as well.
static void Main()
{
int number = 10;
MultiplyByTen(ref number);//Notice the ref keyword here
Console.WriteLine(number);
Console.ReadKey(true);
}
I recommend you to also check this out: Passing Objects By Reference or Value in C#

You need to return the value back to the function also assign the returned value to the number.
static void Main()
{
int number = 10;
number = MultiplyByTen(number);
Console.WriteLine(number);
Console.ReadKey(true);
}
static public int MultiplyByTen(int num)
{
return num *= 10;
}

Using your implementation:
using System;
namespace TestProgram {
class Test {
static void Main() {
int number = 10;
//MultiplyByTen(number);
//Console.WriteLine(number);
Console.WriteLine(MultiplyByTen(number));
Console.ReadKey(true);
}
static public int MultiplyByTen(int num) {
return num *= 10;
}
}
}

Related

An object reference is required for the non-static field, method, or property 'Program.x' [duplicate]

This question already has answers here:
Compiler Error CS0120 [duplicate]
(2 answers)
Closed 2 months ago.
I am new to C#, sorry if this is duplicate. I am simply trying to add two variables together but I get the error from the title
An object reference is required for the non-static field, method, or property 'Program.x'
Here is code
using System;
namespace HelloWorld
{
class Program
{
int x = 3;
int y = 10;
static void Main(string[] args)
{
int mathResults = x + y;
string results = mathResults.ToString();
Console.WriteLine("Hello World!");
}
}
}
Could someone please explain WHY I am getting this error? Thank you!
As your Main() method is static, you must use static variables in it.
So it should be like this :
class Program
{
static int x = 3;
static int y = 10;
static void Main(string[] args)
{
int mathResults = x + y;
string results = mathResults.ToString();
Console.WriteLine("Hello World!");
}
}

Basic C# Program Involving functions not working

I am a beginner in C# and was creating a program with functions but unfortunately am very confused how to return the value in the function to the main program.
I have researched for other similar questions but i cannot find the answer as it is very basic.
class Program
{
public int answer = 0;
public int column = 0;
public int UserInput;
static void Main(string[] args)
{
BinaryInput(int UserInput);
Console.WriteLine(UserInput);
}
static int BinaryInput (ref int UserInput)
{
UserInput = int.Parse(Console.ReadLine());
return UserInput;
}
}
}
Using a return value and a ref parameter at the same time for the same thing is completely unnecessary.
Just choose a method like this:
static int GetBinaryInput()
{
return int.Parse(Console.ReadLine());
}
static void Main(string[] args)
{
int UserInput = GetBinaryInput();
Console.WriteLine(UserInput);
}
Or like that, prefering out here:
static void GetBinaryInput(out int UserInput)
{
UserInput = int.Parse(Console.ReadLine());
}
static void Main(string[] args)
{
GetBinaryInput(out var UserInput);
Console.WriteLine(UserInput);
}
Using ref:
static void GetBinaryInput(ref int UserInput)
{
UserInput = int.Parse(Console.ReadLine());
}
static void Main(string[] args)
{
int UserInput = 0;
GetBinaryInput(ref UserInput);
Console.WriteLine(UserInput);
}
The first is more conventional and clean for the case in question.
Using an instance variable UserInput accessible to all methods is useless too, because this is a parameter between methods interactions, otherwise it is this one that is useless.
Also prefer TryParse to control errors, else use exception handling.
How TryParse method works?
What's the difference between the 'ref' and 'out' keywords?
When to use in vs ref vs out
Best practice: ref parameter or return value?
Which is better, return value or out parameter?
if you're returning a value, you will need the correct type of variable to return it too, e.g:
static void Main(string[] args)
{
int output = BinaryInput();
Console.WriteLine(output);
}
static int BinaryInput()
{
UserInput = int.Parse(Console.ReadLine());
return UserInput;
}

How C# Method change the value of the parameter [duplicate]

This question already has answers here:
c# pass by value
(3 answers)
Closed 2 years ago.
I am confused about how the code below works.
Why is the output not 10 but 5?
public class Program
{
public void MyFunc(int x){
x = 10;
}
public void Main()
{
int x = 5;
MyFunc(x);
Console.WriteLine(x);
}
}
Thanks a lot.
Read about the "ref keyword"
Now result is 10
public static void MyFunc(ref int x)
{
x = 10;
}
public static void Main()
{
int x = 5;
MyFunc(ref x);
Console.WriteLine(x);
}

Can someone explain me the logic behind this sample c# code? I am not getting the answer 10 for the years [duplicate]

This question already has answers here:
Passing Objects By Reference or Value in C#
(9 answers)
Closed 4 years ago.
private void assign(int num)
{
num = 10;
{
private void doSomething()
{
num = 0;
assign(num);
MessageBox.Show(num.ToString());
}
I get the answer 0 and not 10. can someone explain how this happens? my objective is to modify the variable.
As from comments you have 2 options, ref and out
1)
private void assign(ref int num)
{
num = 10;
}
private void doSomething()
{
int num = 0;
assign(ref num);
MessageBox.Show(num.ToString());
}
2)
private int assign(out int num)
{
num = 10;
}
private void doSomething()
{
var num = 0;
assign(out num);
MessageBox.Show(num.ToString());
}
Normally this is done by returning the value:
private int assign(int input)
{
//some complicated calculation on input.
return 10;
}
private void doSomething()
{
var num = 0;
num = assign();
MessageBox.Show(num.ToString());
}

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.

Categories

Resources