Results 1 to 5 of 5

Thread: Passing multiple String.Format()

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    Passing multiple String.Format()

    I was fed up of having a duplicate method side by side so merged them into one. Both sent a request to another class.

    vb Code:
    1. Private _messageFormats() As String = { _
    2.              String.Format(RegistrySettings.PMessageData, Identity, Topic, Body, RegistrySettings.ServerCookieToken), _
    3.              String.Format(RegistrySettings.ThreadData, Topic, Body, RegistrySettings.ServerCookieToken)}

    The issue comes because Identity, topic, body are all member variables declared at run time. and when sending the string.format to another method, Them three ate null since they are already set

    any solutions?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Passing multiple String.Format()

    You need to pass those values to wherever that code is, as method parameters.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    Re: Passing multiple String.Format()

    Thats the trouble i was facing john. I had two identical methods that passed the same string except one contained 4 elements and the other 3 elements.

    So i was wondering if there was a way to hard code both string.format lines and pass when needed. Do you have any alternative suggestions?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Passing multiple String.Format()

    I'm afraid that I don't really understand what you're asking for. Perhaps you should show us exactly what you've got and explain exactly what's wrong with it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    Re: Passing multiple String.Format()

    Ok sorry.

    Right i am using httpwebrequest to login to my server. This class is called ConnectionManager.

    Along side the login method that class contains another method that creates a Httprequest for Two different tasks depending on what information is passed.

    This information is sent from userControl1 for arguments sake. Before i use to call this class i would use select case to determine True on Two radio buttons.

    Rad 1 - passes a string using string format containing 4 elements

    Rad 2 - passes a string using string format containing 3 elements

    I would call from usercontrol1 Either method1 or method2 depending on what rad button was checked then both done identical settings other then the string.format part. Then passed the information on to the class

    Now i hate duplicate code so wanted to try merge them into one..

    SF1 :
    vb Code:
    1. String.Format(RegistrySettings.PMessageData, Identity, Topic, Body, RegistrySettings.ServerCookieToken)

    SF2 :
    vb Code:
    1. String.Format(RegistrySettings.ThreadData, Topic, Body, RegistrySettings.ServerCookieToken)


    My first post i tried to save them as member variables but obviously they would return null.

    I just want a more functional way to pass these two string.formats rather then having duplicate code.

    Merge these two
    vb Code:
    1. Private Sub HandleComposeMessageConnection(ByVal topic As String, ByVal body As String)
    2.         _connectionManager.HandleRequestedUrl = ComposeMessageUrl
    3.         _connectionManager.HandleRequestedData = String.Format(RegistrySettings.PMessageData, _
    4.                                                                txtPostIdentityNumber.Text, _
    5.                                                                topic, _
    6.                                                                body, _
    7.                                                                RegistrySettings.ServerCookieToken)
    8.         _connectionManager.InitiateMessage()
    9.     End Sub
    10.  
    11.     Private Sub HandleComposeThreadConnection(ByVal topic As String, ByVal body As String)
    12.         _connectionManager.HandleRequestedUrl = ComposeThreadUrl & txtPostIdentityNumber.Text
    13.         _connectionManager.HandleRequestedData = String.Format(RegistrySettings.ThreadData, _
    14.                                                                topic, _
    15.                                                                body, _
    16.                                                                RegistrySettings.ServerCookieToken)
    17.         _connectionManager.InitiateMessage()
    18.     End Sub

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