Showing posts with label byval. Show all posts
Showing posts with label byval. Show all posts

Thursday, March 29, 2012

Object passed ByRef or ByVal

Is MyClassObject passed to the sub ParseMyObject ByRef or ByVal?
If it is passed ByVal, which I thought was the default, would this be heavy on memory if the MyClass Class contained a lot of variables? And if it is ByVal, would it be more effecient for me to pass it ByRef cause it will make a pointer and not copy the whole object?
Thank you
Ryan


Dim MyClassObject as New MyClass()
ParseMyObject(MyClassObject)

Public Sub ParseMyObject(MyClassObject as MyClass)
asfsfsad
End Sub

Hi Ryan,
There is no point in passing Reference types (classes) ByRef. Any Reference type is, basically, smart pointer with type information and reference to actual class data. When you passing class ByVal, this structure being copied, but it reference the same location of actual data on heap. Bottom line: in most cases pass classes ByVal.
Is there a known list that defines "Reference" Type object?
I've read in one of the .net books that these operate differently? it didn't work for me though...


dim a as string = "123"
dim b as string = a
a = "456"
response.write(b) 'this would write 456 to the screen

dim x as integer = 4
dim y as integer = x
x = 6
response.write(y) 'this would write 4 to the screen?

So i'm guessing strings are always reference types and integers are not?
Help is appreciated
Thankyou
ryan
Hi Ryan,
It is bit confusing. String is reference type, but it behaves much like value type. It is immutable (new copy created on each change). Integers and other basic types are value types. SeeStrings UNDOCUMENTED for great in depth look at .NET strings.

Object reference error

Private Sub cmdLeftRight_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles cmdLeftRight.Click, cmdCenterRight.Click, cmdCenterLeft.Click, cmdRightLeft.Click

Dim sourcePane As String = CType(sender, ImageButton).Attributes("sourcepane")
Dim targetPane As String = CType(sender, ImageButton).Attributes("targetpane")
Dim sourceBox As ListBox = CType(Page.FindControl(sourcePane), ListBox)
Dim targetBox As ListBox = CType(Page.FindControl(targetPane), ListBox)

end sub

why i can not find my control with the "Page.FindControl" method?
the string name is correcte, the control exist on my page. I still have that error "object reference not set to an instance of..object"

what do i do wrong here !!

thanxYou haven't provided enough information for us to answer your question. Whether Page.FindControl will actually find a control depends on where the control is on your page.

To see what I mean - and hopefully to allow you to solve your own problem - please read:
In Search Of Controls

As an aside, you should always test whether an object has a value before you start working with it.

For example, you should do something like:

' Find the target pane
Dim FoundTargetPane As Control
FoundTargetPane = Page.FindControl( targetPane )

' If found, cast the target pane to a ListBox control
Dim targetBox As Listbox
If Not FoundTargetPane Is Nothing Then
targetBox = CType( FoundTargetPane, ListBox )
End If

' Work with the targetBox
If Not targetBox Is Nothing Then
' we can work with targetBox, knowing that's we've found it
End If

Thursday, March 22, 2012

Object reference not set to an instance of an object.

I keep receiving "Object reference not set to an instance of an object" error when trying to run this code:

Private Sub cmdok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdok.Click
Dim rs1 As SqlClient.SqlDataAdapter
Dim ds As DataSet
Dim xnav As String
Dim xfreq As String
Dim xsn As String
Dim xfw As String
Dim xdte As Date
Dim xrecvd As String
Dim xrels As String
Dim xremarks As String
Dim xcmpy As String
Dim xrver As String
Dim xplate As String
Dim xdr As String
Dim xassign As Date
Dim msg As String

If TXTsn.Text <> "" Then

Dim conPubs As New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
rs1 = New SqlClient.SqlDataAdapter("SELECT * FROM tbl_Inbox WHERE imd_sn = '" & TXTsn.Text & "'", conPubs)

ds = New DataSet
rs1.Fill(ds, "imd_nav")

If ds.Tables(0).Rows.Count <> 0 Then
xrels = ds.Tables(0).Rows(0)("imd_rels").ToString()
xrecvd = ds.Tables(0).Rows(0)("imd_recvd").ToString()

Functions.InsertImdAssign(TXTnav.Text, TXTfreq.Text, TXTsn.Text, TXTfw.Text, TXTdate.Text, xrecvd, xrels, TXTremarks.Text, TXTcmpy.Text, TXTrver.Text, TXTplate.Text, TXTdr.Text, TXTassign.Text)

