Double coding the same function

Hi ,

I try to correct an application in C#..there are 2 Classes in which the "private string GetValueFromString(string data, int start, int end)" function is included in both classes completely like copy paste..This function want us to use-to call in both classes....I tried to disable the one and make the other public static( the field which it returns is only a character--return tempBuilder.ToString();).... and to use like Class1.Function and it works...What have I succed with that..My biggest problem in the application is that I want to achieve lowest processing time cause the application is a multitasking....Or do you think that is the same and my change doesn't influence that

And something last...I do that and with another one function and the message error I have is "An object reference is required for the nonstatic field,method or property:"...What i have to do

Please help me...thanks a lot...




Answer this question

Double coding the same function

  • newbieneedshelp

    Using a static method is normally quicker than using an instance method because: (a) you don't have to create an instance of the class, and (b) one less argument to push onto the stack. As for thread-safety a static method is only thread-safe if it accesses no static fields or properties. Assuming that it takes some arguments as input, works on the arguments and returns some results then it is by definition thread-safe as parameters and locals are on the stack and the stack is per-thread.

    For your error you are attempting to reference an instance field, property or method as Type.Member. For instance members you must create an instance and then call a method on it. The error message will point you to the exact line you are having the problem on so it should be easy to fix.

    Michael Taylor - 6/26/06


  • Double coding the same function