|
Here is some information about pointers in C#. A class is a reference type, and as such, it is always allocated on the heap, with a pointer to it on the stack. A struct us a value type, and as such, it is always allocated directly on the stack. You can force something to be passed by reference using the 'ref' modifier in a parameter. For classes, this will pass the original pointer. This contrasts with the normal method, which passes a copy of the original pointer. For structs, using ref will create a pointer to the struct data on the stack. This contrasts with the normal method, which passes a copy of the whole struct.
class MyClass
{
public MyClass(int number)
{
Number = number;
}
public int Number;
public override string ToString()
{
return Number.ToString();
}
}
struct MyStruct
{
public MyStruct(int number)
{
Number = number;
}
public int Number;
public override string ToString()
{
return Number.ToString();
}
}
public class Program
{
public static void Main()
{
MyClass myClass = new MyClass(10);
MyStruct myStruct = new MyStruct(5);
Console.WriteLine("Original Values:");
Console.WriteLine(myClass);
Console.WriteLine(myStruct);
TestNorm(myClass, myStruct);
Console.WriteLine("After first test:");
Console.WriteLine(myClass);
Console.WriteLine(myStruct);
TestRef(myClass, myStruct);
Console.WriteLine("After second test:");
if (myClass == null)
Console.WriteLine("myClass is null);
else
Console.WriteLine(myClass);
Console.WriteLine(myStruct);
}
// This function gets a copy of the pointer to myClass
// And gets a copy of the whole myStruct struct
// Modifying myClass modifies the same actual data in memory
// as the Main() function modifies.
// Modifying myStruct modifies only the local copy.
public static void TestNorm(MyClass myClass, MyStruct myStruct)
{
myClass.Number = 20;
myStruct.Number = 10;
Console.WriteLine("First Test:");
Console.WriteLine(myClass);
Console.WriteLine(myStruct);
myClass = null;
}
// This function gets the original pointer to myClass
// And gets a pointer to the original myStruct data on the stack
// Modifying myClass modifies the same actual data in memory
// as the Main() function modifies.
// Modifying myStruct modifies the original data on the stack.
public static void TestRef(ref MyClass myClass, ref MyStruct myStruct)
{
myClass.Number = 35;
myStruct.Number = 15;
Console.WriteLine("Second Test:");
Console.WriteLine(myClass);
Console.WriteLine(myStruct);
myClass = null;
}
}
The output of this program will be:
Original Values:
10
5
First Test:
20
10
After first test:
20
5
Second Test:
35
15
After second test:
myClass is null
15
-- modified at 13:54 Tuesday 8th November, 2005
|
|
|
|
|
Hello!
I have a problem in Visual Studio .NET 2003 IDE.
Actually I am creating a User Control named 'MyDateBox' by inheriting
"Microsoft Masked Edit Control" (COM component)
Visual Studio displays the property of this control without any message when I create an instance of this control on a Form If i don't add any property to this control.
But when I add any property to this control (i.e. DateSeparator as string), and draw an instance on any Form. Visual Studio displays the following error in the MessageBox when I call the property window for this control.
"An Exception has occured while painting the label for property 'DateSeparator'. See below for details"
I hope somebody has the solution
Please help!
|
|
|
|
|
Gulfraz Khan wrote: "An Exception has occured while painting the label for property 'DateSeparator'. See below for details"
I hope somebody has the solution
someone might, but without the details we don't have any idea what you're doing wrong.
|
|
|
|
|
Hi!
I think I have stated my problem in detail. but for your reference let me state it again.
I am trying to create a custom control by inheriting the Masked Edit Control.
All is well when I do not add any property to this control's class.
But when I add any read/write property (i.e. MinDate) Visual Studio does not display the properties window of my custom control when I use this control in any project (property window does not display MinDate property and the value and label for this property becomes black in the property window.
Is their something wrong with inheriting a COM control (which I am trying to Inherit for my User control)?
I hope you understand my problem this time.
Thank You
|
|
|
|
|
You've stated what you're trying to do but not the details of the error. IN the error dialog you need to copy out the details it lists below and post them here.
|
|
|
|
|
Hi dan!
I am sorry I forgot to mention that the message is shown in a typical message box and it has no details below this message.
|
|
|
|
|
Hallo!
has someone tried the minihttpd?
I did and it working fine sofar from my PocketLOOX.
But is I tried to download from a iPAQ it doesn't work?
does soneone have any sugestion for me?
thanks you
sayangoin
|
|
|
|
|
Hi!
You should ask this question in the comments section of the article itself, the generic C# forum isn't the right place.
Regards,
mav
|
|
|
|
|
ok got a similar problem to earlier post but im trying to get images
this is what ive got
if (contentType.StartsWith("image/"))
{
byte[] imageBuffer = new byte[reader.BaseStream.Length];
reader.BaseStream.Read(imageBuffer, 0, (int)reader.BaseStream.Length);
Debug.WriteLine("Sending " + contentType + " back to client");
this.m_socket.Send(imageBuffer);
}
but this is not good beacause the underlying stream doesnt support this operation.
the problem i am trying to solve is this. my application is a proxy for browsing http, all is good except when i encounter an image tag like
|
|
|
|
|
What is your "reader" object's type? What is the underlying stream? I highly doubt that the underlying stream doesn't support the Read operation.
Picture a huge catholic cathedral. In it there's many people, including a gregorian monk choir. You know, those who sing beautifully. Then they start singing, in latin, as they always do: "Ad hominem..."
-Jörgen Sigvardsson
|
|
|
|
|
It is using WebResponse stream. i think it was the peek operation it doesnt support.
regards,
g00fy
|
|
|
|
|
I can't see where you used the peek operation in your code, and it is highly unlikely that the stream wouldn't support .Read. A bit more explanation of how your doing things would help.
|
|
|
|
|
sorry here is the entire code.
internal void Run()
{
string message = "";
string uriString;
Encoding encoding;
try
{
byte[] read = new byte[1024];
int bytes = this.m_socket.Receive(read);
message = Encoding.ASCII.GetString(read);
if (bytes < 1)
{
return;
}
HttpHeaderParser parser = new HttpHeaderParser(message);
uriString = parser.RequestUri;
Debug.WriteLine("Received request to page: " + uriString);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriString);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Debug.WriteLine("Received response ... OK");
try
{
encoding = Encoding.GetEncoding(response.ContentEncoding);
}
catch
{
encoding = Encoding.GetEncoding(1252);
}
WebHeaderCollection headers = response.Headers;
string contentType = headers[HttpResponseHeader.ContentType];
Debug.WriteLine(" ### Content Type: " + contentType);
StreamReader reader = new StreamReader(response.GetResponseStream());
if (contentType.StartsWith("image/"))
{
byte[] imageBuffer = new byte[reader.BaseStream.Length];
reader.BaseStream.Read(imageBuffer, 0, (int)reader.BaseStream.Length);
Debug.WriteLine("Sending " + contentType + " back to client");
this.m_socket.Send(imageBuffer);
}
else
{
Debug.WriteLine("reading page content");
string content = reader.ReadToEnd();
Debug.WriteLine("Content read, length is: " + content.Length);
byte[] buffer = new byte[content.Length + 1];
Debug.WriteLine("Sending " + contentType + " back to client");
int length = encoding.GetBytes(content, 0, content.Length, buffer, 0);
this.m_socket.Send(buffer, length, 0);
}
response.Close();
reader.Close();
this.m_socket.Shutdown(SocketShutdown.Both);
this.m_socket.Close();
}
catch (Exception e)
{
Debug.WriteLine(" ***** Exception: " + e.Message);
}
}
thank you
g00fy
|
|
|
|
|
Hi im trying to concatenate three cells in a database table using this code but when any of the three cells are null the entire name is returned as null im just wondering if anyone knows anyway to solve this or any ideas on how to avoid it. Thanks in advance
select p.firstName + ' ' + p.MiddleName + ' ' + p.LastName as Name
from SquadPlayerMapping spm
join Squad s on s.ID = spm.squadid
join Person p on p.id = spm.playerid
where dateentered <= GetDate()
and dateleft is null
and s.ID = 11
|
|
|
|
|
tadhg88 wrote:
but when any of the three cells are null the entire name is returned as null
That is by design. Any operations you carry out on a NULL value will return NULL. You will have to manually extract each field from the DB, check if they are NULL in your C# program, and accordingly perform the concatenation.
HTH!
Cheers,
Vikram.
http://www.geocities.com/vpunathambekar
Google talk: binarybandit After all is said and done, much is said and little is done.
|
|
|
|
|
IsNull[^]
You select becomes
select ISNULL(p.firstName,'') + ' ' + ISNULL(p.MiddleName,'') + ' ' + ISNULL(p.LastName,'') as Name
from ...
|
|
|
|
|
Hi
I want to set the File Access (e.g. FileAccess.Read
) attribute in my C# program. I found the System.IO.File.SetAttributes() Function but it seems not to be the right thing.
I also do not want to use the FileStream constructor.
Is there another way?
Thanks
|
|
|
|
|
Can you be more descriptive on what "it seems not to be the right thing" means? Keep in mind these are just access level permissions. NTFS will have another level that will alter the behavior.
|
|
|
|
|
I work with a file in my program. But I want to avoid that the file is modified outside of my application by another user. So I thought to make it write protected would be a solution. Do you have another recommendation?
Thanks!
|
|
|
|
|
Hi,
is there an example, how to download a (msi) file from a web-site (via http) to the local harddisk in C#?
Thanks!
Ariadne
|
|
|
|
|
Use HttpWebRequest[^] to issue the request for the MSI file, this will be returned in a HttpWebResponse[^] object. You can then stream the data to the local disk.
My: Blog | Photos
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious
|
|
|
|
|
Thanks Colin,
I did this:
<br />
myReq=HttpWebRequest)WebRequest.Create("http://www.xyz.ch/file.msi");<br />
myResponse = myReq.GetResponse();<br />
ReceiveStream = myResponse.GetResponseStream();<br />
FileStream frStream = new FileStream(ReceiveStream, FileAccess.Read);<br />
FileStream fwStream = new FileStream(Application.StartupPath+"file.msi",FileMode.Create,FileAccess.Write);<br />
But this does not work: ReceiveStream is not System.IO.Stream
Ariadne
|
|
|
|
|
If I recall correctly, you need to pass the ResponseStream to a StreamReader . You can then read the contents of the stream to a byte array and then write the array that to your output file stream. The response stream will tell you how large you need to make the byte array (of course, you can always make it smaller and retrieve the information in chunchs by using multiple reads)
My: Blog | Photos
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious
|
|
|
|
|
Yes Colin, the canonical way works! thanks.
Ariadne
|
|
|
|
|
have you tried the WebClient class?
WebClient w = new WebClient();
w.DownloadFile(address_str,file_str);
//if you want to hold the file only in memory, try
// w.DownloadData() //for parameters check msdn
this is maybe a lot simpler than working with HttpWebRequest objects
best regards
j.
|
|
|
|