Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hon'ble Sirs and Ma'ams please show me the code of how you will transfer a value from one function to another function of different class, without declaring 'static'...?
And is there any problem if I declare a variable static and use it for the same purpose ?

What I have tried:

Nothing... just need your help
Posted
Updated 22-Apr-22 21:56pm

You just need a reference to an object of the other class, and a method to transfer the value.
C#
class Foo
{
    public int Value { get; set; }
}
class Bar
{
    private Foo foo;
    public Bar(Foo foo)
    {
        this.foo = foo;
    }
    public SomeMethod(int someNumber)
    {
        foo.Value = someNumber);
    }
}
 
Share this answer
 
Comments
Member 12712527 23-Apr-22 4:59am    
is it parameterised vaiable in use....
Richard MacCutchan 23-Apr-22 5:05am    
What do you mean by that?
This is a very vague question, and it probably shows a lack of understanding of how C# works.

There are four types of variables in C#: local, class, parameter, and static and how you would "share" them differs.

Local variables are declared within a method, and exist only for the duration of that method: when the method is called the variables are created, when it returns they are destroyed. They have no existence at all outside the body of the method, and can only be "shared" by passing them as a parameter to another method by calling it within the body:
C#
void Foo()
   {
   string inp = Console.ReadLine();
   Bar(inp);
   }


Parameter variables are similar to local variables in that they only exist while the method is running, but there value is "shared" by being passed into the method when it is called:
C#
void Bar(string str)
   {
   Console.WriteLine($"I was passed \"{str}\" when I was called");
   }
...
   string inp = Console.ReadLine();
   Bar(inp)


Class variables are declared outside a method, and are available to all non-static methods in the class (and sometimes other classes depending on the class access modifier). The difference is that they are specific to a particular instance of the class, in the same way that a glove box is specific to a particular car: if you put your mobile in the glove box of your car than come for a drive in my car you don't expect to find your mobile in the glove box!
A class variable is effectively shared by all instance methods automatically:
C#
private string inp = "Not assigned yet";
void Foo()
   {
   inp = Console.ReadLine();
   Bar();
   }
void Bar()
   {
   Console.WriteLine($"I can access \"{str}\" here as well.");
   }


Static variables are different: there is only one variable which is "shared" by all code which accesses the class - they are not related to a specific instance and any change to the variable can be seen in all methods regardless of which instance it might be.
That doesn't mean you should use them: I very rarely need a static variable! If the glovebox was static then your mobile would be accessible via any car's glovebox: so the chances are that it would have been stolen, or used, or broken within seconds of you putting it in the box because any driver would find it in his glovebox. They aren't a good way to "share" information within a program unless that reflects the "real-world" situation as the value can be changed from any or all instances and that can cause some real headaches!
 
Share this answer
 
Comments
Member 12712527 23-Apr-22 4:57am    
That means I can use static variables if I use it carefully. Otherwise use a parameterised variable for the given purpose
OriginalGriff 23-Apr-22 5:30am    
To be honest, if you use a static variable, it's probably a mistake in almost every circumstance ...
Richard MacCutchan 23-Apr-22 5:05am    
Take a glance at OP's previous ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900