This question already has an answer here:
String to Array with Classic ASP
(1 answer)
Closed 6 years ago.
EDIT: I now realise after the help from those who replied that my question was about whether Request.Form Data is a string in the same way that a$="FooBar" is a string and the Array command in Classic ASP.
I'm trying to make an Array from data submitted in a Form.
The form fields are dynamically created and have the same name "subj".
The Response.Write(Request.Form("subj")) produces:
"Welcome and Introduction, Talk1, Talk2 ,Interactive review of the conference"
When I check the TypeName or VarType Request.Form("subj") is a string. Then I code:
subjs = """" & Replace(Request.Form("subj"), ", ", """,""") & """"
subjects = Array(subjs)
With the intention to give:
subjs = "Welcome and Introduction","Talk1","Talk2","Interactive review of the conference"
subjects(0) = Welcome and Introduction
subject(1) = Talk1
subject(2) = Talk2
subject(3) = Interactive review of the conference
The problem is that what I actually get is:
subjs = "Welcome and Introduction","Talk1","Talk2","Interactive review of the conference"
subject(0) = "Welcome and Introduction","Talk1","Talk2","Interactive review of the conference"
For some reason the Array isn't correctly formed as there is no subject(1) subject(2) or subject(3).
If I physically copy and paste the output of subjs into my code, then Array works fine but I can't get the Array to work on Form Data.
I've tried using CStr and checked all of the quotation marks.
Why doesn't it work?
Thank you to those who took the trouble to reply. Whilst Split does work in fields without commas, SET var = Request.Form("subj") as per #Kul-Tigin, I think is the key but would be keen to hear other thoughts
Since the request collection values may contain commas, using a split can cause unexpected results.
Creating an array through the collection is more reliable.
Set subject = Request.Form("subj")
ReDim subjects(subject.Count - 1)
For i = 1 To subject.Count
subjects(i - 1) = subject(i)
Next
The Array function expects a comma-separated list: "words","stuff","foo", but what you get from a Request.Form is more like "words,stuff,foo".
Ultimately, though, it doesn't matter, because as you've noted in your comment, the appropriate function to use is Split.
Related
As #vadian suggested, I am learning Xcode from Paul Hudson's 100 days of SwiftUI to better understand the basics (even though I am still struggling with time to deliver working GitHub repository search - no it is not for the job I am working on right now).
I am doing a lesson on Arrays, so pretty basic stuff right now and I struggle to map the String from array on the sorted array. I don't get why, but I get the:
Ambiguous use of 'init'
error.
Here's the code:
let cities = ["London", "Tokyo", "Rome", "Budapest"]
print(cities.sorted())
let citiesSorted = cities.sorted()
let citiesSortedString = citiesSorted.map(String.init).joined(separator:", ")
And it's so really strange, since I pulled the similar thing out before in the same Playground but not with the sorted Array:
var schoolScoresString = schoolScores.map(String.init).joined(separator:", ")
print(schoolScoresString)
And yes I tried changing let to var. It didn't help.
cities is already of type [String], so you're passing a String to String.init, which obviously won't work. If you want to join an array of Strings into a single String, remove the map and just call joined on the array.
let citiesSortedString = citiesSorted.joined(separator:", ")
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 5 years ago.
Hello I have a code and blah blah blah...
The problem is I have a maskedtextbox(s) in a row (12 of them to be exact) and i want to give them a specific value from array. Is it possible to do it by loop?
I tried this:
This is the declaration:
Dim BiA11() As MaskedTextBox = New MaskedTextBox() {BiA11_1, BiA11_2,
BiA11_3, BiA11_4, BiA11_5, BiA11_6, BiA11_7, BiA11_8, BiA11_9, BiA11_10,
BiA11_11, BiA11_12}
And this is the code later in the program:
For index As Integer = 0 To 11
BiA11(index).Text = ""
Next
Yes I know now that i am pusshing nothing to the MaskedTextBox but this was only for my test. But the visual studio gives me this error:
System.NullReferenceException: Object reference not set to an instance of an object.
BiA11() – Nothing.
In the end I want to push to the textboxes strings from an array throgh loop (it will save a quantum of space because I have 132 of these textboxes.
Thank you for your time.
********************EDIT********************
So I found an answer
First of all in the declaration i changed the array a little bit:
Dim BiA11() As MaskedTextBox = New MaskedTextBox(11) {}
The declaration takes a place in the Public Class Form1
Then in the sub that handels the first events of appication in the first lines I filled the array with the references of the MaskedTextBox('s)
BiA11 = {BiA11_1, BiA11_2, BiA11_3, BiA11_4, BiA11_5, BiA11_6, BiA11_7, BiA11_8, BiA11_9, BiA11_10, BiA11_11, BiA11_12}
And then finaly when it comes to deleting all of the MaskedTextBox('s) i just puted this for loop inside and it worked for me.
For index As Integer = 0 To 11
BiA11(index).Text = ""
Next
No exceptions and the MaskedTextBox('s) are clear. Thank you for all your help. Hope that this will come in handy when someone will have similiar problem.
The issue is presumably that you have got that first code snippet at the class level. In that case, it will be executed before the constructor, in which case your controls haven't been created yet. What you need to do is declare the array variable at the class level:
Private BiA11 As MaskedTextBox()
and then create and populate the array inside the Load event handler of the form:
BiA11 = {BiA11_1, BiA11_2, BiA11_3, BiA11_4, BiA11_5, BiA11_6, BiA11_7, BiA11_8, BiA11_9, BiA11_10, BiA11_11, BiA11_12}
I have been working on this program for over a week with no breakthrough. The questions states as follows:
A disc file ‘REQUESTS.TXT’ contains airline flight data formatted
(STR6, STR3, STR3, INT).
Example:
AA1011SFxLAx34 (American Airlines 1010, SF to LA, 34 seats)
W0924DNVDFW101 (Western 0924, DNV to DFW, 101 seats)
Another file ‘AVAILABL.TXT’ contains an unspecified number of reservation request records formatted identically as described above except the Seats Available field is a Seats Requested field.
Guidelines:
Read reservation flights and process requests. If the request can be fullfilled (i.e.. it is in AVAILABL and REQUESTS) then print "Reservation Processed", otherwise print "Reservation Denied".
Print out flight data file before and after reservations are processed, ordered by flight ID in a four(4) column format.
Print an overall outcome report for all processed.(Present totals for the number of requests satisfied and denied)
I have tried a few different approaches.. I tried to split up the first STR6 by isalpha/isdigit and combine them to make the FlightID (AA + 1011). Proceeded to try to then split up the remaining characters between STR3 and STR3 via isalpha + for loop. And lastly, I tried to take the last 3+ digits for the # of seats during each for loop iteration and multiply the first digit by 100(for a 3-digit value) or 10(for a 2-digit value), adding it to a running total for availSeats(INT). This, at least I thought so, would produce a
AA+1011 = AA1011(STR6) // W+0924 = W0924(STR6)
SFx(STR3) // DNV(STR3)
LAx(STR3) // DFW(STR3)
(3*10)+(4*1) = 34(INT) // (1*100)+(0*10)+(1*1) = 101(INT)
All of this stored within a Struct Array.
i.e...
FlightData Flight; ............................................FlightData Flight;
Flight[0].flightID = AA1011; .........................Flight[1].flightID = W0924;
Flight[0].fromCity = SFx; ...............................Flight[1].fromCity = DNV;
Flight[0].toCity = LAx; ..................................Flight[1].toCity = DFW;
Flight[0].seatsAvail = 34; .............................Flight[1].seatsAvail = 101;
I am really at a loss right now and have no other way to progress other than searching up different techniques/methods to use to make this work. I am a beginner clearly and will continue to practice and progress in C, but if anyone could provide me with a push in the right direction on how one would execute this via .txt into a Struct would be amazing. Also, if anyone has another method they used to solve this problem I would love to analyze it. Thanks!
(This is my first post, I spent a lot of time formatting it to be clear on Stackoverflow, so If i messed up in areas some constructive critisism would be useful! This applies to my posting and my coding practices. Thanks again!)
EDIT: The question I am asking here is how to successfully take a string such as AA1011SFxLAx34 and turn it into a Structure like the above diagram. It must also work for the second string W0924DNVDFW101 which has only 1 Char in its ID. (rather than two in AA1011). Im not sure what else I am supposed to edit after reading the guidelines.
I consider this a home work question, so I answer according to
How do I ask and answer homework questions?
Find a tutorial on C, work through it.
Then take a HelloWorld, modify it in small steps to approach your goal in steps from working program to working program. This way you should at least get to being able to read text from a file and print it.
Then learn to store parts of what you print into basic variables.
Then learn about structures.
And so on.
This way you will get quite close to the solution.
If it is not completely what you need show the code you have here at that point and ask a specific question about the first problem explaining what you suspect the problem to be. Show code which has exactly that one problem and makes it visible and has not other warnings (using at least e.g. gcc -Wall mycode).
Fix with the help of commments/answers you receive, repeat.
On this question I solved the problem of querying Google Datastore to retrieve stuff by user (com.google.appengine.api.users.User) like this:
User user = userService.getCurrentUser();
String select_query = "select from " + Greeting.class.getName();
Query query = pm.newQuery(select_query);
query.setFilter("author == paramAuthor");
query.declareParameters("java.lang.String paramAuthor");
greetings = (List<Greeting>) query.execute(user);
The above works fine - but after a bit of messing around I realized this syntax in not very practical as the need to build more complicated queries arises - so I decided to manually build my filters and now I got for example something like the following (where the filter is usually passed in as a string variable but now is built inline for simplicity):
User user = userService.getCurrentUser();
String select_query = "select from " + Greeting.class.getName();
Query query = pm.newQuery(select_query);
query.setFilter("author == '"+ user.getEmail() +"'");
greetings = (List<Greeting>) query.execute();
Obviously this won't work even if this syntax with field = 'value' is supported by JDOQL and it works fine on other fields (String types and Enums). The other strange thing is that looking at the Data viewer in the app-engine dashboard the 'author' field is stored as type User but the value is 'user#gmail.com', and then again when I set it up as parameter (the case above that works fine) I am declaring the parameter as a String then passing down an instance of User (user) which gets serialized with a simple toString() (I guess).
Anyone any idea?
Using string substitution in query languages is always a bad idea. It's far too easy for a user to break out and mess with your environment, and it introduces a whole collection of encoding issues, etc.
What was wrong with your earlier parameter substitution approach? As far as I'm aware, it supports everything, and it sidesteps any parsing issues. As far as the problem with knowing how many arguments to pass goes, you can use Query.executeWithMap or Query.executeWithArray to execute a query with an unknown number of arguments.
I have a form in an MS Access database which lists all the landowners consulted with for a new electricity line. At the end of each row is a button which opens another form, showing the details of all consultation, offers made etc.
I am trying to use vb in MS Access to take the contactID and automatically put it in a field in the details form, so that landowner's consultation details will pop up automatically. I am not a vb programmer at all (I have a comp sci degree mostly in Java and I'm currently working as a GIS analyst but it's a small company so I've been asked to get an Access database working).
I want to say
[detailsForm]![contactID] = [landownerlist]![ID]
in a way that vb and access will be happy with. Then I can see if I'm on the right track and if it will actually work! What I have above does not actually work. It won't compile.
From Kaliana
If you wish to open a form to a new record and to set the ID there, you can use Openargs, an argument of Openform:
DoCmd.OpenForm "FormName",,,,acFormAdd,,Me.ID
The opened form would also need some code:
If Me.Openargs<>vbNullstring Then
Me.Id = Me.Openargs
End If
It is also possible to find:
Forms!LandownersList.Recordset.FindFirst "ID=" & Me.ID
or fill in a value:
Forms!LandownersList!Id = Me.ID
on the form being opened from the calling form.
You may want to look into the code that is behind these buttons. If you are using a docmd.openform you can set the 4th Setting to a where clause on openning the next form.
DoCmd.OpenForm "OpenFormName", acNormal, , "[contactID] = " _
& [detailsForm]![contactID] , acFormEdit, acWindowNormal
This assumes contact ID is numeric and doesn't require any quotes.
Using open args is the generally accepted solution, as alluded to by others. This just falls under the category of "For you edification":) One of the problems with using open args is that unless you are careful with your comments it's easy to forget what they were supposed to mean. Were you passing more than one? Which is which? How did I do it here? How did I do it there etc. For my own money, I standardized to this (below) so I can always pass more than one argument without fear, and when I review my code a year from now, I can still see what's what without a huge hassle:
Option Explicit
'Example use: DoCmd.OpenForm "Example", OpenArgs:="Some Filter|True"
Public Enum eForm1Args
eFilter = 0
eIsSpecial = 1
End Enum
Private m_strArgs() As String
Public Property Get Args(ByVal eForm1Args As eForm1Args) As String
Args = m_strArgs(eForm1Args)
End Property
Private Sub Form_Open(Cancel As Integer)
m_strArgs = Split(Nz(Me.OpenArgs, vbNullString), "|")
If LenB(Me.Args(eFilter)) Then Me.Filter = Me.Args(eFilter)
End Sub
Private Sub Command1_Click()
If LCase$(Me.Args(eIsSpecial)) = "true" Then
'Do something special
End If
End Sub
As previously posted OpenArgs is great for this. One trick I have learned is that it is easy to pass in multiple parameters if required as a delimited string (comma for example), the target form can then access these values using the Split() function thus:
StringArrayVariable()= Split(me.OpenArgs,",")
Me.textbox= StringArrayVariable(0)
Me.textbox1= StringArrayVariable(1)
etc.
This is air code so check out the helpfile for Split().
It is also possible to pass objects in OpenArgs as well, it requires some manual memory pointer manipulation and I don't have the code to hand but I'm sure a Google search will find some examples. This technique can cause some random crashes though. Be Warned!