-1

I have a public class A which contains several methods.

I have two other classes within which I create the object of class A to call its method say MethodA().

Now it is behaving very strangely: when I call MethodA() from the other two classes it returns the same value to both the classes although the input is different from these classes.

Note my application is in multithreaded environment.

Some illustration of my code is below:

namespace Project1
{
   public Class A : SourceClass
    {
        public string MethodA(string input)
        {
        //it performs a logic and returns string variable
        string str1 = input;
        string str2 = "Hello";
        string str3 = "";
        if(str1.Compare(str2) == 0)
        {
            str3 = "Same";
        }
        else
        {
            str3 = "Different";
        }
        return str3;
    }

    public Class B : SourceClass
    {
        A objA = new A();
        string input1 = "Hello";
        string str1 = objA.MethodA(input1);
        MessageBox.Show(str1 + "from Class B");
    }

    public Class C : SourceClass
    {
        A objA = new A();  
        string input1 = "Hi";
        string str1 = objA.MethodA(input1);
        MessageBox.Show(str1 + "from Class C");
    }
}

Now when I run my application I get output as Same from class B and Same from class C, whereas I should get Different from class C.

Since its a multithreaded environment I think I am missing to lock an object. Please guide me as to where am I going wrong.

user2786794
  • 29
  • 1
  • 7
  • 2
    Please indent your code appropriately - at the moment it's *really* hard to read. I doubt that your code looks like that in the IDE. – Jon Skeet May 14 '14 at 09:26
  • 2
    Additionally, your code wouldn't even compile: `string.Compare` returns an `int`, not a `bool`. Please show a short but complete program demonstrating the problem. – Jon Skeet May 14 '14 at 09:27
  • It was just an example it is not the real code. Its a 1000 line code which is not possible to demonstrate here. My main point of interest is why does it return same value to both classes. – user2786794 May 14 '14 at 09:59
  • My point is that you should post neither pseudocode nor your full large code. You should instead produce a short but complete example which allows us to reproduce the problem. You should already have such an example as part of the work you've done to diagnose the problem as best you can before posting anyway. – Jon Skeet May 14 '14 at 10:46

1 Answers1

0

you should change

if(str1.Compare(str2))

as

if(str1.Compare(str2)==0)

Compare is returning negative, zero or positive depending the inputs.