Instantiate Local Variable By Value?

Jan 7, 2010

I sort of understand why this is happening, but not entirely. I have a base class with a Shared (Static) variable, declared like so:
Public Shared myVar As New MyObject(arg1, arg2)

In a method of a derived class, I set a local variable like so:
Dim myLocalVar As MyObject = myVar

Now when I do something like myLocalVar.Property1 += value, the value in Property1 persists to the next call of that method! I suppose I get why it would be happening; myVar is being set by reference instead of by value, but I've never encountered anything like this before. Is there any way (other than my workaround which is to just create a new object using the property values of myVar) to create myLocalVar by value?

View 2 Replies


ADVERTISEMENT

Instantiate Object From Class Using String Variable For Name Of New Object?

Aug 11, 2009

dim myCollection as new Specialized.StringCollection
dim myFoundThings as new ArrayList
dim index as Integer
dim newResultMemberName as String

[code]....

This is part of some code that will run without user interaction once it's spinning away, and I need to create a unique object from some items found in a StringCollection, naming the objects using information found in the strings stored in that StringCollection.

View 2 Replies

For Each Variable Acting Like A Local Var?

Apr 21, 2011

I stumbled upon something i don't quite see the logic of. Let's ork with following piece of code:

For Each ds As DerivedScale In List
If ds.ScaleID = scaleId Then
ds.ScaleID = ds.ScaleID + scaleStep

[Code].....

This piece is written for 2 records to change place and change the sequence number (scaleid). The arrow indicates where the issue occurs. The item "ds" is replaced by the object 1 indexnumber higher/lower. This does however not effect that object in the List. So when i check, item ds isn't set.

However, when i look at ds.ScaleId = ds.ScaleID + scaleStep, this is reflected in the List.

So what I am wondering is: is "ds" acting like a local variable here, and can i only make changes to it's properties?

View 4 Replies

Global Vs Local Variable

Mar 22, 2010

I'm writing a subneting calculator in VB.NET.I'm using the IDE SharpDevelop.Well my problem is I'm using a function to do some calculations and then to figure out the class of the IP addresses.Well I pass a variable from the subroutine, that is accessed when a button is clicked, to the function then the variable is passed back to the subroutine and then the variable is passed again to another function but when it reaches this second function it doesn't retain the value it was given in the first function.I'm not sure if I'm just passing the variable wrong or if this is how it works.I know I can make this work probably by using global variables but I'm not sure if this is proper. So my ultimate question is what is the difference between local and global variables besides just scope and should I try and use one more than the other.

View 4 Replies

Local Variable / WithEvents

May 17, 2010

Does anyone know why a local variable cannot be declared WithEvents?T

View 3 Replies

How To Add Event Handler To Local Variable

Apr 25, 2012

I have a form in VB.NET that is used as a dialog in a mainform. Its instances are always locally defined, there's no field for it. When the user clicks the OK button in the dialog, it will fire an event with exactly one argument, an instance of one of my classes. Since it is always a local variable, how can I add an event handler for that event?

Code for the event, a field in MyDialog:
public Event ObjectCreated(ByRef newMyObject as MyObject)
Code for the main form to call dialog : (never mind the syntax)
Dim dialog As New MyDialog()
dialog.ShowDialog(Me)
AddHandler ObjectCreated, (what do I put here?) //Or how do I add a handler?

View 2 Replies

Returning Reference To A Local Variable?

Sep 15, 2009

is this safe (I know it works, but is it safe)? Is it returning a reference to a local variable, wich falls out of scope as soon as the function returns?

Public
Function myfunc() As String
Dim strXml As String

[code]....

View 9 Replies

Saving To A XML File From A Local Variable?

Mar 16, 2009

I just need some sample code to save the blue values to an xml file as shown below:

textbox.size =48
textbox.font.colour = Blue
Saved to c:myfile.xml as

[Code]....

View 9 Replies

Unused Local Variable Error?

Mar 24, 2011

