|
Actually it's not an overloaded method. All types in the .NET BCL (pointers aside) inherit from System.Object. Since a the listbox wants an object, you can pass literally anything into it, an int, string, bool, whatever.
Now as for anonymous classes, the case you're making for is anonymous objects. To me I don't see a real need; you're defining and instanciating an object in place -- if you really need to instanciate with custom data just use a regular class; less clutter and more concise code that way IMO. But maybe that's just me.
#include "witty_sig.h"
|
|
|
|
|
And why would you want to? As Judah humorously pointed out, there's a much more elegant way of doing it.
C# 2.0 will, however, allow something similar in fashion for delegates. So instead of having to define a method in order to use a delegate (event handler, callback, etc.), you can define it inline, similar to anonymous definitions like you find in Java.
There's things that C# and .NET in general have that Java can't do similarily as well. To each his own.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I want to compare two byte buffer quickly. I used to do this with memcmp in C. I can't find a function to do that in C#.
I'm sure there is one, but I can't find it.
BRCKCC
|
|
|
|
|
There really isn't a way as fast as memcmp , so if you need that kind of performance, you can always P/Invoke it:
using System;
using System.Runtime.InteropServices;
class Test
{
static void Main()
{
byte[] b1 = new byte[10];
byte[] b2 = new byte[10];
for (int i=0; i<b1.Length; i++)
b1[i] = b2[i] = Convert.ToByte(i);
Console.WriteLine("Equals: " + b1.Equals(b2));
Console.WriteLine("Object.Equals: " + Object.Equals(b1, b2));
Console.WriteLine("memcmp: " + Compare(b1, b2));
}
static bool Compare(byte[] b1, byte[] b2)
{
IntPtr retval = memcmp(b1, b2, new IntPtr(b1.Length));
return retval == IntPtr.Zero;
}
[DllImport("msvcrt.dll")]
static extern IntPtr memcmp(byte[] b1, byte[] b2, IntPtr count);
} This would print:
Equals: False
Object.Equals: False
memcmp: True By doing this you sacrific portability, however. If you don't need that kind of performance, use a simple for (not foreach , which is slower) loop and compare each byte using the == equivalency operator.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I'm using the for loop and since I don't want to sacrifice portability, I'll stick with the for loop.
Thanks.
- Bruce
BRCKCC
|
|
|
|
|
Also, if you'd be allowed to use unsafe code, it would be much faster. You'd be able to loop thru the array cast to array of ints and comparing 4 bytes at a time. Plus the CPU works better with int values than single byte values. Of course you'd have to have a special case for padding bytes.
|
|
|
|
|
Do you have a snippet of code on how to do this "casting"?
BRCKCC
|
|
|
|
|
Here is a simple program that tests the comparison using safe and unsafe version. On my machine the unsafe version is about 4 times as fast as safe version.
You have to make sure you allow unsafe code in project properties.
Note, this will only work with arrays whose size is divisble by 4. You can easily change it to handle the last 1, 2 or 3 bytes specially, but i'll leave it as excercise.
using System;
namespace Tester
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
const int repeats = 200;
byte[] bytes1 = randomBytes(4 * 1000000);
byte[] bytes2 = new byte[bytes1.Length];
for (int i=0; i<bytes1.Length; i++)
bytes1[i] = bytes2[i];
int start_t = System.Environment.TickCount;
for (int r=1; r<=repeats; r++)
{
bool res = CompareUnsafe(bytes1, bytes2);
if (r == repeats)
Console.WriteLine("Result: " + res);
}
int end_t = System.Environment.TickCount;
Console.WriteLine("CompareUnsafe: {0} ms.", (end_t-start_t));
start_t = System.Environment.TickCount;
for (int r=1; r<=repeats; r++)
{
bool res = CompareSafe(bytes1, bytes2);
if (r == repeats)
Console.WriteLine("Result: " + res);
}
end_t = System.Environment.TickCount;
Console.WriteLine("CompareSafe: {0} ms.", (end_t-start_t));
}
public static bool CompareSafe(byte[] byteArray1, byte[] byteArray2)
{
if (byteArray1.Length != byteArray2.Length)
return false;
for (int i=0; i<byteArray1.Length; i++)
if (byteArray1[i] != byteArray2[i])
return false;
return true;
}
public unsafe static bool CompareUnsafe(byte[] byteArray1, byte[] byteArray2)
{
if (byteArray1.Length != byteArray2.Length)
return false;
if (byteArray1.Length % 4 != 0)
throw new ArgumentException("Byte arrays have to be divisible by 4", "bytearray");
fixed (byte* bytes1 = &(byteArray1[0]))
fixed (byte* bytes2 = &(byteArray2[0]))
{
int* ints1 = (int*)bytes1;
int* ints2 = (int*)bytes2;
int size = byteArray1.Length / 4;
for (int i=0; i<size; i++)
if (ints1[i] != ints2[i])
return false;
}
return true;
}
public static byte[] randomBytes(int size)
{
Random r = new Random();
byte[] bytes = new byte[size];
r.NextBytes(bytes);
return bytes;
}
}
}
|
|
|
|
|
Thanks very much.
- Bruce
BRCKCC
|
|
|
|
|
Hi All,
I'm calling an unmanaged function (SetScrollPos()) from my C# code and when I try to instantiate the class that uses this method I get a TypeLoadException with the following message:
Additional information: Could not load type Tradition.TraditionDesktop.TraditionDesktopClient.PageMonitor from assembly PageMonitor, Version=1.0.1641.23960, Culture=neutral, PublicKeyToken=null because the method SetScrollPos has no RVA.
What exactly is an RVA ?
|
|
|
|
|
Nevermind figured it out. I forgot to include the DllImport when I was declaring the method.
|
|
|
|
|
You seemed to have figured out your error, but I just wanted to let you know that an RVA is a relative virtual address - it's the memory offset of where in memory a DLL was loaded. Regarding your problem, this is the offset of the exported function from the base address of the native DLL in memory.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thank you Heath. As always you've been very helpful and I appreciate you taking the time to answer my questions.
|
|
|
|
|
Ahoi,
I have some strange problems with an mdi-parent.
When opening a child window, there should normally be either one minimize, maximize and close button for this child window below the ones from the parent window. If i open several of this child-window, those minimize, maximize and close button of the child window suddenly double themselves, but one of those triplets gets inactive and wont disappear even if i close all child windows. It now stays there for the whole period of time the program is running.
Another "funny" thing is, that when i open three windows, the first one i opened gets a z-order between the second one, i opened and the third one, i opened. Additionally one panel that is connected to the window and normally changes during the rotation of the window focus stops changing its appearance and stays with the style, that it got from the second one.
Is there anybode, who could give some tips how to solve these problems?
Thank you very much in advance.
|
|
|
|
|
I created a user control which contains a combobox for table lookups. It basically sets up a DataAdapter and DataSet and binds it to the combobox. For access to the SelectedValue from forms that will use the control I added:
public object selectedValue<br />
{<br />
get { return this.combo.SelectedValue; }<br />
set { this.combo.SelectedValue = value; }<br />
}
The problem that I am having is when I add the user control to form and use simple binding on the property selectedValue, the value isn't updated. It will update if I set it directly. See below.
Works (from a new form)
this.comboSelector.selectedValue = 100;
// The comboSelector will update with the corrisponding data.
Not Working (from a new form)
this.comboSelector.DataBindings.Add(new System.Windows.Binding("selectedValue", dataSet, "table.column"));
I also tried:
this.comboSelector.DataBindings.Add("selectedValue", dataSet, "table.column"));
When the Currency Manager's postion is chaged these values are not updated. What am I missing? Thanks in advance.
Brett Slaski
|
|
|
|
|
"The specified domain either does not exist or could not be contacted" error
the code works fine on my ASPX page but i get the error above when i move the method into my webservice?
my LDAP String = "LDAP://DC=something,DC=org,DC=uk"
|
|
|
|
|
Perhaps you have impersonation enabled for your ASP.NET web site, but you don't have the EnableSession field set in your WebMethodAttribute on the web service method that uses directory services, or your not passing a NetworkCredentials with the proxied call to the XML web service. If the LDAP directory requires authentication, the web service must be told who the user is.
If the LDAP directory does not require authentication, then I really don't know what to tell you since you gave us little information other than that it works in an ASP.NET Page but not a WebService .
More information about where the ASPX and WebService are hosted would be helpful (same ASP.NET web application?) and what resources they may require (authentication and impersonation, etc.).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
thankyou your comments are always welcome. I did not solve the problem it worked fine on my asp page but the dissapointed i could not get it too perform my way. So moved to forms auth instead.
|
|
|
|
|
I have a dataset filled from SqlDataAdapter, and I would like to filter the dataset.
The column I would like to filter is customerID, which have int data type.
I thought I can use :
<br />
int cust_search = Int32.Parse(txt_custsearch.Text.ToString());<br />
ds_customer.Tables["Customer_table"].DefaultView.RowFilter = "CustomerID = 'cust_search'";<br />
I know here cust_search have to be in string to have the code to work properly.
What should I use to filter interger value in my dataset?
Thank you so much in advance.
|
|
|
|
|
Well, for one, don't use .ToString() on a Text property. It's already a String!
int cust_search = Int32.Parse(txt_custsearch.Text);
ds_customer.Tables["Customer_table"].DefaultView.RowFilter = "CustomerID = " + cust_search.ToString();
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
It worked.
Thanks a lot
|
|
|
|
|
Is it possible to filter with wildcard?
I tried:
<br />
cust_name_search = txt_custname_search.Text + "%";<br />
dataview_cust_search.RowFilter = "cust_name LIKE " + cust_name_search;<br />
It comes with error saying that "Missing Operand after Mod operation".
I know that '%' is the wildcard for Sql Command, but what wildcard do I use for dataview rowfilter, and how do I do it?
Thank you very much in advance.
|
|
|
|
|
Strings in SQL must be enclosed in single quotes.
cust_name_search = "'" + txt_custname_search.Text + "%'";
dataview_cust_search.RowFilter = "cust_name LIKE " + cust_name_search;
Also, Wildcard characters, * and %, are supported and are used with the LIKE operator. Wildcards are allowed only at the beginning and/or end of a filter string.
Regards,
Orina
http://orina.org
|
|
|
|
|
Thank you so much, it works perfectly
|
|
|
|
|
Can I actually use LIKE in row filter for interger?
For example I have these following record I want to filter:
1100
1101
1200
1201
1202
1203
1300
1301
When I typed '12' in the textbox, only record with 12 as the beginning appear (1200, 1201, 1202, 1203).
I tried:
<br />
string CustomerID = "'" + txt_customerid.Text + "%'";<br />
dataview_cust_search.RowFilter = "CustomerID LIKE " + CustomerID;<br />
But error showed up saying that I cannot use LIKE on string with integer.
Is there anyway I can do this?
Also, is it possible to filter date?
Please help.
Thank you very much in advance.
|
|
|
|