conPubs.Close()
End If

End If
End Sub

**************

Public Function InsertImdAssign(ByVal xnav As String, ByVal xfreq As String, ByVal xsn As String, ByVal xfw As String, ByVal xdte As Date, ByVal xrecvd As String, ByVal xrels As String, ByVal xremarks As String, ByVal xcmpy As String, ByVal xrver As String, ByVal xplate As String, ByVal xdr As String, ByVal xassign As Date) As SqlDataReader
' Create Instance of Connection and Command Object
Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
Dim MyCommand As SqlCommand
Dim strCommand As String

strCommand = "INSERT INTO tbl_Inbox ([imd_nav],[imd_freq],[imd_sn],[imd_fw],[imd_date],[imd_recvd],[imd_rels],[imd_remarks],[imd_cmpy],[imd_rver],[imd_plate],[imd_dr],[imd_drdte])"
strCommand = strCommand + "VALUES ('" & UCase(xnav) & "','" & UCase(xfreq) & "','" & xsn & "','" & xfw & "','" & xdte & "','" & xrecvd & "','" & xrels & "','" & xremarks & "','" & xcmpy & "','" & xrver & "','" & xplate & "','" & xdr & "','" & xassign & "')"
MyCommand = New SqlCommand(strCommand, myConnection)
' Execute the command
myConnection.Open()
Dim result As SqlDataReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection)

Return result
result.Close()
myConnection.Close()
End Function

What am I doing wrong?On what line do you get the error?
You never open conPubs.

Dim conPubs As New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
rs1 = New SqlClient.SqlDataAdapter("SELECT * FROM tbl_Inbox WHERE imd_sn = '" & TXTsn.Text & "'", conPubs)

conPubs.Open()

ds = New DataSet
rs1.Fill(ds, "imd_nav")
error occurred on this line:

Functions.InsertImdAssign(TXTnav.Text, TXTfreq.Text, TXTsn.Text, TXTfw.Text, TXTdate.Text, xrecvd, xrels, TXTremarks.Text, TXTcmpy.Text, TXTrver.Text, TXTplate.Text, TXTdr.Text, TXTassign.Text)
all,

still having this problem.. error occured at functions..

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

kindly help
Set a breakpoint on this line (click the line, press F9 so a red dot appears beside the line, then press F5 to run the project):
Functions.InsertImdAssign(TXTnav.Text, TXTfreq.Text, TXTsn.Text, TXTfw.Text, TXTdate.Text, _
xrecvd, xrels, TXTremarks.Text, TXTcmpy.Text, TXTrver.Text, TXTplate.Text, TXTdr.Text, TXTassign.Text)
When this line is hit, and turns yellow, hover your mouse over each object - 'Functions', 'TXTfw' etc. One of these should give you an intellisense popup with a value of "nothing". If this is a textbox, make sure yuo've typed the name correctly, if this is another object like the "Functions" one, make sure it is instanciated and ready for use...
Don't you have to use 'Shared' if you want to call a function without instantiating first? I'm not sure, but give it a try.
Copy paste the entire error page for us please, so we can read the stack trace and the few lines of code it is highlighting.

Thanks.
A) It's ASP.Net, break points don't work the same way
B) I'm assuming the function is in the same class as the sub (or atleast an object is created somewhere for it). If it wasn't, they'd receive an error in design time and not be able to build it.
C) Your command is an INSERT, not a SELECT. A sqlDataReader won't be able to do anything with it. You need to use the ExecuteNonQuery member of MyCommand.
I don't see a class 'Functions'. Also, where do you create a new instance of it?
Copy paste the entire error page for us please, so we can read the stack trace and the few lines of code it is highlighting.

Thanks.

FUNCTIONS ERROR!!!

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
CMSnet.imd_outbox.cmdok_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\CMSnet\imd_outbox.aspx.vb:116
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain() +1277

Object reference not set to an instance of an object.

Can somebody pllease tell me what is the error on this code. I got the error
on the executereader line.

Public Function verificarcedula(ByVal cedula As String) As Boolean

Dim mycmd As SqlCommand = New SqlCommand("select * from maestro where
cod_empl=@dotnet.itags.org.cedula", SqlConnection1)

mycmd.Parameters.Add("@dotnet.itags.org.cedula", cedula)

Dim myreader As SqlDataReader

Try

SqlConnection1.Open()

myreader = mycmd.ExecuteReader

If (myreader.Read) Then

Return True

Else

Return False

End If

Catch ex As Exception

Throw ex

Finally

myreader.Close()

SqlConnection1.Close()

End Try

End Function

--
LUIS ESTEBAN VALENCIA
MICROSOFT DCE 2.
MIEMBRO ACTIVO DE ALIANZADEVWhich line ?

You have an object that is not created (ie. Nothing).

--

"Luis Esteban Valencia" <luisvalen@.haceb.com> a crit dans le message de
news:eECvzGZxEHA.1988@.TK2MSFTNGP12.phx.gbl...
> Can somebody pllease tell me what is the error on this code. I got the
error
> on the executereader line.
> Public Function verificarcedula(ByVal cedula As String) As Boolean
> Dim mycmd As SqlCommand = New SqlCommand("select * from maestro where
> cod_empl=@.cedula", SqlConnection1)
> mycmd.Parameters.Add("@.cedula", cedula)
> Dim myreader As SqlDataReader
> Try
> SqlConnection1.Open()
> myreader = mycmd.ExecuteReader
> If (myreader.Read) Then
> Return True
> Else
> Return False
> End If
> Catch ex As Exception
> Throw ex
> Finally
> myreader.Close()
> SqlConnection1.Close()
> End Try
> End Function
>
> --
> LUIS ESTEBAN VALENCIA
> MICROSOFT DCE 2.
> MIEMBRO ACTIVO DE ALIANZADEV
This line.

myreader = mycmd.ExecuteReader

--
LUIS ESTEBAN VALENCIA
MICROSOFT DCE 2.
MIEMBRO ACTIVO DE ALIANZADEV
"Patrice" <nobody@.nowhere.com> escribi en el mensaje
news:uS2ArOZxEHA.3096@.TK2MSFTNGP14.phx.gbl...
> Which line ?
> You have an object that is not created (ie. Nothing).
> --
> "Luis Esteban Valencia" <luisvalen@.haceb.com> a crit dans le message de
> news:eECvzGZxEHA.1988@.TK2MSFTNGP12.phx.gbl...
> > Can somebody pllease tell me what is the error on this code. I got the
> error
> > on the executereader line.
> > Public Function verificarcedula(ByVal cedula As String) As Boolean
> > Dim mycmd As SqlCommand = New SqlCommand("select * from maestro where
> > cod_empl=@.cedula", SqlConnection1)
> > mycmd.Parameters.Add("@.cedula", cedula)
> > Dim myreader As SqlDataReader
> > Try
> > SqlConnection1.Open()
> > myreader = mycmd.ExecuteReader
> > If (myreader.Read) Then
> > Return True
> > Else
> > Return False
> > End If
> > Catch ex As Exception
> > Throw ex
> > Finally
> > myreader.Close()
> > SqlConnection1.Close()
> > End Try
> > End Function
> > --
> > LUIS ESTEBAN VALENCIA
> > MICROSOFT DCE 2.
> > MIEMBRO ACTIVO DE ALIANZADEV
Try creating the dataReader object with the new keyword
Dim myreader As New SqlDataReader
+++++++++++++++++++++++++++++++

"Luis Esteban Valencia" wrote:

> Can somebody pllease tell me what is the error on this code. I got the error
> on the executereader line.
> Public Function verificarcedula(ByVal cedula As String) As Boolean
> Dim mycmd As SqlCommand = New SqlCommand("select * from maestro where
> cod_empl=@.cedula", SqlConnection1)
> mycmd.Parameters.Add("@.cedula", cedula)
> Dim myreader As SqlDataReader
> Try
> SqlConnection1.Open()
> myreader = mycmd.ExecuteReader
> If (myreader.Read) Then
> Return True
> Else
> Return False
> End If
> Catch ex As Exception
> Throw ex
> Finally
> myreader.Close()
> SqlConnection1.Close()
> End Try
> End Function
>
> --
> LUIS ESTEBAN VALENCIA
> MICROSOFT DCE 2.
> MIEMBRO ACTIVO DE ALIANZADEV
>
Well, the reader object reference is only being assigned to in this
line of code, so it should not be a problem (and the ExecuteReader
method will take care of creating the object in any case). If anything
it is the SqlCommand object that we need to be suspicious of, but it
looks as if it was New'ed appropriately.

Have you stepped through the code with the debugger Luis?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 8 Nov 2004 09:39:02 -0800, "Tampa .NET Koder"
<TampaNETKoder@.discussions.microsoft.com> wrote:

>Try creating the dataReader object with the new keyword
>Dim myreader As New SqlDataReader
>+++++++++++++++++++++++++++++++
>
>"Luis Esteban Valencia" wrote:
>> Can somebody pllease tell me what is the error on this code. I got the error
>> on the executereader line.
>>
>> Public Function verificarcedula(ByVal cedula As String) As Boolean
>>
>> Dim mycmd As SqlCommand = New SqlCommand("select * from maestro where
>> cod_empl=@.cedula", SqlConnection1)
>>
>> mycmd.Parameters.Add("@.cedula", cedula)
>>
>> Dim myreader As SqlDataReader
>>
>> Try
>>
>> SqlConnection1.Open()
>>
>> myreader = mycmd.ExecuteReader
>>
>> If (myreader.Read) Then
>>
>> Return True
>>
>> Else
>>
>> Return False
>>
>> End If
>>
>> Catch ex As Exception
>>
>> Throw ex
>>
>> Finally
>>
>> myreader.Close()
>>
>> SqlConnection1.Close()
>>
>> End Try
>>
>> End Function
>>
>>
>> --
>> LUIS ESTEBAN VALENCIA
>> MICROSOFT DCE 2.
>> MIEMBRO ACTIVO DE ALIANZADEV
>>
>>
>
Did you checked they are not Nothing ? I would check also SqlConnection1.

Patrice

--

"Luis Esteban Valencia" <luisvalen@.haceb.com> a crit dans le message de
news:e1qoHUZxEHA.2600@.TK2MSFTNGP09.phx.gbl...
> This line.
> myreader = mycmd.ExecuteReader
>
> --
> LUIS ESTEBAN VALENCIA
> MICROSOFT DCE 2.
> MIEMBRO ACTIVO DE ALIANZADEV
> "Patrice" <nobody@.nowhere.com> escribi en el mensaje
> news:uS2ArOZxEHA.3096@.TK2MSFTNGP14.phx.gbl...
> > Which line ?
> > You have an object that is not created (ie. Nothing).
> > --
> > "Luis Esteban Valencia" <luisvalen@.haceb.com> a crit dans le message de
> > news:eECvzGZxEHA.1988@.TK2MSFTNGP12.phx.gbl...
> > > Can somebody pllease tell me what is the error on this code. I got the
> > error
> > > on the executereader line.
> > > > Public Function verificarcedula(ByVal cedula As String) As Boolean
> > > > Dim mycmd As SqlCommand = New SqlCommand("select * from maestro where
> > > cod_empl=@.cedula", SqlConnection1)
> > > > mycmd.Parameters.Add("@.cedula", cedula)
> > > > Dim myreader As SqlDataReader
> > > > Try
> > > > SqlConnection1.Open()
> > > > myreader = mycmd.ExecuteReader
> > > > If (myreader.Read) Then
> > > > Return True
> > > > Else
> > > > Return False
> > > > End If
> > > > Catch ex As Exception
> > > > Throw ex
> > > > Finally
> > > > myreader.Close()
> > > > SqlConnection1.Close()
> > > > End Try
> > > > End Function
> > > > > --
> > > LUIS ESTEBAN VALENCIA
> > > MICROSOFT DCE 2.
> > > MIEMBRO ACTIVO DE ALIANZADEV
> >

Friday, March 16, 2012

Object reference not set to an instance of an object.

Can somebody pllease tell me what is the error on this code. I got the error
on the executereader line.
Public Function verificarcedula(ByVal cedula As String) As Boolean
Dim mycmd As SqlCommand = New SqlCommand("select * from maestro where
cod_empl=@dotnet.itags.org.cedula", SqlConnection1)
mycmd.Parameters.Add("@dotnet.itags.org.cedula", cedula)
Dim myreader As SqlDataReader
Try
SqlConnection1.Open()
myreader = mycmd.ExecuteReader
If (myreader.Read) Then
Return True
Else
Return False
End If
Catch ex As Exception
Throw ex
Finally
myreader.Close()
SqlConnection1.Close()
End Try
End Function
LUIS ESTEBAN VALENCIA
MICROSOFT DCE 2.
MIEMBRO ACTIVO DE ALIANZADEVWhich line ?
You have an object that is not created (ie. Nothing).
"Luis Esteban Valencia" <luisvalen@.haceb.com> a crit dans le message de
news:eECvzGZxEHA.1988@.TK2MSFTNGP12.phx.gbl...
> Can somebody pllease tell me what is the error on this code. I got the
error
> on the executereader line.
> Public Function verificarcedula(ByVal cedula As String) As Boolean
> Dim mycmd As SqlCommand = New SqlCommand("select * from maestro where
> cod_empl=@.cedula", SqlConnection1)
> mycmd.Parameters.Add("@.cedula", cedula)
> Dim myreader As SqlDataReader
> Try
> SqlConnection1.Open()
> myreader = mycmd.ExecuteReader
> If (myreader.Read) Then
> Return True
> Else
> Return False
> End If
> Catch ex As Exception
> Throw ex
> Finally
> myreader.Close()
> SqlConnection1.Close()
> End Try
> End Function
>
> --
> LUIS ESTEBAN VALENCIA
> MICROSOFT DCE 2.
> MIEMBRO ACTIVO DE ALIANZADEV
>
This line.
myreader = mycmd.ExecuteReader
LUIS ESTEBAN VALENCIA
MICROSOFT DCE 2.
MIEMBRO ACTIVO DE ALIANZADEV
"Patrice" <nobody@.nowhere.com> escribi en el mensaje
news:uS2ArOZxEHA.3096@.TK2MSFTNGP14.phx.gbl...
> Which line ?
> You have an object that is not created (ie. Nothing).
> --
> "Luis Esteban Valencia" <luisvalen@.haceb.com> a crit dans le message de
> news:eECvzGZxEHA.1988@.TK2MSFTNGP12.phx.gbl...
> error
>
Try creating the dataReader object with the new keyword
Dim myreader As New SqlDataReader
+++++++++++++++++++++++++++++++
"Luis Esteban Valencia" wrote:

> Can somebody pllease tell me what is the error on this code. I got the err
or
> on the executereader line.
> Public Function verificarcedula(ByVal cedula As String) As Boolean
> Dim mycmd As SqlCommand = New SqlCommand("select * from maestro where
> cod_empl=@.cedula", SqlConnection1)
> mycmd.Parameters.Add("@.cedula", cedula)
> Dim myreader As SqlDataReader
> Try
> SqlConnection1.Open()
> myreader = mycmd.ExecuteReader
> If (myreader.Read) Then
> Return True
> Else
> Return False
> End If
> Catch ex As Exception
> Throw ex
> Finally
> myreader.Close()
> SqlConnection1.Close()
> End Try
> End Function
>
> --
> LUIS ESTEBAN VALENCIA
> MICROSOFT DCE 2.
> MIEMBRO ACTIVO DE ALIANZADEV
>
>
Well, the reader object reference is only being assigned to in this
line of code, so it should not be a problem (and the ExecuteReader
method will take care of creating the object in any case). If anything
it is the SqlCommand object that we need to be suspicious of, but it
looks as if it was New'ed appropriately.
Have you stepped through the code with the debugger Luis?
Scott
http://www.OdeToCode.com/blogs/scott/
On Mon, 8 Nov 2004 09:39:02 -0800, "Tampa .NET Koder"
<TampaNETKoder@.discussions.microsoft.com> wrote:
>Try creating the dataReader object with the new keyword
>Dim myreader As New SqlDataReader
>+++++++++++++++++++++++++++++++
>
>"Luis Esteban Valencia" wrote:
>
Did you checked they are not Nothing ? I would check also SqlConnection1.
Patrice
"Luis Esteban Valencia" <luisvalen@.haceb.com> a crit dans le message de
news:e1qoHUZxEHA.2600@.TK2MSFTNGP09.phx.gbl...
> This line.
> myreader = mycmd.ExecuteReader
>
> --
> LUIS ESTEBAN VALENCIA
> MICROSOFT DCE 2.
> MIEMBRO ACTIVO DE ALIANZADEV
> "Patrice" <nobody@.nowhere.com> escribi en el mensaje
> news:uS2ArOZxEHA.3096@.TK2MSFTNGP14.phx.gbl...
>