Sub WriteEOFData(ByRef FilePath As String, ByRef EOFData As String)
Dim sFileBuf As String
Dim RoPepfMiyYgyrTfLY7YxdpSoSOycKgR06b1oCptGcO As String
Dim lFF As Integer
Try

[code]....

View 3 Replies

.net - Declare A Local Variable Whenever Access A Property?

May 7, 2011

say i have an auto property Public Property P As Integer and everytime I want to read this variable P in a function, i will declare a local variable as such: dim _p = P then read _p instead of P. I'm wondering does it make sense at all? will it actually make things slower (which of course isn't my intention).Btw if we change the question to Public Property P As Object is there any change in the answer?

View 7 Replies

Getting 6 Unused Local Variable Error Messages

Nov 8, 2010

I am getting 6 unused local variable error messages. If I comment out the variable, I get en error because it really is in use. I tried re-building project.

error

Warning 2 Unused local variable: 'sqlCustomerInsertTicketConn'. C:UsersJeffDocumentsVisual Studio 2008ProjectsJeffComputersPOS1JeffComputersPOS1Module1.vb 1741 17 JeffComputersPOS1

View 4 Replies

Maintain Local Variable Value Between Calls In Program?

Jul 21, 2011

Public Function MethodOne(ByVal s As String) As String[code]...

I want to retain the value of i, but once it goes back into MethodOne, it loses its value. I tried making it static i As integer = 0, but this did not work.

View 2 Replies

Should A Local Variable Call Dispose Method And Set To Nothing?

Oct 14, 2009

I have 2 question, hope somebody give me an explanation: 1. Should a local variable call dispose method and set to nothing? 2. If some method have an arguments which it is a ByVal argument, it means a method create a new object? But If it is a ByRef argument, it means a method don't create a new object?

View 4 Replies

Syntax - Does One Declare A Readonly Local Variable

Sep 11, 2009

How does one declare a readonly local variable in VB.Net? Java and C++ have final/const local variables so I'm sure VB.Net must have them, but I just can't find the syntax for it.

View 2 Replies

.net - Don't Create Local Variable To Hold The Reference Of The Object?

Nov 17, 2010

How does the GC dispose objects created in the following 2 scenarios?

1)

Private Function DoSomething() As Boolean
Return New DatabaseManager().Insert()
End Function

2)

Private Function DoSomething() As Boolean
Dim mngr As New DatabaseManager()
Return mngr.Insert()
End Function

In Option 1, I don't create local variable to hold the reference of the object. In Option 2, I hold the reference in local variable.What option is better and why? (if any)

View 2 Replies

Access Local Variable From Visual Studio Addin?

Mar 24, 2012

I am trying to write a visual studio addin. The following code is used to display the global variables or class names or function names of a selected text inside visual studio code window. However it do not display the variables defined inside a function. How can I modify this to display local variables?

'Call this function inside OnConnection event of the addin
Sub displayCodeElementName()
' Before running this example, open a code document from a project

[Code]....

View 1 Replies

Asp.net - Use A Page_load Local Variable In Event Handler Of A Button?

Apr 27, 2009

Im making a site in Visual Studio using vb and I have a variable in page_load but need its value in the event handler of a button for passing on session.

View 5 Replies

Catch Exceptions Without Defining A Local Exception Variable?

Apr 6, 2011

[Code]...

Notice the catch (Type) instead of catch (Type myVariable). Is this possible with VB.NET, or do you always have to declare a variable when you catch exception types, like so:

[Code]...

View 1 Replies

IDE :: List All The Local Variable Used Inside The Current Function Or Sub?

Aug 12, 2010

I have a button inside a one of my forms which contain a lot of coding and normally a lot of variables declaration.

Is there is a option/add-on that I can use/set that enable me to display all the variable used inside the sub or function I am currently working in?

All I can find is add-on to display the Properties/Methods and only Public variables inside the form or inside the class. but I need to list the local variables also.

View 8 Replies

Force A Compile-time Warning / When Using An Unassigned Local Variable?

