|
Does anybody know how I can get the SQL Connection DialogBox to show up in C#?
You know, the one that comes up when you open SQL query analyzer. I think that I need to use Microsoft.Data.ConnectionUI which I need to install another SDK to use. Does anybody know which SDK this is?
Any help would be appreciated
Thanks
There are 10 types of people in the world, those who understand binary and those who dont.
|
|
|
|
|
I'm looking for a way to represent an instantiated class as a byt array (and populate an instantiated class frm a byte array. I was hoping that [Serializable] would be the magic bullet but it seems I was wrong - or do I just not understand how to use [Serializable] properly?
Let's say I have:
public class myClass
{
public byte myByte1;
public byte myByte2;
public ushort myUshort1;
public ushort myUshort2;
public myClass()
{
this.myByte1 = 0x01;
this.myByte2 = 0x02;
this.myUshort1 = 0x0304;
this.myUshort2 = 0x0506;
}
}
Now, if I instantiate this class with myClass myInstance = new myClass() the member variales will hold the values as defined by the constructor. I'd like to be able to view the contents of the class as an array of bytes (ie. {0x01, 0x02, 0x03, 0x04, 0x05, 0x06} ).
So I was thinking that if the class is serializable and I write the seraialized stream to a byte array (or write a byte array to the associated stream) I'd be able to pull it off, something like this:
MemoryStream memStream = new MemoryStream();
BinaryFormatter binFormatter = new BinaryFormatter();
binFormatter.Serialize(memStream, myInstance);
byte[] myBytes = memStream.ToArray();
But myBytes is 183 bytes in size (as opposed to the expected 6 bytes) once this is finished and the contents of myBytes makes no sense to me whatsoever.
Am I on completely the wrong track here or is there hope?
|
|
|
|
|
Here are many additional information is added with serialization like assembly name, version, class name, structure, ........
All this information is necessary to deserialize this object
|
|
|
|
|
Aha, that makes sense. So does that mean that [Serializable] would not be the proper route then?
Would you have any other suggestions?
|
|
|
|
|
Dewald wrote: Aha, that makes sense. So does that mean that [Serializable] would not be the proper route then?
Yes, if the array size is not critical.
If critical you have to realize you own convertion like:
<br />
public class myClass<br />
{<br />
public byte myByte1;<br />
public byte myByte2;<br />
public ushort myUshort1;<br />
public ushort myUshort2;<br />
<br />
public myClass()<br />
{<br />
<br />
}<br />
<br />
public static byte[] ToByteArray(myClass value)<br />
{<br />
byte[] result = new byte[6];<br />
<br />
result[0] = value.myByte1;<br />
result[1] = value.myByte2;<br />
unchecked<br />
{<br />
result[2] = (byte)(value.myUshort1 / 256);<br />
result[3] = (byte)(value.myUshort1 - result[2] * 256);<br />
result[4] = (byte)(value.myUshort2 / 256);<br />
result[5] = (byte)(value.myUshort2 - result[4] * 256);<br />
}<br />
<br />
return result;<br />
}<br />
<br />
public static myClass FromByteArray(byte[] array)<br />
{<br />
myClass result = new myClass();<br />
<br />
result.myByte1 = array[0];<br />
result.myByte2 = array[1];<br />
result.myUshort1 = (ushort)(array[2] * 256 + array[3]);<br />
result.myUshort2 = (ushort)(array[4] * 256 + array[5]);<br />
<br />
return result;<br />
}<br />
}<br />
P.S. usort is 16-bit type so we need to store it into two bytes.
|
|
|
|
|
Yip, this is exactly what I've been doing up until now but I was just hoping that there wuld be a more elegant sollution. The thing is that I have various classes that I'd like to be able to pack into byte arrays (basically the classes are just messages that I need to transmit and receive over a TCP socket).
I was hoping that I could write a base class that provides a method of packing the class into an array and then derrive all the other classes from that class. That way I wouldn't have to implement a huge amount of tedious code to pack every individual member variable of the class into the array.
|
|
|
|
|
Sorry, I have no ideas how to help you this case.
If you message is more than 6 byte the volume of valuable information will be more. Also you may try to zip it by DeflateStream....
|
|
|
|
|
Dewald, last night I got an idea: in unmanaged C++ we just need to look at the object as byte array:
<br />
MyClass myCl = new MyClass();<br />
byte[] array = meCl;
But in .Net we haven't references (that is very good, in my opinion), but we have some classes that allow to work with objects by references way.
One of them is System.Runtime.InteropServices.Marshal
I have never worked with, just herad about it.
I have being looking through this and find some pairs of static methods wich can help you (I guess):
To get IntPTR from your object:
public static extern void StructureToPtr(object structure, IntPtr ptr, bool fDeleteOld);
public static object PtrToStructure(IntPtr ptr, Type structureType)
To copy to byte array:
public static void Copy(IntPtr source, byte[] destination, int startIndex, int length);
public static void Copy(byte[] source, int startIndex, IntPtr destination, int length)
Or
public static extern byte ReadByte(object ptr, int ofs);
public static extern void WriteByte(IntPtr ptr, int ofs, byte val);
Regards.
P.S. IntPtr (intPointer) is Net analog of C++ pointers
-- modified at 2:48 Saturday 9th June, 2007
|
|
|
|
|
I've done the copying for value object (structure). One thing I can't make is to get a pointer (IntPtr) from reference object (class).
<br />
public struct myStuct<br />
{<br />
public byte myByte1;<br />
public byte myByte2;<br />
public ushort myUshort1;<br />
public ushort myUshort2;<br />
<br />
private const int MY_SIZE = 6;<br />
public static byte[] ToByteArray(myStuct source)<br />
{<br />
IntPtr pointer = System.Runtime.InteropServices.Marshal.AllocHGlobal(MY_SIZE);<br />
System.Runtime.InteropServices.Marshal.StructureToPtr(source, pointer, false);<br />
<br />
byte[] result = new byte[MY_SIZE];<br />
System.Runtime.InteropServices.Marshal.Copy(pointer, result, 0, MY_SIZE);<br />
<br />
return result;<br />
}<br />
<br />
public static myStuct FromByteArray(byte[] source)<br />
{<br />
if (source == null) throw new ArgumentNullException("Source array can not be null");<br />
if (source.Length != MY_SIZE) throw new ArgumentException("Source array's size has to be " + MY_SIZE + " bytes");<br />
<br />
IntPtr pointer = System.Runtime.InteropServices.Marshal.AllocHGlobal(MY_SIZE);<br />
System.Runtime.InteropServices.Marshal.Copy(source, 0, pointer, MY_SIZE);<br />
<br />
myStuct result = (myStuct)System.Runtime.InteropServices.Marshal.PtrToStructure(pointer, typeof(myStuct));<br />
<br />
return result;<br />
}<br />
}<br />
|
|
|
|
|
Thanks for all the effort AikinX,
I also discovered this approach but, like you, I couldn't get it to work for classes. I never even bothered to try using it for structures (silly of me) and this will do nicely.
It is a bit of a pity that I have to resort to unmanage code. I would have prefered if there was a way that I could do this without having to use pointers, and there probably is which is still elluding me.
Either way, for now, this will do. Thanks again.
|
|
|
|
|
I don't like pointers too. But it is the only way to do the serialization in general for any class.
So, I think you have to choose built-in serialization or do it manually.
Best Regards
|
|
|
|
|
When I initialize NumericUpDown, how do I set the Value , which is outside the range-it is not allowed by the control.
F.x.:
NumericUpDown nud=new NumericUpDown();
nud.Min=10;
nud.Max=20;
nud.Value=30;//this will throw ArgumentOutOfRangeExaption !!!, but I want to allow this assignment and cancel or ignore the exaption
Is there any way to do that?
I also need the Min and Max to be set, so the user knows the bounds.
|
|
|
|
|
The control won't let you set the value outside the bounds.
You can catch the exception and ignore it, if you like, but that hardly serves any purpose, as the value still won't be set.
What is the reason that you want to set the value outside the bounds?
---
single minded; short sighted; long gone;
|
|
|
|
|
It is not my idea
But I was asked to do it.
Maybe there is the way to cancel the check of the bounds performed by the control, somehow ?
|
|
|
|
|
Nope. There's no way to get the value outside the bounds, unless you increase. the Max bound.
You'd have to create your own version of the control to get any additional functionality.
So, say you ARE successful at getting the value outside the bounds. Why 30? What is supposed to happen when the user types a value into the text box? What if he/she clicks on the Up and Down buttons? If the value supposed to shoot down into the valid range?
If you need some kind of flag denoted by the value 30, why not just replace that with a checkbox? If the box is checked, the numeric control gets disabled and a flag is set. If not, the numeric is re-enabled and the flag is reset.
|
|
|
|
|
I know, I know
All these thought were the first things that came to my mind when I've got this assignment, but for some reason they whant it to be done this way. Eventually, offcourse, we will do it as you are saying, but first I have to proove that it is not possible to impliment it the way they want
So, any ideas on the stated problem?
|
|
|
|
|
julgri wrote: but for some reason they whant it to be done this way.
Managers... can't live with 'em, can't shoot 'em.
|
|
|
|
|
Maybe I can unsubscribe from the eventhandler that checkes this Value? Is it possible? Just I do not know which event is handling this, but I have a feeling that I know the function, which handles this. Any ideas?
|
|
|
|
|
You never subscribed to one in the first place, so no, you can't unsubscribe from it. Also, this isn't done by an event handler. The check is done inside the Value, Minimum, and Maximum properties. No events are fired to trigger this check.
|
|
|
|
|
Are you sure? If it is so, than there is really no way around it. Good !
How do you know that? I could not find such a precise description anywhere.
|
|
|
|
|
Go get Lutz Roeder's .NET Reflector and look at the code in the NumericUpDown class yourself!
|
|
|
|
|
julgri wrote: Is there any way to do that?
You can inherit this class to realize this feature for derived class.
julgri wrote: I also need the Min and Max to be set, so the user knows the bounds.
How a user can understand the Min and Max when he CAN input more or less?
|
|
|
|
|
I want to convert a string to byte array as follows
<br />
byte[] b = Convert.FromBase64String(s);
it is giving the following error:
Invalid length for a Base-64 char array.
can any one give me a suggestion to solve this.
Best Regards,
M. J. Jaya Chitra
|
|
|
|
|
I guess you could use the Encoding class (System.Text).
Encoding encoding = Encoding.UTF8;
byte[] b = encoding.GetBytes(s);
But you have to use the right encoding.
Hope this helps.
|
|
|
|
|
For Conversion of String to Byte Array go like this,
public static byte[] StrToByteArray(string str)
{
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}
Don't go with Base 64 because it is of invalid length.
Because of an unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.
Regards,
Satips.
|
|
|
|