|
|
This does not appear to be working and I can't figure out why
<br />
MemoryStream ms = new MemoryStream();<br />
string message;<br />
<br />
….some code here to fill the memory stream….<br />
<br />
Using (StreamReader sr = new StreamReader(ms))<br />
{<br />
message = sr.ReadToEnd();<br />
}<br />
<br />
ms.Position = 0;
Blog Have I http:\\www.frankkerrigan.com
|
|
|
|
|
Frank Kerrigan wrote: This does not appear to be working and I can't figure out why
What exactly do you mean by "not working"?
|
|
|
|
|
This code was taken from a live web service that had been running perfectly happily for nearly 3 months and then all of a sudden stopped working, throwing the ObjectDisposedException.
Blog Have I http:\\www.frankkerrigan.com
|
|
|
|
|
|
Alois Kraus wrote: http://geekswithblogs.net/akraus1/articles/81629.aspx
Sorry, that link doesn't work for me.
|
|
|
|
|
The reason is because the using block closes the StreamReader when you move out of the scope of the using block. When you close a StreamReader OR a StreamWriter , however, the underlying stream object is also closed and thus disposed. If your stream was a FileStream then that might make logical sense but it is not so obvious with a MemoryStream because you probably want to use it as temporary storage or pass to another method for further processing etc. Even if you did not use the using block the MemoryStream would still be closed the moment the StreamReader is disposed of or garbage collected.
I've no idea why it would just suddenly show up now after so many months - That is a curiosity.
|
|
|
|
|
Thanks guys
Blog Have I http:\\www.frankkerrigan.com
|
|
|
|
|
sr.ReadToEnd will read to the end from the current position. Make sure the position is 0 before reading to end. Also, don't touch the memory stream after it has been disposed. The using(...) block will dispose the object; it will actually compile down to this:
StreamReader sr = null;
try
{
sr = new StreamReader(ms);
message = sr.ReadToEnd();
}
finally
{
if(sr != null)
{
sr.Dispose();
}
}
Since it gets disposed when the using block leaves scope, callign ms.Position = 0 after the using scope is going to cause problems.
|
|
|
|
|
Hi,
I am trying to make no difference between the . (dot) and , (comma) which is typed by the user in a textbox.
I got an array of 20 textboxes. How can i change this for all the textboxes?
I tried this:
private double[] _answer = new double[20];
private TextBox[] _textBox = new TextBox[20];
private Label[] _labels = new Label[20];
private double _tempAnswer;
foreach (Control textbox in this.Controls)
{
if (textbox is TextBox)
{
int controlIndex = 0;
for (int k = 0; k < 20; k++)
{
string decimalSeparator = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
this._tempAnswer = Convert.ToDouble(this._textBox[k].Text.Replace(".", decimalSeparator).Replace(",", decimalSeparator));
this._answer[controlIndex] = this._tempAnswer;
}
}
}
I don't get error's in compile time but in run time.
The error i get is "Input string was not in a correct format".
And my input was the first time the . (dot) second time , (comma). Both give me that error pointing to this line:
this._tempAnswer = Convert.ToDouble(this._textBox[k].Text.Replace(".", decimalSeparator).Replace(",", decimalSeparator));
What am i doing wrong?
Thanks in advance!
|
|
|
|
|
You don't want to replace the '.' with anything, just replace the ',' with a '.'
|
|
|
|
|
Hello,
Yustme wrote: What am i doing wrong?
It depends on what your input Text was.
But, you shouldn't use the Convert.ToDouble Method.
Better use:
double d;
if(double.TryParse(this._textBox[k].Text,System.Globalization.NumberStyles.Float, CultureInfo.CurrentCulture, out d))
{
}
In this case there is no Exception.
Yustme wrote: Text.Replace(".", decimalSeparator).Replace(",", decimalSeparator));
Never seen that bevor?
Hope that helps
Martin
|
|
|
|
|
Hi guys,
This is what i got now:
foreach (Control textbox in this.Controls)
{
if (textbox is TextBox)
{
double d;
for (int k = 0; k < 20; k++)
{
if (double.TryParse(this._textBox[k].Text, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.CurrentCulture, out d))
{
string decimalSeparator = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
}
}
}
}
I don't get any error's but its not working either.
I was wondering what i had to put in the "if(double.TryParse..." body?
Thanks in advance!
|
|
|
|
|
Hello,
string _decimalSeparator = System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;<br />
<br />
string input = this._textBox[k].Text;<br />
input = input.Replace(".", decimalSeparator );<br />
input = input.Replace(",", decimalSeparator );<br />
<br />
double d;<br />
if(double.TryParse(input ,System.Globalization.NumberStyles.Float, CultureInfo.CurrentCulture, out d))<br />
{<br />
}<br />
This should do it!
All the best,
Martin
|
|
|
|
|
Hi Martin,
Thanks for your reply!
I've been debugging it for like 30 times now to see where it go's wrong.
The double d seems not to getting the right separator from "input".
My input was 0.5 and it gets replaced by the , (comma).
In the body of the if statement, the d has 0.5 as value instead of 0,5 which is the value of "input".
Never did anything with Globalization. I also want to know what "out d" does.
Can you look into this again for me please?
|
|
|
|
|
Hello,
Yustme wrote: I also want to know what "out d" does.
This is just the out parameter of the TryParse Method.
That means, you give the reference of your lokal double variable "d" to the method, and you will get the result out of the Method on exactly the same variable.
This Method is using the possibility of the out parameter, because the return value is boolean.
The parsing too double was only ok, if the return value is true.
Therefore you have to put the code into an if statement, like I did.
Yustme wrote: In the body of the if statement, the d has 0.5 as value instead of 0,5 which is the value of "input".
please send the whole code lines.
All the best,
Martin
|
|
|
|
|
Hi Martin,
Thanks again for your reply!
This is the code where i try to convert the separator:
foreach (Control textbox in this.Controls)
{
if (textbox is TextBox)
{
int controlIndex = 0;
for (int k = 0; k < 20; k++)
{
string decimalSeparator = System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
string input = this._textBox[k].Text;
input = input.Replace(".", decimalSeparator);
double d;
if (double.TryParse(input, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.CurrentCulture, out d))
{
this._tempAnswer = d;
this._answer[controlIndex] = this._tempAnswer;
}
}
}
}
|
|
|
|
|
Hello,
Yustme wrote: foreach (Control textbox in this.Controls)
{
if (textbox is TextBox)
{
Do I have too understand why you are doing that?
Yustme wrote: string input = this._textBox[k].Text;
input = input.Replace(".", decimalSeparator);
You forgot:
input = input.Replace(",", decimalSeparator );
Yustme wrote: if (double.TryParse(input, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.CurrentCulture, out d))
{
this._tempAnswer = d;
this._answer[controlIndex] = this._tempAnswer;
}
When you debug, are you jumping in if statement?
Martin
|
|
|
|
|
Hi Martin,
The foreach statement is to iterate through my array of textboxes i got on my Form.
I removed the line: input = input.Replace(",", decimalSeparator ); since its standard in my configuration (windows: control panel).
When i debug, i get in the body of the if statement yes.
|
|
|
|
|
Hello,
Yustme wrote: The foreach statement is to iterate through my array of textboxes i got on my Form.
But now you are doing it twice, or not
You have the "foreach" and than inside the "for".
I would think, the "for" should do it.
Yustme wrote: I removed the line: input = input.Replace(",", decimalSeparator ); since its standard in my configuration (windows: control panel).
But if you install your project on a second PC, you will have a problem at this point.
Yustme wrote: When i debug, i get in the body of the if statement yes.
That means the conversion to double was good.
So, aren't you happy? Or is there an other problem.
All the best,
Martin
|
|
|
|
|
Hello,
Yustme wrote: for (int k = 0; k < 20; k++)
{
Why "20"?
I would prefere:
for (int k = 0; k < this._textBox[].Length; k++)
All the best,
Martin
|
|
|
|
|
Hi,
I made an eventhandler for handling keys. I no longer handle the . (dot), it drives me crazy.
Thanks for your time and help!
|
|
|
|
|
Yustme wrote: I got an array of 20 textboxes. How can i change this for all the textboxes?
Create a class that dervives from TextBox and do whatever it is you are trying to do in that class. It would be a lot cleaner that way.
|
|
|
|
|
Hi,
Sounds intresting. I never did such thing before.
Do you got a sample about that?
|
|
|
|
|
Hi All,
As i need to pass some values from .net dll to html page. I embedded the .net dll using object tag. As well as i am ablt o pass values from html page to .net dll using PARAM tag. After clicking the button in dll , i want to pass return value to the html page. As i am novice in this, can anyone help me to get resolve the same.
As I need it very urgently, can anyone help me.
Thanks in advance,
Aruna
Aruna
|
|
|
|