Should Always Lock Static Methods?

Nov 3, 2011

In the following example GetList returns an instance of a static (shared) variable. That one needs locking in order to be thread-safe.But what about DoSomething which doesn't use any static variables outside the method? Does it need locking too?I'd like to clarify that in this particular case I expect DoSomething to always print 0-100 in sequence (i.e. no 0123745...) regardless of number of calling threads. Or, in general, that different threads don't affect each other's variables (printing to console is only an example). The language in the following example is VB.NET.[code]

View 3 Replies


ADVERTISEMENT

.net - Static Property And Lock Usage

Mar 27, 2009

Is this code correct or is there any possibility of some random threading deadlocks etc?

Is it a good idea to use static properties and locking together? Or is static property thread-safe anyway?

Private Shared _CompiledRegExes As List(Of Regex)
Private Shared Regexes() As String = {"test1.Regex", "test2.Regex"}
Private Shared RegExSetupLock As New Object

[Code]....

If it's not obvious code is compiling regexes and storing in a List(Of Regex) so they can run faster. And it's shared so every instance of class can get benefit out of it.

View 6 Replies

C# - Static (Shared In VB) Or Normal Methods

Oct 27, 2009

I Want to know which one is preferred while coding to use Static Methods or normal instances, I prefer to use static if they where few but if there was many of them I start to get some doubts

[code]...

if the EmployeeManager Has Many methods (selects deletes updates) is it ok to make them all static.and if it was Normal instance. wouldn't be a drawback if the object is initiated every time specially if GetAllEmployees() is heavily used.What is the better approach to use?

View 3 Replies

C# - When Should Use Public / Private / Static Methods

Apr 27, 2009

I'm new to C#.Till this moment I used to make every global variable - public static.All my methods are public static so I can access them from other classes. I read on SO that the less public static methods I have,the better.So I rewrote my applications by putting all the code in one class - the form class.Now all my methods are private and there's no static method.

My question: What should I do,keeping everything in the form class is dump in my opinion. When should I use public,when private and when static private/public? I get the public methods as a 'cons' ,because they can be decompiled,but I doubt that.My public methods can be decompiled too.What is so 'private' in a private method? EDIT: I'm not asking how to prevent my program to be decompiled,I'm asking whether I should use static,private and public.And also : Is there are problem in putting all the code in the form class so I dont have to use public methods?

View 6 Replies

Permission Denied / Cannot Call Non-public Or Static Methods Remotely

May 17, 2010

There are no "non-public" or "static" methods in my code. All are public. What I'm trying to do is pass a FrameworkElement (more specifically a web browser control) that was created in one process over to another process for display and use. Also I'm not using (and would to avoid using) any of the framework 3.5 addin stuff.[code]

View 2 Replies

Mvc - Repository Pattern Implements Methods By Adding Add1 But Should Use Methods Of The Baseclass?

Aug 29, 2011

This is the original code in c#

