|
Yeah, to pass a reference of anything into a method, just use the ref keyword.
However, arrays are reference types themselves. When you pass it into a method, it's passing a pointer to the real data:
byte[] myBytes = { 5, 10, 15 };
FillItAllWith7s(myBytes);
...
void FillItAllWith7s(byte[] bytesToModify)
{
for(int i = 0; i < bytesToModify.Length; i++)
{
bytesToModify[i] = 7;
}
}
Notice how I didn't even need to use the ref keyword; passing any class type (AKA reference type) into a function simply passes a pointer to that object. Passing any struct type (AKA value type, including int, double, float, etc.) will pass a copy of the struct instance to the function. This is when the ref keyword really comes in handy.
However, you can certainly use the ref keyword for parameters that are classes/reference types, as you just did in your above example. This allows you to actually change the reference itself:
byte[] myBytes = {1, 2, 3};
SetToNull(ref myBytes);
...
void SetToNull(ref byte[] bytesToModify)
{
bytesToModify = null;
}
To summarize, if you're just changing the data of the class passed in as a parameter to a function, you don't need the ref keyword. In fact, I think I've used the ref keyword -- for reference type parameters -- on functions maybe 2 or 3 times in my 500k line project at work. I've used the ref keyword many times for value type parameters.
The only time you *need* the ref keyword on classes/reference types passed in as parameter is if you need to actually modify what the object points to. In your case, if you needed to change the byte[] to point to something else (that is, a new byte[], or a null), then you'd use the ref keyword. Otherwise, you can omit the ref keyword and pass in the array if you just want to manipulate that array (i.e. change the array's elements). Make sense?
|
|
|
|
|
Has anyone found a way to put hyperlinks to web pages in a help file? I have tried using <summary><see cref="http://www.codeproject.com">CodeProject</see></summary> but it doesn't work. I have tried <seealso> but this doesn't work either.
Chris
|
|
|
|
|
I'm not sure on Sandcastle, but it worked in NDoc so it probably works in Sandcastle is to use XHTML.
So your summary becomes:
<summary><a href="http://www.codeproject.com">CodeProject</a></summary>
|
|
|
|
|
<summary><a href="http://www.codeproject.com">CodeProject</a></summary> doesn't work. Thanks for the try. Anyone else have any possibilities?
Chris
|
|
|
|
|
Chris
I believe that you need to use <summary><see href="http://www.codeproject.com">CodeProject</see></summary>
cref refers to items that are in the docs that you are building, and anything that is external has to be href .
Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world."
Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that."
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
I tried it with an h and no luck.
Chris
|
|
|
|
|
I need to read the extended/summary properties of an mp3 file. What class .net class would be used for this? I have been searching the internet trying to find examples but I have had no luck.
Thanks,
Patrick
|
|
|
|
|
|
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
|
|
|
|
|