Results 1 to 13 of 13

Thread: Variable help

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    12

    Variable help

    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?

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Variable help

    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.

  3. #3
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Variable help

    If I understood correctly you have StringArray like
    VB Code:
    1. strArr(0)="1234"
    2. strArr(1)="text"
    3. strArr(2)="1.222"
    4.  and so on.
    You should know for each index whiich data-type you expect, using this information you can go like that:
    VB Code:
    1. InterVar=Cint(strArr(0))
    2. StrVar=CStr(strArr(1)) 'of course this typechanging isn't neccessary
    3. SngVar=CSng(strArr(2))
    I hope that helps.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    12

    Re: Variable help

    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?

  5. #5
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Variable help

    Quote Originally Posted by stx531
    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.

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    12

    Re: Variable help

    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?

  7. #7
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Variable help

    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

    VB Code:
    1. strArch() = Split(strWeatherData, " ")
    If you want to get the 8th and the 10th from the last array value use:
    VB Code:
    1. Value8th=CInt(strWeatherData(7)) 'the first one will be index 0!, I assumed an Integer here!
    2. Value10thfromLast=CSng(strWeatherData(UBound(strWeatherData)-10))'I assumed a Single here!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  8. #8
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Variable help

    Quote Originally Posted by stx531
    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?
    VB Code:
    1. MsgBox "8th Value is : " & strArch(7)
    2. MsgBox "10th Value from the Last Array index is : " & strArch(UBound(strArch) - 10)
    3. 'But you need to check if you have that many array values are there in the array by validating like this..
    4. If UBound(strArch) > 10 Then
    5.     'It has more than 10 elements...
    6. End If
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  9. #9

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    12

    Re: Variable help

    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?

  10. #10

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    12

    Re: Variable help

    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.

  11. #11
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Variable help

    Quote Originally Posted by stx531
    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!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  12. #12
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Variable help

    Quote Originally Posted by stx531
    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.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  13. #13
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Variable help

    Quote Originally Posted by stx531
    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?
    Quote Originally Posted by ganeshmoorthy
    VB Code:
    1. 'But you need to check if you have that many array values are there in the array by validating like this..
    2. If UBound(strArch) > 10 Then
    3.     'It has more than 10 elements...
    4. End If
    Quote Originally Posted by stx531
    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.
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width