473,320 Members | 1,910 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

lock and overloads

am
Hy,
I need 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.

Nov 17 '05 #1
5 1403
Should I declare and create a static object for every overload to use
in a lock block, or can I do something else?


I don't see why you're using lock at all. You're not reading or
writing any shared state.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #2
am
You're right...
I shouldn't need lock in that case.
However I will in others. Have you some tips?
Thanks a lot!

Nov 17 '05 #3

As Mattias said you are not sharing nothing, so there is no need to use a
lock

Also you are talking about a sealed class in your text , but in your code
you don't have a sealed class, you do have a "static" class.

What is what you want to do after all?
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"am" <am*******@email.it> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hy,
I need 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.

Nov 17 '05 #4
am
In the class that I will build there will be many functions, not just
that I wrote in the exemple.

Some functions will share resources, so I have to avoid simultaneous
access.

The class should be sealed (I forgot it in the exemple), and I have to
build it with c# 1.1, "static class" is c# 2.0.
Thanks.

Nov 17 '05 #5
Hi,

You should create one locking object by member variable you need to protect
from concurrency.

Take a look at Jon's skeet article about it
http://www.yoda.arachsys.com/csharp/
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"am" <am*******@email.it> wrote in message
news:11********************@g49g2000cwa.googlegrou ps.com...
In the class that I will build there will be many functions, not just
that I wrote in the exemple.

Some functions will share resources, so I have to avoid simultaneous
access.

The class should be sealed (I forgot it in the exemple), and I have to
build it with c# 1.1, "static class" is c# 2.0.
Thanks.

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: valamas | last post by:
Hi All How can I recode the following so that I do not repeat my code? Public Overloads Sub SetNodeImageIndex(ByVal oNode As System.Windows.Forms.TreeNode, ByVal ImageIndex As String) ...
12
by: Lee Silver | last post by:
In a base class I have the following 2 declarations: Overridable Sub Remove(ByVal wIndex As Integer) and Overridable Sub Remove(ByVal wValue As Object) In an immediately derived class I...
10
by: Atif | last post by:
Hi I am here to solve a small confusion i have in "Overloads Overrides". "Overloading" says that the method's name should be same while no. of parameters and/or their datatypes should be changed...
5
by: Laurent Allardin | last post by:
Hi, Why this code compile??? We should not be able to Override the code by using the Overloads since the sub as exactly the same parameters.... Thank you. Laurent Allardin,MCSD,MCAD
3
by: Arthur Dent | last post by:
Hi all, Im just curious, what is the purpose of the keyword "Overloads" in VB nowadays? I understand conceptually what overloads are and what they do, but im a little puzzled, because if you...
1
by: Ian | last post by:
I've just discovered the msclr::lock class in the C++ Support Library online documentation. This seems like a much cleaner way to implement thread protection than using...
2
by: Don | last post by:
How to stop a process which is running in a separate thread!!! I've got a class which performs some lengthy process in a background (separate) thread. And this lengthy process raises events...
12
by: André | last post by:
Hi, i'm learning working with classes. In class "classbase1", i defined an overridable function. In the class "subclass1", i defined the same function with 'Overrides'. In class "classbase2", i...
2
by: =?Utf-8?B?QU1lcmNlcg==?= | last post by:
In the class below, I inherit from Generic.Dictionary so I can override property Item. Item is not overridable, so I used Shadows, and it works as I want. It works equally well if I replace...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.