Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

lock and overloads

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
am_public@email.it - 04 Aug 2005 09:18 GMT
Hi,
I have to do a sealed class with some functions that ruturns me a
string with a required length.

I.e.

public class MyClass
{
 static Object lockStringFixedLength = new Object();

 public static string GetStringFixedLength(
    string stringToChange,
    int length,
    char charToUse,
    bool insertAtLeft,
    bool cutIfLonger
    )
 {
    lock( lockStringFixedLengthString )
    {
    string fixedString = "";

    if ( stringToChange.Length == length )
      return stringToChange;

    if ( stringToChange.Length < length )
    {
     string stringToAdd =
             new string( charToUse, length - stringToChange.Length );

         if ( insertAtLeft )
         {
            fixedString = stringToAdd + stringToChange;
     }
         else
         {
             fixedString = stringToChange + stringToAdd;
         }
       }
       else
       {
          fixedString = ( cutIfLonger )
                      ? stringToChange.Substring( 0, length )
                      : stringToChange;
    }

       return fixedString;
   }
}

I need to add many overloads for default options.

I.e.

public static string GetStringFixedLength(
   string stringToChange,
   int length,
   char charToUse
   )
{
 return GetStringFixedLength(
               stringToChange,
               length,
               charToUse,
               false,
               true
               );
}

public static string GetStringFixedLength(
   string stringToChange,
   int length
   )
{
 return GetStringFixedLength(
               stringToChange,
               length,
               ' ',
               false,
               true
               );
}

Should I declare and create a static object for every overload to use
in a lock block, or can I do something else? I need many overloads
(more than 20, and I need to write many other functions with a lot of
overloads in the same class).

Some tips?

Thanks a lot.
Patrice - 04 Aug 2005 14:45 GMT
Depending on what you are doing, each overload could perhaps call the same
full featured function with appropriate default values ?

This way you would have a single code path and the other overloads are just
making a call to the same full featured function providing default values...

Signature

Patrice

> Hi,
> I have to do a sealed class with some functions that ruturns me a
[quoted text clipped - 87 lines]
>
> Thanks a lot.
am - 04 Aug 2005 15:45 GMT
Yes, all funtion will call the full one with appropriate default
values.

So, I do not need to use lock?

I think I should study a little more aboute threading...

Thanks a lot!
S. Senthil Kumar - 04 Aug 2005 18:03 GMT
You still need to lock the function that all other overloads end up
calling.

Regards
Senthil
Patrice - 05 Aug 2005 10:16 GMT
Not that familiar but if you are using the same code path you'll end up with
locking your critical resource. As a side note I'm not sure it's needed
here. What is the critical resource you are trying to protect ? It looks
like you are using just static methods but they don't share any static
fields or whatever.

How could they use the same shared data ?

Let me know about your findings. I'm myself not well versed in threading...
---
Patrice

> Yes, all funtion will call the full one with appropriate default
> values.
[quoted text clipped - 4 lines]
>
> Thanks a lot!
am - 05 Aug 2005 13:19 GMT
I need to share an HashTable, in some other functions.
However now I think I know how avoid problems on overloading.
Probably I will need help for other things.
Thanks a lot.
Patrice - 05 Aug 2005 16:16 GMT
Ah ok. Didn't saw it in your code. Was wondering. Sorry.

Signature

Patrice

> I need to share an HashTable, in some other functions.
> However now I think I know how avoid problems on overloading.
> Probably I will need help for other things.
> Thanks a lot.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.