Use Of Static Local Variables In Lazy Loading Property

Sep 19, 2011

I just recently learned about the uses of static local variables in VB.NET and wondered about it's potential use in lazy loading properties.

Consider the following example code.

Public Class Foo
Implements IFoo
End Class

[Code].....

As far as i can see, this has a few advantages over the usual implementation, primary your inability to access the variable outside of the property, as well as not having to use an additional variable.

My question to you is: Which of those is the "right" way to do it? I know that static variables have additional overhead, but is it bad enough to create, in my personal opinion, unclearer code that can be misused easier? How much performance do you lose compared to the "traditional" method? How does it matter for small classes compared to huge factories?

View 2 Replies


ADVERTISEMENT

Local Variables In Shared Method Work Like Static Variable In C?

Aug 23, 2011

Will the list in this shared method keep its state throughout the life of the method? Or will a new list be created every time this method is called?

[Code]...

View 3 Replies

Asp.net - Local Static Variables In Timer.Tick Event (Stopping A Timer)

Jul 9, 2009

I have a timer on a page in ASP.NET.

After a certain period of time elapses, I want to disable the timer.

I want to put a static variable in the timers tick event that will track how many seconds have elapsed.

My question is, will this work?

If user X and Y are viewing the page will they both have separate local static variables?

What is the best method of shutting down an ASP.NET timer after a certain elapsed time?

View 1 Replies

Lazy Loading On A Collection Of Objects?

Mar 6, 2009

i have a sql query that can bring back a large number of rows via a DataReader. Just now I query the DB transform the result set into a List(of ) and data bind the Grid to the List.This can result occasionally in a timeout due to the size of Dataset. I currently have a three teir setup where by the UI is acting on the List of objects in the business layer.

View 3 Replies

.net - Handle Lazy Loading/ConnectionString In Library?

May 4, 2011

I've created a solution that contains a single class library project and two web applications. My main problems are the connection strings. These are held/declared in the web projects and I'm having to pass them into the class library every time I perform any kind of data access. I sort of understand why I should do this so I'm going with it for the moment.This has now led me to a problem/question with lazy loading. I'm using lazy loading for the following property:

Public Property KeyRelationshipManager() As Employee
Get
If _keyRelationshipManager Is Nothing Then
_keyRelationshipManager = Employee.GetEmployee(_keyRelationshipManagerStaffNumber)

[code].....

I need to pass in the connection string to that function.This means I would need to pass the connection string in to the property every time I use it so I could pass it into the function.Is this correct? It doesn't 'feel' right to me because I'm going to have to adjust a huge number of functions and property and pass through the connections string.

View 1 Replies

Error With The Using Statement And Lazy Initialized Property

Dec 1, 2009

The code below will throw an InvalidOperationException: The ConnectionString property has not been initialized. The exception is thrown at the line calling Connection.Open() in the Load method. If I use the try-finally statement instead of the using statement, everything works correctly. Can anyone explain why the exception occurs with the using statement?

[Code]...

View 2 Replies

Use Variables Or Properties, And Global Or Static Variables In A Class?

Jun 9, 2012

I'm new in .NET programming.I have a class Form1 that includes Button1_Click event.Button1_Click creates a multiple Text Boxies at run time)Here is the class:

Public Class Form1
Dim shiftDown As Integer
Dim counter As Integer

[code].....

View 3 Replies

Static Variables On A Web Garden?

Aug 17, 2010

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

View 1 Replies

Difference Between Static And Shared Variables In .NET?

Jun 22, 2009

exact diff b/t Static v/s Shared variables in VB.NET?

View 3 Replies

.net - When Are Inline Static Variables Initialized In A Class

Aug 10, 2010

Suppose we have a class like:

Public Class Question
Private Shared _field as Integer = CrazyIntegersRepository.GetOne()
' Some other useful things go here
End Class

And the method GetOne throws an exception... How can we manage that? Is a good practice to rewrite that into a static constructor? When is the GetOne method going to be executed if we leave it there in the inline _field declaration?

View 3 Replies

Sub Main In A Project-static And Global Variables

May 17, 2009

