Parse-ing Strings

Discussion in 'ASP .Net' started by Martin Eyles, Jan 12, 2006.

  1. Martin Eyles

    Martin Eyles Guest

    Hi,

    I am getting a string passed back from a web form in the format
    name1:variable1;name2:variable2...

    I would like to split this up into several variable, such that the variable
    names are name1, name2 etc. (I actually know what these names are) and the
    values of the variables are variable1, variable2 etc.

    I tried using split to split the string into an array at the semicolons, and
    was then thinking I would split the value out with a second split command,
    but this seems a bit of a complex way round the problem. Also I am having
    some problems with the implementation.

    My two questions are:
    1. What is wrong with the code as it stands
    2. What better way is there of acheiving the desired effect

    My VB.NET code behind is something like:
    'Declare Variables
    Dim DataString As String
    Dim DataArray(9) As String
    'Get Data from http
    DataString = Request("DataString")
    'Parse Data
    DataArray = TimeEntryData.Split(CChar(";")) 'This is the line that gives an
    error
    name1=TimeEntryDataArray(1)
    Response.Write(name1)

    And the Error I get is:
    Object reference not set to an instance of an object.

    Thanks,
    Martin
     
    Martin Eyles, Jan 12, 2006
    #1
    1. Advertisements

  2. In your code, what is TimeEntryData?
    You seem to want to split 'DataString'
    You should have DataArray = DataString.Split( CChar(";"))
     
    Phillip N Rounds, Jan 12, 2006
    #2
    1. Advertisements

  3. Martin Eyles

    Martin Eyles Guest

    Sorry, didn't finish renaming the variables for general-ness. The code
    should read:
    'Declare Variables
    Dim DataString As String
    Dim DataArray(9) As String
    'Get Data from http
    DataString = Request("DataString")
    'Parse Data
    DataArray = DataString.Split(CChar(";")) 'This is the line that gives an
    error
    name1=DataArray(1)
    Response.Write(name1)
     
    Martin Eyles, Jan 12, 2006
    #3
  4. Martin,

    I think using the split function is a fine way to get your values. I'm not
    aware of a better way. But perhaps someone else may chime in here on that.

    That said here's a small example:

    Dim TestData As String = "9;8;7;6;5;4;3;2;1;0"

    Dim Data() As String = TestData.Split(CChar(";"))

    Dim Value As String

    For Each Test As String In Data

    Value = Test

    Next



    Note that I'm not pre-defining the array.


    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
     
    S. Justin Gengo [MCP], Jan 12, 2006
    #4
  5. Martin Eyles

    Mythran Guest

    Hrm...for something like this, I would use regular expressions...

    Example:

    Dim input As String = "name1=value1;name2=value2;name3=value3;"
    Dim pattern As String = _
    "(?<Name>[^=;]*)=(?<Value>[^=;]*);"
    Dim matchCollection As MatchCollection = Regex.Matches(input, pattern)
    For Each match As Match In matchCollection
    Console.WriteLine( _
    "Name: {0} - Value: {1}", _
    match.Groups("Name").Value, _
    match.Groups("Value").Value _
    )
    Next match

    HTH,
    Mythran
     
    Mythran, Jan 12, 2006
    #5
  6. Martin Eyles

    intrader Guest

    Looks like you are transmitting a JavaScript object literal. This protocol
    has been named JSON. You may find in open source forums code to split this
    effectively. Note that is similar to splitting a name/value string (as
    Form or QueryString have); so code in CPAN for doing this can also be
    found there.
    Now, since you already have some sort of Javascript object literal, why
    don't you use JSscript to decode this? First enclose the hole thin in {},
    then change your ':' to ":\'" and your ',' to, you get the idea. Then
    instantiate the object in JScript and there you have it all nicely
    separated.
     
    intrader, Jan 12, 2006
    #6
  7. Martin Eyles

    Martin Eyles Guest

    Even not pre-defining the array, I still get the same error. Any idea why?

    Thanks,
    Martin
     
    Martin Eyles, Jan 16, 2006
    #7
  8. Martin Eyles

    Martin Eyles Guest

    I think you have mis-understood what I am trying to do.

    What is happening is that the user fills in a form in a pop-up window.
    Javascript on the client side makes the string, and passes it from a pop-up
    to a hidden input in the main window's form. The main window then closes the
    pop-up and performs a post-back. Finally, the VB.net code on the server
    decodes the string, and uses it as input to an SQL query, returning the
    results with the posted page.

    I think that the other answers are already covering what I am looking for on
    this, but thanks for the info anyway,

    Martin
     
    Martin Eyles, Jan 16, 2006
    #8
  9. Martin Eyles

    Martin Eyles Guest

    Found a potential source of the problem - the string is empty. But I am
    confused, it should have all the data. I have tried both

    Data = Request("Data")
    and
    Data = Request("Menu1_Data")

    but both of these leave Data entirely empty. (note, Data is the name in the
    menu.ascx file, but Menu1_Data is the name that appears on the page itself).

    Thanks,
    Martin
     
    Martin Eyles, Jan 16, 2006
    #9
  10. Martin,

    It wasn't until I read your reply to intrader that I realized I have an
    example you may be able to use.

    In the code library on my website:
    http://www.aboutfortunate.com?page=codelibrary do a search for "close pop-up
    and refresh" I have some example code for closing a pop-up window and
    refreshing the main one and also calling code. It may help you get those
    values back. I think I'm using a different technique than you, but it may
    work for you.

    Using the technique you'll find you can actually run your code within the
    pop-up's page and then close it.

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
     
    S. Justin Gengo [MCP], Jan 18, 2006
    #10
  11. Martin Eyles

    Martin Eyles Guest

    Thanks for the example. I don't think it does quite what I want (send data
    back to the server), but that is OK, as I have solved the problem. I can in
    fact use the fact that the form is server side, so rather than having to use
    request, I can just use the variable name, and the language recognises it as
    already declared. Thanks Anyway.

    Martin
     
    Martin Eyles, Jan 19, 2006
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.