Nov 15, 2010

Today I discovered that something I had assumed about VB.NET for many years was not true (worrying!). I assumed that a variable declared within a loop had a lifetime of the iteration it was declared in, but in fact it seems it has a lifetime of the whole procedure.[code]I had assumed an output of False, True, False, True but instead it is actually False, True, True, True..In C# the equivalent code would not compile as you would get a compile time error of Error "Use of unassigned local variable 'var1'".I realise there are many ways to fix this and that best practice would be to declare the variable outside of the loop and reset it at the beginning of every loop through.I find this behaviour so counter-intuitive to me that I would like at least a compile time warning in VB.NET when/if I do this. (I could also then set this on any projects I already have and get warning that would allow me to check that my assumptions aren't causing errors).Does anyone know how/if I can get this to generate a compile time warning in VB.NET? Am I the only one that finds this counter-intuitive?

View 1 Replies

Gained By Using The Function Itself To Hold The Return Value Instead Of Declaring A Local Variable?

Oct 28, 2010

What's best practice (in VB.Net):

Function GetSomething() as String
GetSomething = "Here's your string"
End Function
or
Function GetSomething() as String
Dim returnString as String = "Here's your string"
Return returnString
End Function

Obviously, neither of these implementations make any sense, but they're just meant to illustrate my point. Is there anything to be gained by using GetSomething itself to store the return value instead of declaring returnString locally and then returning that (does it avoid having an extra string allocated/instantiated - and if so, are there any performance/memory benefits)?

View 6 Replies

Increment Local Variable Inside Razor Syntax, MVC3

Sep 21, 2011

I am working on MVC3, i have a situation where i want to do something like this:

<Div>
@Code
Dim i = 1

[Code]....

but razor is throwing wrong syntax error message. how to do this properly in side razor code.

View 1 Replies

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

Serialize Two And VB Returns "local Variable Is Already Declared Within The Current Block?

May 17, 2011

I've used this code before but only when I was serializing one array. Here I'm trying to serialize two and VB returns "local variable is already declared within the current block."Someone tell me how to do this correctly, pleases. I'm banging my head against a brick wall that I can't see.

[Code]...

View 2 Replies

Error: A Local Variable Named 'arow' Cannot Be Declared In This Scope Because It Would Give A Different Meaning To 'arrow'

May 24, 2010

This webservice is almost converted form VB to C#, except I get this error shown below on the DataRow arow object when I use it in the foreach statement below to populate the Results Class with a DataSet Object...

Error: A local variable named 'arow' cannot be declared in this scope because it would give a different meaning to 'arrow', which is already used in a 'parent or current' scope to denote something else

using System;
using System.Web;
using System.Collections;

[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

Add Domain User To Local Group While Logged In As Local Account?

Sep 15, 2010

Does anyone know how this can be done? The problem is that when you run the script to add a domain user to the local group as a local user you have no permissions to query the domain. Is there a way to pass a user name and password when doing this? [code]...

View 3 Replies

Reference A Report Variable In The Report Properties Code Window Of A Local Report?

Apr 19, 2011

how I can reference a report variable in the Report Properties Code Window of a local report? I have tried Variables!Claimant.Value (for a variable called Claimant) but I get an error "Reference to a non-shared member requires an object reference." I have tried qualifying it with Report. and also with the report name in front of it but I get errors with this also.I have the variable CLaimant defined in the Variables section of the Report Properties.

View 3 Replies

C# - Instantiate The Object To Use It In The Immediate Window?

Dec 13, 2011

If I have a console application containing the class

[Code]...

I'm not a VB.NET guy. Am I correct in assuming I need to instantiate my object first? Why does the VB.NET example differ?

View 1 Replies

How To Instantiate A Class In A Web Site

Sep 28, 2009

I have created a WebSite with a folder called Classes. In the folder, there is a class with a Public Shared Function. How do I go about instantiating that class so I can use that function on a web form?

View 5 Replies







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