I try to create a sub main in a project but i can not.I have create a module called Module 1 and inside i have created a Main Sub which is intended to creta a thesaurus_form class instance , like this Code: Module module 1Public Sub Main()Dim thesaurus_form2= New Thesaurus_formEnd SubEnd module.Instead of this, once it has cretaed a first instance of the thesaurus_form class, it creates another one what i dont want at all..How can i do do make it execute the code which is inside the thesaurus_form_load sub..I also can not create a global variable or a staitc variable, because i receive this message.static is not valid in a member variable declaration.

View 2 Replies

Vb Shell Script - (supposed To) Local Area Connection Ipv4 Static Ip Address

Jun 12, 2011

heres the script basically it sets (supposed to) Local area connection ipv4 static ip address to 10.XX.XX.2 but it dosent work it works fine if you run cmd as addy and put the script in netsh interface ip set address "Local Area Connection" static 10.XX.XX.2

Dim txtlen As Integer = Len(TextBox2.Text)
Dim IPAddy As String = ""
Dim asciiSplit(4) As Char

[CODE]...

View 3 Replies

VB 2008 Why Are Static Variables Set Equal To 0 In The Declaration When 0 Would Be The Default Value Of The Numeric Variable

Apr 21, 2010

Why are static variables set equal to 0 in the declaration when 0 would be the default value of the numeric variable anyway when it's first declared? Not including the "=0" in the static declaration seems to work with no problem.

View 2 Replies

.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

Possible To Have A Shared/Static Dependency Property?

May 25, 2010

