|
|
You're welcome - but please try the official documentation first next time!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I use C# in Visual studio 2022 (in portuguese) and I have a form solution where I open an Excel file and introduce data. In one cell I have to put a formula: "=IF(H5=0;D5;H5)". When I do this I receive an error: System.Runtime.InteropServices.COMException: 'Exceção de HRESULT: 0x800A03EC'. For simple formulae, like "=B4*C4", it works fine. The error is the same wheter the formula is in portruguese or english. Does anyone know what is hapening? Thanks.
|
|
|
|
|
Googling the error code returns several hits suggesting that switching from .xls to the more recent (and more functional) .xlsx format can fix the problem. As always: An error code may have different underlaying causes. Maybe this doesn't help with yours. Make sure to use the .xlsx format anyway, to be sure that that is not the cause of your problems.
|
|
|
|
|
You should be using comas; not semi-colons.
IF function
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Gerry. You are right. In my language we use semicolons. When I substituted the semicolons with commas, it worked! Thank you!
|
|
|
|
|
using System;
using System.Collections.Generic;
using System.Linq;
namespace ValidParenthese {
public class Solution {
public bool IsValid(string s) {
var result = s.ToCharArray().ToList();
bool value = true;
for (int i = 0; i < result.Count; i++) {
if (result[i].ToString().Equals("(")) {
if (result[i + 1].ToString().Equals(")")) {
value = true;
} else {
var count = result.Count;
if (count >= 3) {
var index = result[i + 1];
if (result[i + 2].ToString().Equals(")")) {
value = false;
} else if ((index.ToString().Equals("{") && result[i + 2].ToString().Equals("}"))
|| (index.ToString().Equals("[") && result[i + 2].ToString().Equals("]"))) {
if (result[i + 3].ToString().Equals(")")) {
value = true;
} else {
var ind = result[i + 2];
if ((ind.ToString().Equals("{") && result[i + 3].ToString().Equals("}"))
|| (ind.ToString().Equals("[") && result[i + 3].ToString().Equals("]"))) {
if ((index.ToString().Equals("{") && result[i + 4].ToString().Equals("}"))
|| (index.ToString().Equals("[") && result[i + 4].ToString().Equals("]"))) {
if (result[i + 5].ToString().Equals(")")) {
value = true;
} else {
value = false;
}
} else {
value = false;
}
} else {
value = false;
}
}
}
} else {
value = false;
}
}
} else if (result[i].ToString().Equals("{")) {
if (result[i + 1].ToString().Equals("}")) {
value = true;
} else {
var count = result.Count;
if (count >= 3) {
var index = result[i + 1];
if (result[i + 2].ToString().Equals("}")) {
value = false;
} else if ((index.ToString().Equals("(") && result[i + 2].ToString().Equals(")"))
|| (index.ToString().Equals("[") && result[i + 2].ToString().Equals("]"))) {
if (result[i + 3].ToString().Equals("}")) {
value = true;
} else {
var ind = result[i + 2];
if ((ind.ToString().Equals("(") && result[i + 3].ToString().Equals(")"))
|| (ind.ToString().Equals("[") && result[i + 3].ToString().Equals("]"))) {
if ((index.ToString().Equals("(") && result[i + 4].ToString().Equals(")"))
|| (index.ToString().Equals("[") && result[i + 4].ToString().Equals("]"))) {
if (result[i + 5].ToString().Equals("}")) {
value = true;
} else {
value = false;
}
} else {
value = false;
}
} else {
value = false;
}
}
}
} else {
value = false;
}
}
} else if (result[i].ToString().Equals("[")) {
if (result[i + 1].ToString().Equals("]")) {
value = true;
} else {
var count = result.Count;
if (count >= 3) {
var index = result[i + 1];
if (result[i + 2].ToString().Equals("]")) {
value = false;
} else if ((index.ToString().Equals("(") && result[i + 2].ToString().Equals(")"))
|| (index.ToString().Equals("{") && result[i + 2].ToString().Equals("}"))) {
if (result[i + 3].ToString().Equals("]")) {
value = true;
} else {
var ind = result[i + 2];
if ((ind.ToString().Equals("(") && result[i + 3].ToString().Equals(")"))
|| (ind.ToString().Equals("{") && result[i + 3].ToString().Equals("}"))) {
if ((index.ToString().Equals("(") && result[i + 4].ToString().Equals(")"))
|| (index.ToString().Equals("{") && result[i + 4].ToString().Equals("}"))) {
if (result[i + 5].ToString().Equals("]")) {
value = true;
} else {
value = false;
}
} else {
value = false;
}
} else {
value = false;
}
}
}
} else {
value = false;
}
}
} else {
}
}
return value;
}
}
class ValidParentheses {
public static void Main(string[] args) {
var solution = new Solution();
if (solution.IsValid(Console.ReadLine())) {
Console.WriteLine("valid");
} else {
Console.WriteLine("invalid");
}
}
}
}
|
|
|
|
|
See my answer to your original question on this subject.
|
|
|
|
|
Hint: If you have to call .ToString more than twice in a single method, your design is showing signs of being flawed to the point of being unsalvageable and needs to be scrapped and your approach to the problem redesigned from scratch.
|
|
|
|
|
using System;
using System.Collections.Generic;
namespace CalPoints {
class Solution {
public int CalPoints(string[] ops) {
var opsList = new List<string>();
var value = 0;
foreach (var item in ops) {
if (item.Equals("+")) {
var index = int.Parse(opsList[opsList.Count - 1]) + int.Parse(opsList[opsList.Count - 2]);
opsList.Add(index.ToString());
value = 0;
foreach (var val in opsList) {
value += int.Parse(val);
}
} else if (item.Equals("D")) {
var index = int.Parse(opsList[opsList.Count - 1]) * 2;
opsList.Add(index.ToString());
value = 0;
foreach (var val in opsList) {
value += int.Parse(val);
}
} else if (item.Equals("C")) {
opsList.RemoveAt(opsList.Count - 1);
value = 0;
foreach (var val in opsList) {
value += int.Parse(val);
}
} else {
opsList.Add(item);
foreach (var val in opsList) {
value += int.Parse(val);
}
}
}
return value;
}
}
class CalPoints {
public static void Main(string[] args) {
var solution = new Solution();
var space = new char[] { ' ' };
string[] ops = Console.ReadLine().Split(space);
int output = solution.CalPoints(ops);
Console.Write(output.ToString());
}
}
}
|
|
|
|
|
The error means that you are trying to use a variable reference that does not point to a valid object. For example in the following line:
string[] ops = Console.ReadLine().Split(space);
If the call to Split does not return any strings, then ops is a null reference. You need to use the debugger to find out exactly where the exception is thrown.
|
|
|
|
|
To add to what Richard has said ...
This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.
Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterday's shirt when you took it off last night.
We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!
Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, it will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, the debugger will stop before the error, and let you examine what is going on by stepping through the code looking at your values.
But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
That happens when you declare an object (you mention just the type and object name) and leave it there, and later try to use it. You need to also use the new keyword followed by the object type before you can use the object (sometype somename = new sometype() that sort of thing, I don`t remember the exact C# syntax)
modified 28-Jul-22 7:53am.
|
|
|
|
|
I use the following code to read pdf file into memory stream:
private void PdfReport(string address)
{
PdfSource = new FileStream(address, FileMode.Open);
}
When I tried to open some files one after another, I noticed that the memory occupation increases gradually. How can I manage memory in this case?
modified 26-Jul-22 12:40pm.
|
|
|
|
|
It may mean that you are creating streams faster than the GC can clean up any freed objects. But without more information that is very much a guess.
|
|
|
|
|
I use Syncfusion PDF for loading pdf files in my WPF application. I use MVVM architecture in my project. I have the following property:
private int _selectedId;
public int SelectedId
{
get { return _selectedId; }
set
{
SetProperty(ref _selectedId, value);
if (value != null)
{
PdfSource = null;
string fileName = GetFileName(SelectedId);
if (fileName != null)
{
PdfReport(string.Format("{0}/{1}", "src/pdfs/", fileName));
}
}
}
}
private Stream _pdfSource;
public Stream PdfSource
{
get { return _pdfSource; }
set { SetProperty(ref _pdfSource, value); }
}
When I read file stream into this property, it can load the pdf and show it.
I don't know why there is a memory leak.
Is there any solution to this problem?
|
|
|
|
|
You need to Dispose of disposable objects like streams in order to free up the resources they're using.
When you set the PdfSource property to null , you throw the stream away without disposing of it. You have to wait for a GC cycle to detect that it's no longer referenced and "finalize" it.
Change your code to dispose of the existing stream (if any). For example:
public Stream PdfSource
{
get { return _pdfSource; }
set
{
if (ReferenceEquals(value, _pdfSource)) return;
if (_pdfSource != null) _pdfSource.Dispose();
SetProperty(ref _pdfSource, value);
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
First, are you looking in Task Manager to determine this?
It's lying to you. It's not showing you how much memory your app is taking. It's showing you how much is RESERVED for your app.
You have to understand how memory allocations work in the .NET CLR and how it relates to Windows memory management. The .NET CLR manages the "Managed Heap", which is what your app sees (and Task Manager does NOT). When your app allocates an object, it's allocated on the Managed Heap. When an object is freed, that memory goes back into the Managed Heap, NOT BACK TO WINDOWS!
Just because your code is not using an object anymore, that does NOT mean the memory it occupies is immediately returned to the Managed Heap!
When more objects need to be allocated, it's faster for the CLR to allocate from the Managed Heap than it is for it to find out there's not enough enough heap left, go to Windows to get another block of memory, add it to the heap, then allocate your object.
This is why you're seeing what's RESERVED for your app.
When the CLR feels the conditions are right, it cleans up the managed heap, moving objects around and freeing up objects that don't have a reference being held anymore, returning that freed memory back to the Managed Heap. This is called Garbage Collection.
When Windows needs more memory, it asks the .NET CLR for whatever it can free up. If there's sufficient space on the Managed Heap, it'll shrink the size of the heap and return that freed memory back to Windows. If your code quickly allocats lots of objects and frees them, this can give the illusion of a memory leak.
|
|
|
|
|
Thanks for the explanation. Another question: Can giving a null value to a filled property release the memory it takes?
|
|
|
|
|
If you're thinking about the memory the property itself takes, no.
If you're thinking of the object the property is holding onto, it just reduces the reference count on the object. Reducing that count to 0 (nothing holds a reference to the object anymore) only tells the GC that the object CAN be collected and the memory returned to the managed heap.
In no case does releasing a managed object in .NET return memory back to Windows.
|
|
|
|
|
(both applications are written by us, application A can be a C# application or a C++ application )
I have an application A that starts application B.
I would need to know when application B is ready to process some rpc messages from application A.
I was thinking of sending a windows event from application B and wait for it in application A (application A can wait a little bit, no need to be extra fancy)
Or maybe start sending rpc (dummy ping) message in repetition from application A until application B can answer them properly ?
Any other better way to do this ?
Thanks.
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|
|
Why not get application B to send the first message? That way application A knows that B is ready.
|
|
|
|
|
If A has a FileSystemWatcher, and B writes a file when it starts, A gets a wake-up call via the FileSystemWatcher's event handler.
FileSystemWatcher Class (System.IO) | Microsoft Docs
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
I have this simple async web request code:
class AsyncResponse
{
public AsyncResponse()
{
test2();
Console.ReadKey();
}
public void test2()
{
for (int i = 0; i < 100; i++)
{
string url = "https//www.google.com";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.BeginGetResponse(new AsyncCallback(FinishWebRequest), request);
}
}
private void FinishWebRequest(IAsyncResult result)
{
HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
Console.WriteLine("Finished");
}
}
I only got on console two "Finished" lines, meaning FinishWebRequest didn't get finished more then 2 times, but I have 100 requests.
What is the issue?
|
|
|
|