|
I have this question :
A gas pump calculates the cost of gas at a local gas station. The station charges 2.09 per gallon for regular grade gas, $2.16 per gallon for special grade gas and $2.26 per gallon for the super grade gas. The click event handler for each button calls a method to compute the total cost
from the number of gallons entered and the selected grade.
I have attempted this way :
Public Class FrmGasolineCalculator
Inherits System.Windows.Forms.Form
'Labels for the form are defined here
Friend WithEvents lblTitle As System.Windows.Forms.Label 'Title label
Friend WithEvents lblLabel1 As System.Windows.Forms.Label 'Label to prompt user input
Friend WithEvents lblLabel3 As System.Windows.Forms.Label 'Label to display the output
'Read one number from the user as an input
Friend WithEvents txtInput As System.Windows.Forms.TextBox
Friend WithEvents txtResult As System.Windows.Forms.Label 'Result will be displayed here
'Reads input and calculates result accordingly
Friend WithEvents btnRegular As System.Windows.Forms.Button
Friend WithEvents btnSpecial As System.Windows.Forms.Button
Friend WithEvents btnSuperplus As System.Windows.Forms.Button
Dim result As Decimal
Function Calculate(ByVal Total As Decimal) As Decimal
Dim Quantity As Integer = 0
txtInput.Text = Convert.ToInt32(Quantity)
Total = 0
Dim Cost As Decimal
Select Case Cost
Case 2.09
Total = Quantity * 2.09
Return Total
Case 2.16
Total = Quantity * 2.16
Return Total
Case 2.26
Total = Quantity * 2.26
Return Total
End Select
End Function
Private Sub Regular_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Regular.Click
txtResult.Text = Convert.ToDecimal(result)
result = 0
If Not txtInput.Text = " " Then
result = Calculate(result)
End If
End Sub
End Class
I have not handled the other two buttons Special and Superplus because the programming will be the same.
Error is :
Object reference not set to an instance of an object. How to handle this ?
Thanks
|
|
|
|
|
Hi,
a couple of remarks:
1.
all errors show line numbers:
- compile errors mention file name and line number
- run-time exceptions show complete stack traceback with line numbers
2.
Visual Studio can be told to show line numbers in the left margin of the editor,
see Options/Tools/TextEditor
3.
combining 1 and 2, if something goes wrong you can easily determine where the error
occured.
4.
I have never seen cases with decimal values, but I doubt you need them.
What happened to TotalCost = Quantity * UnitCost ?
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- use PRE tags to preserve formatting when showing multi-line code snippets
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Hi all
sorry to trouble you.
actually my problem is i have to split one large file into small files.
based on these conditions, i have maximum file size and minimum file size.
to split the large file into smaller ones,the child file should be either maximum size or greater than minimum size and less than maximum size.
iam struggling to write the logic,plese help me to write the logic.
suppose for example large filesize is 1050MB and max size is 100MB and minimmum size is 90MB.i have to split first as many as possible max size files and remaining are in between max and minimum size.
advancedly thanks to giving replies
Thanks&Regards
RENU
|
|
|
|
|
I remember answering the same question a while back. Search in the forum and see if you can find it.
---
single minded; short sighted; long gone;
|
|
|
|
|
Yes, tell your teacher/professor to give you different homework, since that one
can be found everywhere on the web.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- use PRE tags to preserve formatting when showing multi-line code snippets
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
I have created an application in vb .NET using ODBCConnection Object (for MS-Access). this is a MDI Application. when i am running this Exe thru the network it is showing following error-
[Request for the permission of type 'System.Data.Odbc.OdbcPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
Error in SQL Connection
I Want to solve this problem thru code in 'VB .NET'. I do not want to do this manually as we do from "Control Panel->adminstrative tool->MS .NET Framework2.0 Configuration" How can i solve this problem. Please help me.
my mail ID is kkb_mda@yahoo.co.in
Thanks and Regards
Kishore
|
|
|
|
|
You asked this yesterday and were given the correct answer. You have NO CHOICE but to read up on and understand Code Access Security[^].
You're getting this exception because code executed from an non-local (read: network) source is NOT TRUSTED and runs in a heavily restricted sandbox. You have to tell the machines running the code to trust the source of the code to get more permissions.
|
|
|
|
|
Dear All im a newbie to scripting so please go easy on me.
Ive nearly finished my script but im stuck on the last points.
I need to append data in "Column A" created early in the script (groupmember.csv) with the data that is outputted by the password software last in the script, Currently its a .txt (newpasswords.txt) but could be a .csv.
Help please
thanks
paul
Option Explicit
Dim strCN, strFilter, objShell, strCmd, filePath
Dim oExcel, oSheet
' Prompt for group names and construct the filter.
strCN = ""
strFilter = "(|"
Do
strCN = InputBox("Enter Common Name of a group or Cancel", "Test")
If (strCN <> "") Then
strFilter = strFilter & "(memberOf=cn=" & strCN _
& ",ou=MYOU,dc=DOMAIN,dc=co,dc=uk)"
End If
Loop While strCN <> ""
strFilter = strFilter & ")"
' Use the WshShell object to run the csvde command.
If strFilter <> "(|)" Then
filePath = "d:\get-group\GroupMember.csv"
Set objShell = CreateObject("Wscript.Shell")
strCmd = "%comspec% /c csvde -f " & filePath & " -r """ & strFilter & """"
strCmd = strCmd & " -l givenName,userPrincipalName"
strCmd = strCmd & " -s SERVER.DOMAIN.co.UK"
objShell.Run strCmd, , True
'Deletes line 1 from GroupMember.csv
Const xlUp = -4162
Const xlLeft = -4131
Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = True
oExcel.Workbooks.Open filePath
Set oSheet = oExcel.ActiveWorkbook.Worksheets(1)
oSheet.Rows(1).Delete xlUp
'deletes Column A from GroupMember.csv
oSheet.Columns(1).Delete xlLeft
Dim c
For Each c In oSheet.UsedRange.Columns(1).Cells
c.Value = Left(LCase(c), 3)
Next
' saves the file
oExcel.DisplayAlerts = False
oExcel.ActiveWorkbook.SaveAs filePath
oExcel.ActiveWorkbook.Close
oExcel.Quit
Set oSheet = Nothing
Set oExcel = Nothing
End If
' send commandline parameters to Password Generator Professional
Set wshShell = WScript.CreateObject ("WSCript.shell")
wshShell.run """C:\Program Files\Kristanix\Password Generator Professional\PGP.exe"" /output ""d:\get-group\newpasswords.txt"" /format ""$p"" /quantity 1600 /showmsg /unique /mask ""{N:n,3,3}{N:s,1,1}""", 6, True
set wshshell = nothing
|
|
|
|
|
I always love when someone says that they're "nearly finished", but yet half their code still needs to be written...
To translate what I THINK you're trying to say, you want to read the passwords from a text file and put them in Column B of this Excel sheet??
Since it looks like you already know how to use the Excel object model, all you would need to do is use a FileSystemObject to open and read the passwords file, line by line, and put the password in the cell you're keeping track of, until you reach the end of the file.
|
|
|
|
|
Hi there Dave thank you so much for your reply.
(when i say nearly there ive only one more step to go on paper anyway !!!)
Sorry if my description was garbled
Yes i want to read the passwords from a text file.
But i want to add them INTO Col A (APPEND T0)
(Col A already contains info [First 3 chars of forename in LC] )
Column A will then be read by an app my developers are creating which will reset the users password to that in Col A.
Dave,
Im really feeling my way in the dark and have just been playing until i get somthing that works as im really new to scripting anything of consiquence.
Its the appending part that i suppose im really stuck on.
Many kind Regards
Paul
|
|
|
|
|
OK. Same thing, only instead of assigning the string read from the password file to column B, you're combining strings in column A.
Cell(row,col).Value = Call(row,col).Value & TextLineFromFile
|
|
|
|
|
Hi Dave,
Thanks for that.
Ive managed to cobble together some code which will copy from a .TXT to Col A of a .CSV.
Whereabouts\how would i addin your suggestion of:
Cell(row,col).Value = Call(row,col).Value & TextLineFromFile
Where you say (row,col)does that mean i need to specify the row and column..as ive a lot of row data to add.
Code below
-----------------------------------------------------------------------------
Const ForReading = 1
Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = True
oExcel.Workbooks.Add
i = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\passwords.txt", ForReading)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
arrLine = Split(strLine, ",")
oExcel.Cells(i, 1).Value = arrLine(0)
i = i + 1
Loop
oExcel.ActiveWorkbook.SaveAs ("c:\newtest.csv")
objFile.Close
oExcel.Quit
-----------------------------------------------------------------------------
|
|
|
|
|
Sorry
Forgot to add that quite obviously Creates a new file not adding to an existing one....
Paul
|
|
|
|
|
I don't do Office interop so I can't give you the exact syntax, but this is the right idea. You just need to get the existing value of the cell then append the string you read from the file to it and assign the new value to the cell.
|
|
|
|
|
I have an Access DB with a list of members and attendance dates. I have brought the data in with a left join (I need all members regardless of attendance). I need help setting up the following - I would like to be able to choose a member name from a combobox drop down and upon selection populate a datagrid with their attendance information. I need to do two things: Retrieve only unique names from the table for the combobox (right now, every name can have multiple entries because of the attendance join) using some kind of filter, and at the same time when a record with attendance entries is selected remove the filter to populate the datagrid. Right now I am binding the comboxbox to a BindingSource. Could someone walk me through how I would go about this?
Thanks.
|
|
|
|
|
You should ask these questions in the database forum as your problems are SQL-related rather than VB.
Paul Marfleet
|
|
|
|
|
What do you mean? The import and all that is working fine, I just need to know how to filter the datatable for the combobox and at the same moment have it display unfiltered info for the datagrid. In other words, at this point the datatable would look something like this:
MemberID, MemberName AttendID AttendHistory
100,a,1, 12/1/2007
100,a,2, 6/1/2007
100,a,3, 7/1/2007
101,b,1,12/1/2007
101,b,2,1/1/2007
etc.
I need the combo box to only retrieve the unique MemberIDs for its datasource (which I think will require filtering) and once selected to retrieve all of the rows connected to that member (which I think will require removing the filter.
|
|
|
|
|
have the datagrid continue to be connected to the datatable.
for your combobox have it be connected to a dataview. first setup the dataview filtering out the records you want. just check the help or internet on how to use a dataview.
|
|
|
|
|
Thanks.. so you are saying that so long as the source of data (ie the datatable) is showing all of the information, when I move through the records, the filtered dataview and unfiltered datatable will be in sync?
|
|
|
|
|
Yeah, the import is working fine, but your database stucture is total garbage. You should have three tables. One with all the members in it, another with all the conferences in it and another table that joins the two with memberId's that tell which conferenceId's they attended.
Ask this in the SQL Forum, cause right now, your code isn't workable until the database behind gets fixed.
|
|
|
|
|
How can i simultaneously Iterate and modify a .NET Queue i am using the following code:
<code>
Dim q As Queue = New Queue
Sub LoadValues()
q.Enqueue("mystr1")
q.Enqueue("mystr2")
q.Enqueue("mystr3")
executeloop()
End Sub
Sub executeloop()
If q.Count > 0 Then
For Each str As String In q
'do something with str
q.Dequeue()
MsgBox(q.Count.ToString())
Exit For
Next
executeloop()
End If
End Sub
</code>
And how can i add items to the Queue while iterating should i use SyncLock because I'm working on a Multithreaded application, and also not sure about the performance need your suggestions...
AliAmjad (MCP)
|
|
|
|
|
You cannot modify the collection as your enumerating it. You can modify the objects IN the collection, but you cannot modify the size of the collection (Add, Delete) at all in a For/Each loop.
|
|
|
|
|
Then what should I use for this purpose. I want to add items to the queue and at the same time want to manipulate it. Should I use collection object. And what's wrong with the code given above i know i cannot modify it while iterating but then what should i do to achieve the objective. Thanks for your time man.
AliAmjad (MCP)
|
|
|
|
|
Why are you using a Queue?? A queue is a list of items that's organized and manipulated on a First-In, First-Out basis.
What's the purpose of this collection?? The type of collection you use isn't so much dictated by what it's going to hold, but how the collection is going to be used.
If you want to manipulate items, adding them and removing them, then a List(Of T) would work, an array of strings, ArrayList, ...
|
|
|
|
|
Actually I'm working on a Distributed Web Crawler a Multithreaded application where each URL discovered will going to be added in a queue to implement the breadth-first search model. Many threads will access this queue and get the URLs from the top and then add the newly discovered URLs at the end of the queue and them remove the carwled URLs from the queue. Now what should i use in such a scenario. but when i implemented this logic i get this error "Collection was modified; enumeration operation may not execute".
AliAmjad (MCP)
|
|
|
|