|
Thanks for your reply.
Christian Graus wrote: This should not compile.
I compiles fine, 'is' works with structures as well. And besides - 'x' in the previous example is of type 'object', so the compiler has no way of knowing it is going to contain an integer.
Christian Graus wrote: You could try calling ToString on the object and then decimal.TryParse, that should work for things like int.
String parsing seems to me like the slowest possible approach. I guess I'll go with the try/catch, which is guaranteed to work, unless someone comes with something faster.
|
|
|
|
|
Member 1033907 wrote: I compiles fine, 'is' works with structures as well.
I didn't think that was the case. I know I've had 'is' not compile for me before because I used an integral type.
Member 1033907 wrote: String parsing seems to me like the slowest possible approach
Well, you're already paying the cost of boxing, which is expensive, and talking about try/catch, which is VERY expensive.
Member 1033907 wrote: I guess I'll go with the try/catch, which is guaranteed to work, unless someone comes with something faster.
I would do some testing to see which is faster.
Christian Graus
Driven to the arms of OSX by Vista.
"! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums.
I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
|
|
|
|
|
I've got it. It's 'as' that doesn't work, because int cannot be null. But, is and as both rely on an inheritance tree, they do not try to do any sort of conversion between types. decimal does not derive from int, or vice versa, so neither of those will work. You're asking if the type can be converted, not if it is of that type to start with.
Christian Graus
Driven to the arms of OSX by Vista.
"! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums.
I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
|
|
|
|
|
Member 1033907 wrote: int can be cast to decimal without an exception being thrown - so why does it not work?
Just because you can cast to it does not mean it is part of the same type heirarchy. Types can provide their own casting operators to make coercion to other types easier for the programmer.
is /as only work on things when they are along the same branch of an object heirarchy.
decimal derives from object . int derives from object . Each is heading down different branches.
Man who stand on hill with mouth open wait long time for roast duck to drop in
|
|
|
|
|
The term "cast" is used to mean two different things, one of which is more like "convert".
When an int is "cast" to decimal it gets converted. The is operator doesn't work with that type of cast.
|
|
|
|
|
I wrote an app which creates excel reports. I would like to kill all the excel processes my application started. How do I figure out if my application started the process so I only kill those processes?
I have this code so far:
foreach (Process p in Process.GetProcessesByName("EXCEL"))
{
p.Kill();
}
However, as it is clear from the code, it kills all excel processes.
CodingYoshi
Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07
|
|
|
|
|
Hi,
AFAIK there ever is only one Excel process; opening an Excel document causes Excel to start if it isn't already running; opening a second Excel document gets directed towards the first Excel process. So you can only kill one Excel process, and doing so may be a mistake since it may have open documents your app did not open. Your app should tell Excel to close documents, and optionally, when no more documents are open, it could tell Excel to exit. Under no circumstances it should kill Excel.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Luc Pattyn wrote: AFAIK there ever is only one Excel process
You can use the .IgnoreRemoteRequests = true property to force excel to open in multiple instances for each workbook, so sometimes you would need a loop.
My question is if he opens excel in his code to generate reports, there is no need to terminate them abnormally, especially when a client may have unrelated excel instances open unrelated to his code. He can simply terminate them through his code by using .quit, can't he?
|
|
|
|
|
No, there are actually separate processes created. Excel 2003
CodingYoshi
Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07
|
|
|
|
|
Why don't you just quit the excel instance through your code?
|
|
|
|
|
I tried Quit() along with setting it to null but the process still existed. Any other idea?
I am thinking of logging the time my app starts and then checking the start time of each process. If the start time is bigger than start time of app and created by the same user then I will kill it. But it is still possible, the user opened an excel file outside my app while my app is active so this is not the best solution.
CodingYoshi
Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07
|
|
|
|
|
Did you try something along the lines of
Where _excelApplication = Excel.Application
_excelApplication.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(_excelApplication);
?
|
|
|
|
|
Just did both quit and ReleaseComObject methods but still there.
CodingYoshi
Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07
|
|
|
|
|
Without seeing your code I'd say your out of luck.
|
|
|
|
|
Hope you can follow it through since I have written it in VB.NET.
This is just a wrapper. The parts which might be of interest to you are the constructor and the Create method.
Public Class ExcelBook
Implements IObservable
Private _parameters As Parameters
Private _repoHelper As RepositoryHelper
Private _sheets As Dictionary(Of String, Worksheet)
Private _workBook As Workbook
Private _xlApp As Application
Private _summarySheet As ExcelSheet
Public Sub New(ByVal params As Parameters)
Me._xlApp = New Application()
Me._workBook = Me._xlApp.Workbooks.Add()
Me._repoHelper = Nothing
Me._sheets = New Dictionary(Of String, Worksheet)(53)
Me._parameters = params
End Sub
Private Sub DeleteDefaultSheets()
Dim DeadSheet As Worksheet
DeadSheet = Me._workBook.Sheets("Sheet1")
DeadSheet.Delete()
DeadSheet = Me._workBook.Sheets("Sheet2")
DeadSheet.Delete()
DeadSheet = Me._workBook.Sheets("Sheet3")
DeadSheet.Delete()
End Sub
Public Sub Create()
Me.CreateRepositories()
Dim xSheet As ExcelSheet = Nothing
Dim sheetSpecs As Sheet = Nothing
Dim wSheet As Worksheet = Nothing
' If the report has a detail sheet and range is multiple for month or week or season then provide detail sheet specs to
' ExcelSheet constructor to create sheets with specification of the detail sheet
' First sheet must be reserved for the summary sheet
' After all detail sheets are finished, a summary sheet must be created.
Dim stepInfo As New StepEventArgs()
If (Me._parameters.Report.HasDetailSheet And Me._parameters.IsMultipleRanges) Then
' Details Sheets
sheetSpecs = Me._parameters.Report.DetailSheet
For Each repo As KeyValuePair(Of String, Repository) In Me._repoHelper
wSheet = CType(Me._workBook.Worksheets.Add(), Worksheet)
wSheet.Name = repo.Key
xSheet = New ExcelSheet(wSheet, sheetSpecs, repo.Value, False, Me._parameters, Me._workBook)
xSheet.Fill()
Me._sheets.Add(repo.Key, xSheet.SpreadSheet)
stepInfo.Message = repo.Key & " completed."
RaiseEvent StepCompleted(Me, stepInfo)
Next
Me.DeleteDefaultSheets()
' All detail sheets are done now do the summary
Dim repositoryTemplate As Repository = Nothing
For Each repo As KeyValuePair(Of String, Repository) In Me._repoHelper
repositoryTemplate = repo.Value
Exit For
Next
wSheet = CType(Me._workBook.Worksheets.Add(), Worksheet)
wSheet.Name = "Summary"
' Summary Sheet
sheetSpecs = Me._parameters.Report.SummarySheet
xSheet = New ExcelSheet(wSheet, sheetSpecs, repositoryTemplate, True, Me._parameters, Me._workBook)
Me._sheets.Add("Summary", xSheet.SpreadSheet)
Me._summarySheet = xSheet
Me._summarySheet.Fill()
stepInfo.Message = "Summary sheet completed."
RaiseEvent StepCompleted(Me, stepInfo)
Else ' Report has only one sheet
sheetSpecs = Me._parameters.Report.SummarySheet
wSheet = CType(Me._workBook.Worksheets.Add(), Worksheet)
Me.DeleteDefaultSheets()
wSheet.Name = "Summary"
xSheet = New ExcelSheet(wSheet, sheetSpecs, Me._repoHelper.Repository, False, Me._parameters, Me._workBook)
xSheet.Fill()
Me._sheets.Add("Summary", xSheet.SpreadSheet)
stepInfo.Message = "Summary sheet completed."
RaiseEvent StepCompleted(Me, stepInfo)
End If
Dim path As String = My.Settings.FilePath & "Report - " & Now.Hour.ToString() & "-" & Now.Minute.ToString() & Me._parameters.Div.ToString() & ".xls"
'path = Me._workBook.Name
Me.FreezeColumns(Me._sheets("Summary").Range("C8:C8"))
Me._workBook.SaveAs(path, Excel.XlFileFormat.xlExcel9795)
Me._xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(Me._xlApp)
'Me._xlApp.DisplayAlerts = True
'Me._xlApp.ScreenUpdating = True
'Me._xlApp.Visible = True
End Sub
Private Sub AddToSummarySheet(ByVal spreadSheet As ExcelSheet)
' Ask the sheet to give you its formula in a dictionary for each column and row
End Sub
Private Sub CreateRepositories()
If (Me._parameters.TypeOfReport = ReportTypes.Year_To_Date_Weekly) Then
Me._repoHelper = New RepositoryHelperWeekly()
Me._repoHelper.CreateRepositories(Me._parameters)
End If
If (Me._parameters.TypeOfReport = ReportTypes.Year_To_Date_Monthly) Then
Me._repoHelper = New RepositoryHelperMonthly()
Me._repoHelper.CreateRepositories(Me._parameters)
End If
If (Me._parameters.TypeOfReport = ReportTypes.Year_To_Date_Seasonal) Then
Me._repoHelper = New RepositoryHelperSeasonally()
Me._repoHelper.CreateRepositories(Me._parameters)
End If
End Sub
Private Sub FreezeColumns(ByVal range As Range)
range.Select()
Me._xlApp.ActiveWindow.FreezePanes = True
End Sub
Public Event StepCompleted(ByVal sender As Object, ByVal e As StepEventArgs) Implements IObservable.StepCompleted
End Class
CodingYoshi
Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07
|
|
|
|
|
next time, post your question in the vb.net forums, it really helps to know what language you are dealing with.
Secondly, use the pre tags.
Thirdly, this is how I did it (in vb.net)
Public Sub Terminate()
If _excelApplication IsNot Nothing Then
_excelApplication.DisplayAlerts = False
_excelApplication.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(_excelApplication)
_excelApplication = Nothing
_excelApplication.Finalize()
End If
End Sub
jeez.
|
|
|
|
|
Sorry about that. The reason I posted it here is because I did not think it will get to the point where I need to post code.
Still no luck. The process exists after doing all of the above.
EliottA wrote: Thirdly, this is how I did it (in vb.net)
You mean it worked for you? Finalize() is not a member of Excel.Application.
CodingYoshi
Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07
|
|
|
|
|
Sorry, did not mean to throw that line in there.
CodingYoshi wrote: he reason I posted it here is because I did not think it will get to the point where I need to post code.
Still belongs in the VB.Net forum.
CodingYoshi wrote: The process exists after doing all of the above.
Err....ummm....does the process exist before execution...?
|
|
|
|
|
No, I manually check to make sure no process exists before the execution and I also kill all the instances programmatically just to reassure using this code:
foreach (Process p in Process.GetProcessesByName("EXCEL"))
{
p.Kill();
}
CodingYoshi
Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07
|
|
|
|
|
Again that is horrible design, what if a client has excel running?
Create a new instance of excel, and set it's IgnoreRemoteRequests property to true, this will cause *your* instance of excel to operate under a new process.
|
|
|
|
|
How do you create excell processes?
System.Diagnostics.Process.Start("...") Something like that? If so save the process that you create.
List<Process> list = new List<Process>();
Process p = System.Diagnostics.Process.Start("...");
list.Add(p);
...
list[i].Kill();
|
|
|
|
|
No, the problem is to make excel spreadsheets you have to create a new instance of the application class and add workbooks to it. This creates a process and I am trying to kill the process which I created but no luck so far. Here is the snippet:
public void Constructor(Parameters params)
{
this._xlApp = New Application(); // This is the process I want to kill once I am finished creating the file.
this._workBook = Me._xlApp.Workbooks.Add();
this._repoHelper = null;
this._sheets = New Dictionary<String, Worksheet>(53);
this._parameters = params;
}
CodingYoshi
Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07
|
|
|
|
|
Hi,
I'm kind of new to programming and to c# about 18 months c# and twice that with vba with bits of javascrip and, vbscript... and a hobbyist so it sounds a lot but it isn't!
Anyways I have never used images before in programming other than to swap a picture given a value in the code.
I now want to be able to have an image on a windows form that you can left click and a dialogue box appear that shows the position in pixels the mouse is on the image.
To be honest I'd be happy with just that. BUT really on the picture I want to have (probably the wrong word) but 'hotspots'/hot areas.
I am designing a form to be able to record where on a person has an injury. I want the user to be able to click say on the left shoulder of the person on the image and the program know that the user clicked on the left shoulder at the image pixel count of (100, 100) for example.
Would it be best to do this thru a browser displayed image that has a javascript script running on it and embedding in it somehow position (left shoulder) the user has clicked?
The reason why I want need the pixel count is because the results will be shown on a website.
Am I attempting too much? Could somebody please give me some direction?
Thank you in advance,
Mark
|
|
|
|
|
Take a look at The Hot Spot .NET Control[^], to see if it gives you any ideas.
A friend of mine did an application for Bronchoscopy using similar techniques in VB 3.0 some time ago.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Henry Minute wrote: in VB 3.0 some time three decades ago
ftfy
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|