Reference To The String In The Heap Is Passed Even When You Pass The String ByVal To A Method?

Mar 2, 2011

So strings are reference types right? My understanding is a reference to the string in the heap is passed even when you pass the string ByVal to a method.

String myTestValue = "NotModified";
TestMethod(myTestValue);
System.Diagnostics.Debug.Write(myTestValue); /* myTestValue = "NotModified" WTF? *[code].....

And what is going on under the hood? I would have bet my life that the value would have changed....

View 5 Replies


ADVERTISEMENT

Pass The String As A NULL If Nothing Is Passed?

Oct 31, 2011

If I send a String set to Nothing as an input parameter to a SQL stored procedure, and in the sproc it's like this @MyString Char(9) = NULL, what does it turn NULL into? Because I'm firing the sproc and it's not firing the statement that says IF @MyString = NULL... So I don't know what it is... Or how can I pass the string as a NULL if nothing is passed as a parameter to my calling function?

View 4 Replies

Passing String ByVal Into Function Without Changing Reference Value?

Jun 26, 2009

I know strings are immutable, so the minute you change a string reference's value .NET makes a brand new string on the heap. But what if you don't change the value of a string reference; rather, you simply pass it into a function ByVal -- does this operation copy the string value on the heap as well? My inclination is "no," but I'd like to confirm.

For example:
Public Function IsStringHello(ByVal test As String) As Boolean
Return (String.Compare(test, "Hello") = 0)
End Function

Calling program:
Dim myWord as String = "Blah"
Dim matchesHello as Boolean = IsStringHello(myWord)

I know passing myWord by value makes a copy of the reference to "Blah", but since I have not tried to change the string itself, would it make another copy of the string on the heap?

View 6 Replies

C# - Pass An Integer As A String Parameter To A Method Without Calling .ToString()?

Mar 12, 2010

In VB.NET, it is entirely possible to pass an integer as a string parameter to a method without calling .ToString() - it's even possible to call .ToString without the ()'s. The code will run without a problem, VB will interpret the integer as a string without having been told to. In C#, these would cause compilation errors - you are required to call .ToString() and to call it correctly in that situation before it will compile. Is there a way to make the VB compilation process check for the same things as the C# compilation process? Would it be best practice in a mixed team to force this check?

View 3 Replies

Limit To The Parameters That Can Be Passed ByVal Or ByRef To A Function ?

Jul 2, 2009

Is there a Limit to the parameters that can be passed byVal or byRef to a function or procedure in vb .net ?

[URL]

View 2 Replies

Use IList(Of IList(Of String) - Pass It As A Parameter To Another Method?

Aug 2, 2011

I'm trying to use a variable that is simply a list of a list of strings.I've declared it as follows:

Dim iRows As New List(Of List(Of String))

Then I'm trying to pass it as a parameter to another method and I've defined the method as follows:

Public Sub Import(ByVal Rows As IList(Of IList(Of String)))
For Each Row As IList(Of String) In Rows
ImportRow(Row)[code]....

Unfortunately, when I try to run that code I get the following error where it tries to pass my variable to my method.

System.InvalidCastException was unhandled by user code
Message="Unable to cast object of type 'System.Collections.Generic.List1[System.Collections.Generic.List1[System.String]]' to type 'System.Collections.Generic.IList1[System.Collections.Generic.IList1[System.String]]'."

When I change the method definition to use the types rather than the interfaces as follows, it works.

Public Sub Import(ByVal Rows As List(Of List(Of String)))
For Each Row As IList(Of String) In Rows
ImportRow(Row)[code]....

So, is it not possible to use generics with interfaces in this way? It works fine as long as I'm not nesting them.

View 3 Replies

Public Sub SaveExcelFile(ByVal FileName As String)?

Nov 22, 2011

Public Sub saveExcelFile(ByVal FileName As String)Dim Excel As Object

[Code]...

that is my code. everytime i open the saved file, it says that it has a different file type and asks me if i want to continue opening it and i click yes. the data inside the excel file is only the word "Success"

View 14 Replies

Copy The Value From String To Char Array Using String.CopyTo() Method?

Jul 13, 2010

I am trying to the copy the value from string to char array using String.CopyTo() method.

Here's my code

Dim strString As String = "Hello World!"
Dim strCopy(12) As Char
strString.CopyTo(0, strCopy, 0, 12)

[code]....

Edit : I get the this error at runtime.ArgumentOutOfRangeException Index and count must refer to a location within the string. Parameter name: sourceIndex

View 2 Replies

Use String.format On A Url To Pass Several Values Into The String?

Feb 3, 2012

I am trying to use string.format on a url to pass several values into the string. It's probably a simple error but I cannot get the following code to work. It doesn't even build the string.

Public Sub getStockData()
Dim client As New WebClient()
Dim url As String

[code]....

View 1 Replies

Get Value Of A String That Is Passed As Parameter?

Oct 19, 2011

I need to pass in parameters to my sub/function. When the parameter is passed, the value in the string, I would like to get the value evaluated and sent as:

Dim strParams As String = drRow(0)
' drRow is a row from DB Table. the value in drRow(0) =
' "@FromDate=""" & Now.AddDays(-10).ToShortDateString & """&@ToDate=""" &

[code].....

View 2 Replies

VS 2005 String Not Being Passed?

Dec 6, 2011

I'm sure there are better ways to do this and i am open to suggestions. I'm not worried about speed i figured get it to work then i could improve it but my problem is mynewstr2 is not being set to the last value of mynewstr for some reason.(probably something silly im overlooking right now). at this point just converting the image to a string i can work with for my very basic ocr.

Public mynewstr2 As String
Public Sub pictostr(ByVal image As System.Drawing.Bitmap, Optional ByVal Mode As BWMode = BWMode.By_Lightness, Optional ByVal tolerance As Single = 0)

[Code]....

View 7 Replies

Invoke C++ Method With String Handled As String Array

Nov 17, 2010

I have a C++ method with this signature:

[Code]...

View 1 Replies

Public Function GetHDDFreeSpace(ByVal Drive As String) As Double?

Jul 28, 2009

ManagmentObject is not recognized!!! Help me!

Imports System.Management
Imports System.Management.Instrumentation
Public Class Form1

Public Function GetHDDFreeSpace(ByVal drive As String) As Double

If drive = "" OrElse drive Is Nothing Then
drive = "C"
End If

[Code]...

View 3 Replies

Addline Function - Public Overloads Sub AddTextLeft(ByVal Text As String

Dec 13, 2010

I've got question. I created this;

Public Overloads Sub AddTextLeft(ByVal Text As String, ByVal Fontsize As Decimal, ByVal Bold As Boolean)<br/>
'-- PBA / 22.08.2006<br/>

[CODE]...

View 5 Replies

Compare The Data Passed In Query String?

Feb 3, 2011

sir i m working on a project and i want to pass a value in query string and redirect it to next page .....ok now on next page i want to make a check on this if the value of querystring is equal to a particular value then a message show.... i have done upto here now the prob comes if the values is not matched the code written in else is not working i m writting my code:-----

if(request.param.count>0) then
label1.visible="true"
label1.text="hello"
else
label1.visible=false

View 1 Replies

Source For Image To Pick Up Value Passed In URL As Request String?

Nov 8, 2010

You pull the value out of the Request object
Request.Querystring()
Then set the ImageUrl property of your image

View 1 Replies

Use The ToString Method And A Counter To Concatenate A File Name Into A String Variable From The Image.FromFile Method?

Dec 18, 2010

I have nine pictures that I need to animate -I need to use the ToString method and a counter to concatenate a file name into a string variable from the Image.FromFile method. Once the counter reaches its maximum value, and the last picture is displayed, the counter should be reset to zero or one depending on how the first image file has been name. Also a static counter variable should be incremented in the time routine. what I have so far -- I know what I have to do; however, I just do not know how to code this properly.. Right now I have this going thru a button procedure but it needs to go through a timer.

[code]....

View 2 Replies

Pass ByRef And ByVal Parameters In Same Routine?

Dec 7, 2010

Is it considered bad practice (VB.NET or any language) to code a function with both ByVal and ByRef parameters as used in my getFile function below?

Function getFile(ByVal channel As Integer, _
ByRef Filename As String, _
ByRef Filesize As String) As Integer
End Function
Dim status As Integer

[Code]...

View 1 Replies

Passing Reference Types By Value (ByVal)

Aug 17, 2009

I'm still working on the code in this thread [URL] and as I mentioned in that thread, I thought that passing objects around between methods would probably be bad for performance (perhaps not really noticeably at first but maybe if I did it enough then it could have an impact).

1. When you pass a reference type to a method using ByVal then what is actually 'copied' ? I understand that ByVal copies the object into the local method that you are calling where as ByRef passes a reference to the actual object so you can work with the original object. So when you have a Reference type and you pass this ByVal then does copy the entire object or does it just copy a 'reference' to the object?

2. When working with an object that relates to a 'physical' item, such as a file on disk or a network stream between two connected machines, then what happens when you pass it around between methods (this might be irrelevant depending on the answer to the first question) ? I mean if I have a NetworkStream object that represents the data stream coming from a remote computer, if I then pass that ByVal into another method then does that mean I am still working with the same data stream or a copy of it or what?

View 9 Replies

Calling A Method When The Method Name Is Contained In A String?

Oct 5, 2011

Let's say I have a page Test.aspx along with test.aspx.vb.Test.aspx.vb contains a class name "TestClass". In that class I have method1(), method2() and method3()I need to be able to call one of those methods, but I can't hard code it, the method to be executed comes from a string.

I can't do
Select Case StringContainingTheNameOfTheDesiredMethod
Case "Method1"

[code]...

View 3 Replies

Cannot Use "Byval" String To Connect With Strings

Oct 20, 2009

I'm working on a class that creates a communication between multiple computers. When a client connects he has an ID which is a string.Now I need to show the user ID in a text box and write "SomeUser Connected!" (Replace SomeUser with the ID), this is the sub[code]...

View 5 Replies

Switching Byref To Byval On Method Calls .NET?

Mar 1, 2010

Switching Byref to Byval on method calls I have many warnings raised due to: "Implicit conversion from xxxx to yyyy in copying the value of 'ByRef' parameter zzzz back to the matching argument."

My feeling is that it would be safe to change the function parameters from byref to byval as nothing special is being done with the reference type pointers inside these methods the reference types are simply being used and I think the behaviour would be exactly the same if running with a copy a pointer rather than the original.

Another consideration is that I have two classes which inherit from a base class. The same situation is occuring in that the byref params are causing implicit casting from the base class to the narrower concrete class. Again I can't see any problems with this code running byval either.Does anyone have any tips regarding use of parameters in functions when dealing with reference types?

Some of the other things that are currently being passed around byref in my project are database connection objects i.e. OracleConnection and SqlConnection. Is there any good reason for passing these around byref?

[Code]...

View 1 Replies

Populate A List(Of Object) In A Class Method, Where The List Was Passed To The Method As A Parameter

Aug 13, 2011

I'm trying to code a class of RandomNumber. One of the class methods needs to populate a "List (Of RandomNumber)" ... which was passed as a parameter to the method ... with 10 random numbers between 1 and 50. DEAD SIMPLE :)

View 6 Replies

Format String To Pass To String.Format That Would "move" The Decimal Point?

Aug 26, 2010

So I need a format string to pass to String.Format that would "move" the decimal point.I can't perform any math operations before doing the String.Format, so it has to work right off the bat.Basically I'm emulating a formatting string from a proprietary server. In it if I say:

"MR2"

for the value:

12345

The result is:

123.45

I'm close with this, but it's not spot on:

String.Format("{0:#0.##}", 12345)

an extra, but not necessary... there is also MR2Z, which moves the decimal 2 left, but if the value is 0 "" is returned.

View 5 Replies

Variable Passed By Reference?

Oct 28, 2011

I am working on a little program (game) is called Pig latin. I created a sub procedure and I passed the word to be converted by value and the piglatin word byref.Now when I call the procedure in the button click event I get the wavy green line under the variable passed by reference but the program works ok. Call Convert_to_Pig_Latin(strOriginalWord, trPigLatinThe error message when I hover my mouse over the green line says " Variable strPigLatin' is passed by reference before it has been assigned a value. A null reference exception could result at runtime."

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

[code].....

View 4 Replies

How To Pass String From Vba

Mar 18, 2012

I want get a string from excel cell by vba and pass that string to vb.net function

View 1 Replies

Pass String To Vba?

Mar 15, 2012

How can I correct this?

In vb

Private Sub TextBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.DoubleClick
Dim oExcel1 As Excel.Application

[Code]....

View 3 Replies

Dynamically Create An Instance Of An Object When Passed The Name Of The Object As A String?

Nov 12, 2010

I have a class library that contains a number of classes. I would like to dynamically create an instance of one of these classes, set its properties, and call a method.

Example:

Public Interface IExample
Sub DoSomething()
End Interface
Public Class ExampleClass

[Code].....

View 1 Replies

Forms :: Store The Reference Passed To A Sub?

Aug 6, 2010

I've searched a lot on both google and here but sorry, couldn't find.Maybe I'm just asking the wrong question, so I'll try to explain it here to see if someone can help me:

what I'm trying to do is what in C would be as simple as int varptr = &var I have a form that is just a communication watcher/logger. It's just a blank text box with a Pause/Play button and a copy to clipboard button and every new activity on my serial communication is logged. At the moment the form have a timer that once a second do textbox.text = serial1.log ...

but the problem is that I also have serial2; serial3; sql1 that I would like to keep an eye on them, and considering my work here I'm sure I would be re-using this form a lot if I could make it portable. Create one form for each is just stupid, as you'll all agree.

Then I would like to have an sub initialise(byref log as string) that would store this reference into an local variable within the form; so that every new instance of my form could be initialised with whatever log they'll be looking for and the timer just make textbox.text = LogPointer to the actual value of the log string.So, as I said I really wanted a int varptr = &var

View 4 Replies

Variable Is Passed By Reference Before It Has Been Assigned A Value

Jul 7, 2008

Im getting the Variable (Data) is passed by reference before it has been assigned a value error.

[Code].....

View 3 Replies







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