public class CategoryRepository: RepositoryBase<Category>, ICategoryRepository
{
public CategoryRepository(IDatabaseFactory databaseFactory)
: base(databaseFactory)

[Code]...

Does anyone has an idea what i should change to let it work and let my UserRepository use the methods in RepositoryBase while implementing the IUserRepository?

View 1 Replies

Cant See Available Methods List When Write Object.METHODS?

Jun 24, 2011

I am working with a vb program, but there is something strange on one of my .vb code pagewhen i put the "dot" afther the object name its dont show the methods availables for this objectbut on other vb code pages i can see it. but in this one no.for exmaplethis is a piece of code: Dim sb As New StringBuilder()

View 3 Replies

Making Class Methods Instead Of Instance Methods In .NET?

Mar 29, 2010

I am not sure how clear my question is by the title, but I am trying to make Class methods instead of Instance methods in Visual Basic that way I don't have to waste memory and code creating temporary objects to execute methods that don't need instance variables.

I am not sure if you can do that in VB but I know you can in Objective-C by using either a "+" or "-" sign in front of the method declaration. And in C++ (at least I think, I can't remember) you put the static keyword or const keyword in front of the function.How would I do this in VB if it is possible? Or should I just make a separate set of functions that are not members of a class?

View 2 Replies

2 Different Methods Using Same Object Methods?

Aug 26, 2010

Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim someproc As Process

[Code]...

View 3 Replies

Static Constructor In .Net?

Feb 9, 2010

I am rewriting a C# class in VB. The C# class has a static constructor in it:

static Class()
{
...
}

which, through the Call Stack, I found is called by the main window's InitializeComponent() method.Everywhere I've looked has told me that the corresponding static constructor in VB is just:Shared Sub New()

but this method is never invoked. Am I creating my VB static constructor right? Is it likely something else that has nothing to do with my static constructor?

View 1 Replies

Static Variable In Asp.net?

Dec 15, 2011

I am writing a code for a webapplication in vb.net. I am using a shared variable in the vb code.

Consider the static variable value is 3 for a user who hits the web application. Again when another user hits the same web application, whether the static variable will be 3 again or whether he will have a separate value for static variable for his session?

View 1 Replies

.net - LINQ And Static ObservableCollection?

Nov 7, 2011

This will probably be a basic questions for the LINQ & architecture experts however I am failing to understand a problem i've encounted when trying to update a 'Static ObservableCollection.

Me.Grid1.ItemsSource = ContactList Me.Grid2.ItemsSource = From s In ContactList Where s.ContactTypes.Any(Function(t) t.ContactTypeName = "Christmas List")

If I add a new Contact with the ContactType "Christmas List" to the ContactList ObservableCollection, Grid1 reflects the additional Contact however Grid2 does not reflect the change unless I rebind.

Anyway to Reflect the change in Grid2 to show the new Contact with the queried ContactType

View 2 Replies

.net - Static DateTime For Testing?

Dec 11, 2009

We have a DateTime Service that pulls the Database date down when we need to use a Date. Now, If I want to run a test using "Todays" Date using FitNesse, What would be the easiest way of creating a way to have (OurService).CurrentDate be static? We have a control that ads Days,Years, or Months to the current date.. and I would like to be able to test this.

View 1 Replies

Adding A Static Crypt Key?

Jul 15, 2011

I am currently using Visual Basics 2010 (Ultimate), and I am trying to modify a free-project listed here:I want the above project to use these hard-coded values below:Key= "087CF262E0E856B8F911B369EE027CF9330F6288343651B1198379A9813BC8C4"
IV = "11D2A07242B79C752E6B53CDD14792C9"So basically i want to encrypt AND decrypt with the above keys.

View 8 Replies

C# - Shared / Static Variable Should Be Nothing?

Jan 6, 2011

I have the following code:
Public Class TestClass
Public Sub Main()
If theGlobal IsNot Nothing Then Throw New Exception("What gives!")
End Sub
Private Shared theGlobal As Object = Nothing
[Code] .....
Why is theGlobal object NOT Nothing?

View 2 Replies

C# Vb: When They Say Static Classes Should Not Have State?

Apr 23, 2011

when they say static classes should not have state/side effects does that mean:

static void F(Human h)
{
h.Name = "asd";

[code].....

View 3 Replies

Limit Static Integer?

Apr 28, 2011

how to limit static integre.

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Static a As Integer[code]......

View 2 Replies

Static Gmaps In Winform?

Oct 2, 2011

Im trying to load a google static map in a winform. I found a control online that would helpme but i cant get it working. i got the control from Here. i was not able to drag on the control on to the form like others. i have also email the dev of the control but have not heard back yet.

[Code]...

View 1 Replies

Static Variables On A Web Garden?

Aug 17, 2010

Do vb.net static variables work on an IIS web garden?

View 1 Replies

VB Static Date Updates?

Oct 11, 2010

I have an excel sheet developed by a vendor that I'm stuck with the current functionality as a short term solution. The users workflow have them pull 4 separate sets of data which they are copying and pasting into a different spreadsheet where they append the data to previous days' pricing for trend analysis. That macro is built looking for the first blank row in column A, simple enough.

However, they then add the current day's date to this other spreadsheet as an additional column to do that trending. I found a nice link to a site that mentioned I could do this logic by adding code by right clicking the sheet name and selecting 'View Code'. I then entered the following logic that states if column A is populated, populate Column T with today's date statically (Today and Now don't work because I need it to stay at that date when it is refreshed). My issue is that this works great if I enter test data into cell A and hit enter. However it is not populating the date if a copy and paste is executed into the column A. Is there something I'm missing or a recommendation to have this work with copy/paste?

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("A2:A15000")) Is Nothing Then

[code].....

View 2 Replies

What Is Equivalent Of A Static Class

Nov 30, 2007

what is the Equivalent of a Static class in VB.Net. Is it Modules?And When i used one of the C# to VB.Net conversion tools then the code "Public Static class Class1 " was converted to "Public NotInheritable Class Class1"

View 1 Replies

Why Is Static Method Not Visible

Sep 14, 2011

I have a C# method in my datatier that I am trying to convert to VB.Net. I converted it to VB.Net but when I bring up the datatier class the method is not showing. It has been a long time since i have used VB.Net and forgot alot of things [code]I believe it has to do with the declaration, which i have as:Public Static Function UserActInfo(ByVal _eno As String) As useraccount..

View 1 Replies

C# - Assign Value To Readonly Static Field?

Nov 17, 2011

I have a field which is static and readonly. The requirement is that the value should be allocated to the field at the login time and after that it should be readonly. How can i achieve this ?

public static class Constant
{
public static readonly string name;
}

View 5 Replies

C# - Performance Of Static Properties/functions?

Apr 5, 2011

is there a difference in performance when making properties static/shared that were instance properties before, maybe any locking mechanisms?There is a heavily used object in HttpCache which was accessible through a property in a page-instance(Service.aspx). Now i wonder if it would be better to make it static because the HttpCache is shared across the application anyway.

View 4 Replies

C# - Reset A Static/shared Class?

May 4, 2009

I've got a shared class (static in C#) which mostly carries some settings data that any class in the application can read and sometimes write. Also there are some static properties which holds some internal states.

Now I want to revert this class to initial stage of it. With all default variables etc. Assume that the user want to reset the current state and start over without restarting the application.

In a singleton model I'd simply renew it with something like this :

Public Sub Reset()
_Instance = New MyClass()
End Sub

However this is not possible in a Shared class. Or should I switch back to Singleton?

View 3 Replies

C# - Static Checking For Framework Conventions?

Jan 27, 2012

Is there a product/project that would allow you to define conventions for say an MVC Project to statically check for naming conventions like Controller being appended on the end of classes that inherit from controller and/or enforce a certain method signature when decorating a method with an attribute.I am basically looking for a way to kind of set up some guard rails for new developers coming onto our team where we have a clear set of conventions some of which are used to wire things up dynamically through reflection. Seeing that this reflection wire-up would fail because of an incompatible signature would be a huge boon to our ramp up process.Static/Compile time checking for broken rules Ability to target methods decorated with specific attributes (via RegEx or a Wizard)Different Sets of rules based on different types of projects.(example: A set of conventions for an MVC App, a different set for a Web Forms App, and a different set for a Class Library suffixed with .BLL)?

View 1 Replies

Call A C# Class's Static Method ?

Oct 28, 2010

I have a C# dll and want to use it in VB.NET. I'm using C# 2008 Express and VB 2008 Express. I have added a reference in a VB project to the C# dll. When I create an instane of a class in the C# dll, it gives the following error messsage: "Type 'RF.RabinFingerprint' has no constructors". My C# dll code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;[code].......

View 3 Replies

Communications :: IP Address Static Or DHCP?

Apr 12, 2010

I am trying to write a quick utility that will find out if a Windows XP computer is set to Static or DHCP and write a log file c:computername_static.log or c:computername_dhcp.log.

View 2 Replies

Converting A Static C# Class To VB Module?

Dec 3, 2009

I am trying to convert following code into a VB equivalent so that I can use it with threading my forms and controls.

using
System;
using
System.Collections.Generic;

[code]....

View 3 Replies

Declare A Static Variable As Global?

Oct 25, 2006

How can i declare a static variable as global in vb.net?(Global static variable)

View 5 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved