Click here to Skip to main content
15,885,979 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
could you help with dataarrival event on vb 2008, i am trying to develop a chat application but it gives me warning each time i have something like
dim recievedData as string
sckClient.GetData(recievedata)

please i need complete this fast.
also working on a chat the speaks written messages and it gives error in my declaration
dim listener As New TCPlistener

Please, i have been on these for weeks now. Thanks in anticipation


Posted by OP as an answer:
"this is the warning that comes up
variable 'recievedata' is passed by reference before it has been assigned a value. A null reference exception could result at runtime
. thanks"
Posted
Updated 6-May-11 2:20am
v2
Comments
OriginalGriff 6-May-11 3:03am    
Please use the "Improve question" widget and explain what warning you are getting, and what error message for the second statement.
Oh, and take out the urgency - it is not important to us, and may slow you getting a response.
Odiagbe justus 6-May-11 7:43am    
this is the warning that comes up
variable 'recievedata' is passed by reference before it has been assigned a value. A null reference exception could result at runtime
. thanks

1 solution

It complains because you are doing something it thinks is silly! And, in truth it is.
dim recievedData as string
sckClient.GetData(recievedata)
You declare a variable "recievedData" and then call a function with it as a parameter.

Because you do not assign a value at any point between the declaration and the use, the compiler knows that the value entering the function will be null - it will give it that when you declare it.
Because the method "GetData" does not use a ref paramater, the value in "recievedData" can only go into the function - nothing that happens to it in the function can be returned, so "recievedData" will always wtill be null after the function call.

What you need to do is modify the GetData functionso that either it returns a string, which you assign to recievedData:
dim recievedData as string
recievedData = sckClient.GetData()
or make the parameter ByRef if you are going to assign it in the function:
Private Sub GetData(ByRef str As String)
...
str = "hello"
End Sub
 
Share this answer
 
Comments
Odiagbe justus 9-May-11 4:53am    
Thanks.
Odiagbe justus 9-May-11 4:56am    
How do I write a code to receive messages sent from a remote user of my application. Is it that i am going to write a module or what cos the tutorial am using are not using modules in them. Please help. The ref you said i could use, i don't know how to go about it. Am a newbie. Thanks in anticipation
OriginalGriff 9-May-11 5:03am    
Add the word "ByRef" to your function declaration as in the last example above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900