|
Both == and Equals perform a reference comparison rather than value comparison.
Rewrite your code like this and find out what happens:
object[] obj1={1,"hello"};
object[] obj2=obj1;
|
|
|
|
|
If you are wanting to compare the contents of the arrays, then consider the following code:
public static bool CompareArrays<T>(T[] data1, T[] data2)
{
if ((data1 == null) && (data2 == null))
{
return true;
}
if ((data1 == null) || (data2 == null))
{
return false;
}
if (data1.Length != data2.Length)
{
return false;
}
for (int i = 0; i < data1.Length; i++)
{
if (!data1[i].Equals(data2[i]))
{
return false;
}
}
return true;
}
|
|
|
|
|
|
Better post this in General Database forum.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
Hi;
I'm trying to create a server whose client-handler gets a tcpclient-object and needs to read data.
My problem is: when using the networkstream.read(byte[], offset, length) method, you cant really tell when the client has stopped sending a block.
I tried using while(length == buffer.length) to check if the buffer has been completely filled, meaning there is still data on the stream, but that does not include the possibility that the client has send exactly the amount of bytes the buffer can hold. Then i tried using the DataAvailable-property of the stream, but how does this work when the clients for example sends two different objects, which both happen to have the same size as my buffer? I would read the first object but there would still be data available on the stream since the second object was send right after the first one.
Could someone please point me in the right direction for receiving a variable amount of data representing a custom class / structure over the internet?
MfG Frank
Edit:
Thank you Paulo Zemek for the hint.
On the client side, just serialize the object on the network-stream:
private void sendAction(GameEvent action)<br />
{<br />
BinaryFormatter formatter;<br />
<br />
formatter = new BinaryFormatter();<br />
formatter.Serialize(this.Server.GetStream(), action);<br />
}
The server side was a little bit tricky, as I first had to create a new binder for the formatter before deserializing:
using System;<br />
using System.Reflection;<br />
using System.Runtime.Serialization;<br />
<br />
public class Binder : SerializationBinder<br />
{<br />
public override Type BindToType(string assemblyName, string typeName)<br />
{<br />
return Type.GetType(typeName + ", " + Assembly.GetExecutingAssembly().FullName);<br />
}<br />
}
The deserialization can then be done quite easy:
private void handleClient(Object tcpclient)<br />
{<br />
GameEvent action;<br />
BinaryFormatter formatter;<br />
TcpClient client;<br />
<br />
formatter = new BinaryFormatter();<br />
formatter.Binder = new Binder();<br />
client = (TcpClient) tcpclient;<br />
<br />
while (client.Connected)<br />
{<br />
try<br />
{<br />
action = (GameEvent) formatter.Deserialize(client.GetStream());<br />
this.newAction(client, action);<br />
}<br />
catch (Exception)<br />
{<br />
<br />
}<br />
}<br />
this.Clients.Remove(client);<br />
}
Thank you again for the great help.
MfG Frank
|
|
|
|
|
A "block" (object that has been serialized) needs to be clearly defined so that when a client receives an array of bytes it is capable of determining where each block in that array begins and ends. For raw bytes streams the start and stop flags should be a byte (or a sequence of bytes) that can't naturally occur inside of the serialized object; that way there's no way to have "accidental" start and stops in the stream.
|
|
|
|
|
If in one side you do:
binaryFormatter.Serialize(connectionStream);
and in the other side you do:
binaryFormatter.Deserialize(connectionStream);
The BinaryFormatter itself will know when it finished receiving the object.
You can also:
Instead of serializing directly, serialize to a MemoryStream. Then, you first send the length of the serialized data (for example, as an WriteLine) and then the data. And, on the other side, you first receive the length of the data, and then creates a buffer and read it to the end. But I really think you don't need that.
|
|
|
|
|
I'm trying something similar to this:
http://msdn.microsoft.com/en-us/library/ms838165.aspx[^]
I used that code to handle de editing:
private void grdOrders_CurrentCellChanged(object sender,
System.EventArgs e)
{
if (!inUpdateMode)
{
if (inEditMode && !grdOrders.CurrentCell.Equals(editCell))
{
inUpdateMode = true;
grdOrders.Visible = false;
DataGridCell currentCell = grdOrders.CurrentCell;
grdOrders[editCell.RowNumber, editCell.ColumnNumber] =
txtEdit.Text;
grdOrders.CurrentCell = currentCell;
grdOrders.Visible = true;
inUpdateMode = false;
txtEdit.Visible = false;
inEditMode = false;
}
editCell = grdOrders.CurrentCell;
txtEdit.Text = (string)grdOrders[editCell.RowNumber,
editCell.ColumnNumber];
Rectangle cellPos = grdOrders.GetCellBounds(editCell.RowNumber,
editCell.ColumnNumber);
txtEdit.Left = cellPos.Left - 1;
txtEdit.Top = cellPos.Top + grdOrders.Top - 1;
txtEdit.Width = cellPos.Width + 2;
txtEdit.Height = cellPos.Height + 2;
txtEdit.Visible = true;
inEditMode = true;
}
}
But I'm having a problem. I dont know what to put on Form1 Design so I'm getting the error:
txtEdit does not exist on current context.
Can you please help me ?
|
|
|
|
|
From the page you referenced:
When a cell is tapped (both inEditMode and inUpdate mode is false), the current cell is saved (editCell). Then, the hidden TextBox (txtEdit) gets the current cell contents, is repositioned to the location of the current cell, and is made visible (over the current cell). When editing is complete and another cell is selected, the event code is disabled (inUpdateMode set to true) and the new cell is saved (currentCell) while the edited cell gets updated from the TextBox control. During the update the grid is not updated (Visible set to false), and when the update is complete the TextBox is hidden again.
If you want to do things the way they did, you need to create the hidden TextBox control named txtEdit on your form.
CQ de W5ALT
Walt Fair, Jr., P. E.
Comport Computing
Specializing in Technical Engineering Software
|
|
|
|
|
I need to display information from MS SQL table, and I need to have edit, insert and delete options.
What should I use? ListView? DataGrid..? Or another ?
|
|
|
|
|
You should use DataGrid. It has edit, insert and delete function itself.
|
|
|
|
|
hisen wrote: You should use DataGrid. It has edit, insert and delete function itself.
Thanks! Should I save information with SQL Server CE or XML ?
Can you please point me to information where I can learn to do those operations?
|
|
|
|
|
I think you needn't. But it depend on your project specfic request.
|
|
|
|
|
|
|
Hello,
Can anyone explain me how to build ListView from SQL Query? (MS SQL Server)
|
|
|
|
|
Hi all,
I have an xml file that I want to make a small app to present its contents in a convenient way to the user. Nothing especially fancy is needed here - a pane for a treeview so the user can select what he or she wants to look at, a panel for displaying the selection(s), and the facility to load the file in the first place.
I'm thinking this would be a good time for me to get familiar with linq2xml (linq noob here), but wanted to see if there were other equally-or-better good routes to go as well.
Thx!
-sff
|
|
|
|
|
sherifffruitfly wrote: a pane for a treeview
Why not simply load the whole XML document into the TreeView?
|
|
|
|
|
No major reason - I just think of treeview data situations a 2-paned affairs is all. Possibly that's an out-moded frame of mind.
I was more interested in alternative methods of getting the data to the app in the first place though, I can fiddle with it once it's there in a variety of ways.
|
|
|
|
|
Hello, I do not know why it is happening, but sometimes DateTime.Substract returns either 0 or 1, while parameter have same values
public bool bCodeChange1;
public bool bCodeChange2;
private DateTime RemoveTime(DateTime time)
{
time = time.AddHours(-time.Hour);
time = time.AddMilliseconds(-time.Millisecond);
time = time.AddMinutes(-time.Minute);
time = time.AddSeconds(-time.Second);
return time;
}
private void dateTimePickerDateDeadLine_ValueChanged(object sender, EventArgs e)
{
DateTime dtDateDeadLine = dateTimePickerDateDeadLine.Value;
DateTime dtDateReciet = dateTimePickerDateReciet.Value;
dtDateDeadLine = RemoveTime(dtDateDeadLine );
dtDateReciet = RemoveTime(dtDateReciet );
if (dtDateRecit >= dtDateDeadLine )
{
MessageBox.Show("DeadLine need to be at least one day ahead of Reciet", "Račun", MessageBoxButtons.OK);
dateTimePickerDateDeadLine.Value = dtDateReciet.AddDays(1);
return;
}
if (bCodeChange2)
{
bCodeChange2 = false;
return;
}
bCodeChange1 = true;
numericUpDownDaysTillDeadLine.Value = dtDateDeadLine.Subtract(dtDateReciet).Days + 1;
}
private void numericUpDownDaysTillDeadLine_ValueChanged(object sender, EventArgs e)
{
if (bCodeChange1)
{
bCodeChange1 = false;
return;
}
bCodeChange2 = true;
numericUpDownDaysTillDeadLine.Value = dateTimePickerDateReciet.Value.AddDays(Convert.ToDouble(numericUpDownDaysTillDeadLine.Value));
}
I am so puzzled. Thanks goes in Advance.
PS: If variables names missmatches, you do not need to point out, because i nearly translated variable names, so that you can understand.
|
|
|
|
|
Saksida Bojan wrote: numericUpDownDaysTillDeadLine.Value = dtDateDeadLine.Subtract(dtDateReciet).Days + 1;
In your example above this should return zero, as the Subtract() method returns -1; dtDateDeadLine is smaller (i.e. less than) dtDateReciet . If the dates are equal then Subtract() will return zero giving a final answer of 1.
|
|
|
|
|
Saksida Bojan wrote: i nearly translated variable names
not a good idea in general
One problem you have is DateTime contains time up to a single tick, that's a tenth of a microsecond.
To strip the time, just use DateTime.Date
In general, when in trouble with a particular class, read the first 3 MSDN pages about that class, including the list of methods, properties and events.
|
|
|
|
|
Thank you, it worked like a charm.
|
|
|
|
|
you're welcome.
|
|
|
|
|
Good afternoon.
I was wondering if there is a more efficient way to write this code:
if (cboSP_Site.SelectedIndex == -1 && inttmpSP != -1)
{
WebBrowser1.GoBack();
}
else if (cboWS.SelectedIndex == -1 && inttmpWS != -1)
{
WebBrowser1.GoBack();
}
else if (cboFolder.SelectedIndex == -1 && inttmpF != -1)
{
WebBrowser1.GoBack();
}
I want to call the WebBrowser1.GoBack(); once if possible.
Thank you, WHEELS
|
|
|
|