[using VB.NET, but I can easily read C# code in responses]I have a class called QuestionClipboard with ALL shared methods/properties. I previously had a QuesitonClipboard.doesClipboardHaveContent function that returned true/false if there was a Object on my 'clipboard'.I'd prefer to implement a Dependency Property so I can allow this true/false value to participate in data binding.The "GetValue(dp as DependencyProperty)" method requires an object instance, which would mean that my Property CAN'T be shared!Here is what the code would look like in my perfect world... Of course, the word "Shared" before the property declaration renders this code useless.

Private Shared clipboardHasContentPropertyKey As DependencyPropertyKey = DependencyProperty.RegisterReadOnly("clipboardHasContent", GetType(Boolean), GetType(QuestionClipboard), _

[code]....

View 1 Replies

Statically Type Local Variable To Be Static Type Of Expression On Right Had Side Of Assignment?

Dec 22, 2009

E.g. how do I get VB.Net to statically type a local variable to be the static type of the expression on the right had side of the assignment?

View 1 Replies

Disposal Of Local Variables

Feb 3, 2010

When you declare a local variable (inside a method), is it automatically disposed when the method ends? Or should I still use variable.Dispose() ?

View 16 Replies

List Of Local Variables

Feb 6, 2010

I am using a lot of variables in my code and like to have them listed or printed. How can I do that?

View 3 Replies

List Of Local Variables?

Feb 7, 2010

I am a Visual Basic 2008 user (under Visual Studio 2008) and I have numerous local variables in my project. How can I list them to a file or to a printer?

View 2 Replies

Reflection On Local Variables?

Feb 13, 2009

Is it possible to get the name of a local variable from a reference to the variable? For example, I can get the names and values of a calling function's parameters like this:

Dim frame As New StackFrame(1)
Dim pInfos() As ParameterInfo = frame.GetMethod().GetParameters()

Is there some way to get the same information for a calling function's local variables? This is kind of what I have in mind:

Sub SomeSub()
Dim count As Integer = 10
Dim average As Single = 45.67

[Code]......

View 4 Replies

Local Variables Are Automatically Shared

Mar 13, 2012

I was reviewing the MSDN documentation on VB.Net's little-used Static keyword for local variables. My question is not about how Static works (I personally plan to avoid using it as it seems like it could cause grief to future programmers who don't notice the side-effects.) My question is about this statement in the docs:[code]So can anyone interpret the above statement for me in a way that makes sense? Or is this a bug in the documentation? It's been there since the VS 2005 version of the docs and is still present in the Visual Studio 11 version.

View 2 Replies

Private Fields Or Local Variables?

Apr 18, 2012

I am wondering if you have a class and in the class you have a function which executes a sqlcommand. Which would u use ? the local variable declaration or the private member command variable.Assuming that the class has a private field called m_Cmd as OracleCommand..i am using oracle db so i use oraclecommand , if sql server then it should be sqlCommand.[code]

View 3 Replies

Serialize All The Available Local Variables And Save Them?

May 13, 2010

when an exception occures, I want to save the data available on the stack and log it. how can I serialize all the available local variables and save them?

View 1 Replies

VS 2008 - Loading Symbols Used On Local Machine

May 23, 2009

I'm aware of a way to load the fonts found on the locale machine, but is there also a way to load the symbols used on the locale machine.

View 7 Replies

Asp.net - Server.Transfer Losing Local Variables?

Oct 26, 2011

For my application, I have a page that redirects to another page (within the same application) via Server.Transfer. I need to do this because the original page has an object that I need to access by using the Page.PreviousPage property.

Once my "destination" page has been fully loaded, a local deep clone that I made of the source page's object is suddenly released from memory once I perform a postback? Is this by design--something to do with the Server.Transfer?

Page1.aspx:
Public Structure myCustomObject
Implements ICloneable
Dim someField as String = "default value" ' Default value

[code]....

At this point, EVERYTHING works as it should. Stuff got cloned over correctly and all is well.(Let's say this is on a button click event) Debug.Write(newObj.someField) ' Output: "default value"<- This is NOT "changed value from source page" for some reason when it was working literally a few lines ago! My guess is that the Server.Transfer stops any association with the source page after the new page loads.

View 1 Replies

Creating Temp Local Variables - Bad Practice?

Jan 12, 2012

When I want to maintain code readability and keep things neat. I would create temp variables . Below is one of my functions that uses it. Is it a bad practice? I would create temp variables cost more memory ?

Private sub test(byref transac as transaction , byref txntime as string )
Dim cmd as OracleCommand = nothing
Dim Adapter As OracleDataAdapter = nothing
Dim SQLStr As New StringBuilder
[Code] .....

View 9 Replies

Server.Transfer Losing Local Variables?

May 24, 2010

I have a page that redirects to another page (within the same application) via Server.Transfer. I need to do this because the original page has an object that I need to access by using the Page.PreviousPage property

View 1 Replies

Loading Variables From .xls File

Nov 18, 2009

I have been searching for days to get some code that will do this. I need to open a spreadsheet and read maybe 2 dozen cells into variables in a VB.Net program. Specifics are: MS Office 2003, Vis Basic of VB.Net 2008. Everything seems to revolve around the following lines which do not work for me. I am not sure if this is version differences as most ppl here do not name which version they are using. [code]Does this method not work in VB.NET 2008?

View 2 Replies

Private Shared Variables Vs Local / Namespace Performance

Jun 8, 2009

I came across a number of new Private Shared variables (of type Hashtables(Of String), initialized in the declaration) added to a partial class for a very large (DataContext-derived) class. This seems sensible to me in one sense because they never change, and making these shared variables ensures that they won't get re-initialized every time a function is called. However, these variables are only used within the scope of one function in the class, and I fear the private namespace of this DataContext-derived class is getting rather polluted, and having these sorts of things exposed at such a high level might be confusing to others reading the code in the future.

Would there be negative performance impact to making these local variables within the function where they are used, or is there some better way to handle this? Basically we are using these 3 hashtables to determine whether anything within particular subsets of properties changed (using GetModifiedMembers and then using the Overlaps function of the hashset to see if any of the modified members correspond to members we care about).

Edit: I caved and took the time to write my own test program, which confirmed that there is a cost to using local variables (which I assume applies generally to all cases -- I doubt there's any case where a shared variable would be slower unless using the shared variable requires some additional logic to do so properly): [Code]

So in this particular case, using the local variable costs about 200%. But in most cases (including my own), the time is probably negligible compared to the overall task. So I guess the question now becomes, how do people generally feel about improving code maintainability at the cost of negligible but known performance impacts?

View 3 Replies

Reading SQL Select Values Back Into Local Variables?

Apr 20, 2010

I as part of the support VB code for a larger ASP project, I need to read back the existing data from an Access database, to see if user has changed a value.

I'm reading the existing data back into a DataView using the following function:

Public Shared Function GetPolicyStatus(ByVal intPolicyID As Integer) As DataView
Dim strConString As String
Dim conMyData As OleDbConnection

[Code]....

View 4 Replies







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