PDA

Click to See Complete Forum and Search --> : Variable help


stx531
Dec 5th, 2006, 04:28 PM
I have an array of string variables. How can I take each part of the array and put the value into another variable that is a different data type?

DigiRev
Dec 5th, 2006, 07:43 PM
What data type are you trying to convert the strings to?

Depending on that, you might be able to use CopyMemory() API function, or just a for/next loop through the array and put the current array element into the other one.

opus
Dec 6th, 2006, 01:37 AM
If I understood correctly you have StringArray like

strArr(0)="1234"
strArr(1)="text"
strArr(2)="1.222"
and so on.

You should know for each index whiich data-type you expect, using this information you can go like that:

InterVar=Cint(strArr(0))
StrVar=CStr(strArr(1)) 'of course this typechanging isn't neccessary
SngVar=CSng(strArr(2))

I hope that helps.

stx531
Dec 6th, 2006, 09:39 PM
How much information can a regular string hold? I have a huge packet coming from a server (a maximum of 432 bytes). Can this all fit into one string variable so that I can split the information after it is sent from the server?

DigiRev
Dec 6th, 2006, 09:45 PM
How much information can a regular string hold? I have a huge packet coming from a server (a maximum of 432 bytes). Can this all fit into one string variable so that I can split the information after it is sent from the server?

A string can easily hold 432 bytes.

It can hold up to 2GB from what I've learned.

stx531
Dec 6th, 2006, 10:12 PM
Private Sub sckArch_DataArrival(ByVal bytesTotal As Long)
strWeatherData = ""
ArchDate = txtDate.Text
lstStatus.AddItem "Data received"
Dim strData As String
sckArch.GetData strData, vbString, bytesTotal
'Remove new-line characters.
strData = Replace$(strData, vbCrLf, "")
Debug.Print strData
'Check for welcome message.
If InStr(1, strData, GREETING, vbTextCompare) > 0 Then
' lstStatus.AddItem "Received greeting message."
'MsgBox strData
'Send username (user@host) with a new line as well as the "c" character.
' lstStatus.AddItem "Sending username and ""c"""
sckArch.SendData USER & vbCrLf & ArchDate & vbCrLf
'Check for ACK.
ElseIf InStr(1, strData, "ACK", vbTextCompare) > 0 Then
' lstStatus.AddItem "Received ACK."
'Check for weather data
'Find the ":" character in the time
ElseIf InStr(1, strData, ":", vbTextCompare) < 5 Then
strWeatherData = strData
' lstStatus.AddItem "Weather data received."
'Unknown data.
Else
'lstStatus.AddItem "Unknown data received."
End If
'The weather data is seperated (delimited) by a space character.
'Split each item into an array.
strArch() = Split(strWeatherData, " ")





here is my code. I need to get the 8th array value in the strArch array, and the 10th from last array value in the strArch array. How can I do that?

opus
Dec 7th, 2006, 12:35 AM
First of all, please use the vbcode-tags and indenting, that way your code is read more easily.



It seems to me that your messages have a standard syntax.
Problably they start with a keyword (Greeting, Ack ,....), using this keyword you can decide what message it is. What I'm missing is the keyword for your weatherdata-message. If there isn't one, you should consider using a keyword in this one also, because winsock may give you two messages in one _DataArrival event. In such a case you might miss a message. Also consider to use a standard ending for each message, that makes it easy to seperate packed messages.

Now to your problem to get specific array values


strArch() = Split(strWeatherData, " ")

If you want to get the 8th and the 10th from the last array value use:

Value8th=CInt(strWeatherData(7)) 'the first one will be index 0!, I assumed an Integer here!
Value10thfromLast=CSng(strWeatherData(UBound(strWeatherData)-10))'I assumed a Single here!

ganeshmoorthy
Dec 7th, 2006, 01:27 AM
I need to get the 8th array value in the strArch array, and the 10th from last array value in the strArch array. How can I do that?MsgBox "8th Value is : " & strArch(7)
MsgBox "10th Value from the Last Array index is : " & strArch(UBound(strArch) - 10)
'But you need to check if you have that many array values are there in the array by validating like this..
If UBound(strArch) > 10 Then
'It has more than 10 elements...
End If

stx531
Dec 7th, 2006, 07:27 AM
When I put a number into the subscript of the array I always get a Subscript out of range error. Why would it do that?

stx531
Dec 7th, 2006, 07:37 AM
Also, how can I use a keyword for my weatherdata. I think that winsock is giving me multiple messages when I try ro get that data.

opus
Dec 7th, 2006, 08:29 AM
When I put a number into the subscript of the array I always get a Subscript out of range error. Why would it do that?
In that case your array doesn't have enough elements, like you call strWeatherData(7) and get hat Errr, your strWeatherData does have lees than 8 elements!

opus
Dec 7th, 2006, 08:34 AM
Also, how can I use a keyword for my weatherdata. I think that winsock is giving me multiple messages when I try ro get that data.
Here is what I do:
First each message has a header (i.e. a keyword at the beginning) and a typical ending (like ";").
In the Reading part I seperate the header and the remainder of the message. Using the header I process the remainder of the message, for each keyword I expect either nothing or some data, so I read everything until the ending sign.
If there is more info after the endign sign I consider it as a new message and guess what.... I use the Reading part of my code again.

ganeshmoorthy
Dec 8th, 2006, 12:29 AM
When I put a number into the subscript of the array I always get a Subscript out of range error. Why would it do that?'But you need to check if you have that many array values are there in the array by validating like this..
If UBound(strArch) > 10 Then
'It has more than 10 elements...
End If
I think that winsock is giving me multiple messages when I try ro get that data.check which delimiter is used to separate the fields in the received data. And may be using Split() will help you, if you have any delimiter in your data.