|
I understand how to write an implicit conversion to in32, but I don't get how to write an implicit conversion from int32 to my class.
Currently I came up with something like that:
public struct MyId
{
public const int kServer = 0;
public const int kBroadcast = -1;
public override bool Equals(object obj)
{
return mId.Equals(obj);
}
public override string ToString()
{
return mId.ToString();
}
public override int GetHashCode()
{
return mId.GetHashCode();
}
public static implicit operator int(MyId id)
{
return id.mId;
}
private int mId;
}
The C# reference says operator = cannot be overloaded.
What I my missing so far?
Is it useful to overload GetHashCode/ToString/Equal?
TIA
R/
|
|
|
|
|
Funny, I could not find any reference to Stephen Toub in GotDotNet, yet I found a nice team page for Eric and his article on MSDN regarding operator overloading
Anyway, I am in the right direction when I say that:
- it's a C++-programmer reflex of mine to expect to be able to overload operator=()
- in the case above, I actually don't need such an operator=, what I need is an operator from int to MyId, such as:
public static implicit operator MyId(int id)
{
MyId v = new MyId; v.mId = id; return v;
}
R/
|
|
|
|
|
To "override" the = operator, all u need to do is an implicit cast
<a TITLE="See my user info" href=http:
|
|
|
|
|
Here's how you overload the conversion from int to your class:
public static implicit operator MyId(int id)
{
return new MyId(id);
}
|
|
|
|
|
As siad by Eric your best best is to make a continer class. Stephen Toub from MS has written an execellent (as all his stuff) utility for this. Search GotDotNet for stoub (his username).
<a TITLE="See my user info" href=http:
|
|
|
|
|
leppie wrote:
Search GotDotNet for stoub (his username).
I had to look on his blog just to find his username...it's toub...not stoub.
Hawaian shirts and shorts work too in Summer.
People assume you're either a complete nut (in which case not a worthy target) or so damn good you don't need to worry about camouflage...
-Anna-Jayne Metcalfe on Paintballing
|
|
|
|
|
I am having a couple of problems setting form size at run-time.
First problem is when I first show the form, it sets the size of the form to 148x40 when I specify the width less than 148 or hight less than 40, they reset to 148 and 40 respectively.
It happens during handle creation time and I can't figure out where the form gets the 148x40 values. I happens somewhere in Control's constructor.
I tried overriding DefaultSize property but the size still defaults to 148x40 if I specify smaller dimentions.
The workaround I use is to force handle creation and then set the size of the form after.
Second problem is that when I set the height of the form and then show the form, the form adds extra height during handle creation time if I have a main menu specified.
Once again the workaround is to set the size after the handle has been created.
However I would like to know if I am missing something and if there is a better way of fixing these problems. Perhaps it is a bug in the form creation logic.
I wish I could see the source of the library so I knew what it was doing.
|
|
|
|
|
Did you look in the InitializeComponent method? That's where the forms designer does its initialization.
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi
|
|
|
|
|
Yes, I did.
148x40 does not come from there.
|
|
|
|
|
Acidis wrote:
I wish I could see the source of the library so I knew what it was doing.
You can always use ILDasm to see the IL or use Anakrino or Reflector to decompile see C#, MC++, or VB.NET versions of the IL.
As to your specific problem I haven't seen that before, just doing a quick test I can resize my forms all the way down to 109w 28h which is the minimum size allowed by Windows. Depending on your Appearance settings, your minimum may differ.
James
"I despise the city and much prefer being where a traffic jam means a line-up at McDonald's"
Me when telling a friend why I wouldn't want to live with him
|
|
|
|
|
I might have missed a few details.
Here is the set up of my form. I call it ToolTipForm.
Properties that are different from default:
FormBorderStyle: None
AutoScale: False
Size: 100, 50
StartPosition: Manual
ShowInTaskbar: False
The form contains only one Lavel control.
Label properties:
BorderStyle: FixedSingle
TextAlign: MiddleCenter
UseMnemonic: False
Dock: Fill
Now, if you create a main form and add some way to display the ToolTipForm. USe the following code to show it:
<br />
ToolTipForm ttf = new ToolTipForm();<br />
ttf.Size = new Size(50, 18);<br />
ttf.Show();<br />
The first time you display this form (when it creates the handle) it will set the size of the form to 148x40 (in my case.)
Given the form set up, it should not do it.
If I set the Size property to 50, 18 after the Show() method, it sets the size to 50, 18 without any problems.
The reason I am using the form is because I need my tool tip to be displayed on top of my main window (form) so it is not clipped. I don't think the tooltip control is capable of doing it. And I was unable to make a control display as a top level window. Controls seem to require a parent.
As to decompiling, I am not sure how to do it. I have the Reflector but it does not allow me to decompile anything in the library. I am probably not understanding how to do it properly. Will have to look at it some more.
|
|
|
|
|
Acidis wrote:
I don't think the tooltip control is capable of doing it.
Using .NET 1.1 the ToolTip control isn't getting clipped by the child main window or by child windows. I don't know if this is something that changed between 1.0 and 1.1 though.
Acidis wrote:
I have the Reflector but it does not allow me to decompile anything in the library. I am probably not understanding how to do it properly. Will have to look at it some more.
A new version was released recently that added decompiling to the list of features. When you find a method you want to see the source to, you can press Enter to see the IL or Space to see the IL->C# or IL->VB.NET conversion.
http://www.aisto.com/roeder/dotnet[^]
James
"I despise the city and much prefer being where a traffic jam means a line-up at McDonald's"
Me when telling a friend why I wouldn't want to live with him
|
|
|
|
|
I red the page for this menu, and I found it very interesting, so I downloaded the code, but I can’t find any sample, and couldn’t figure out how to use it. Has anyone already integrated this program with existing codes? What I’d like to do is for each form, make a XML menu description, and make an object of this class, and call a method by passing the XML file to make a menu for the form. Any help would be greatly appreciated.
|
|
|
|
|
Send me your email address and I'll send you an example, from which you can rape and pillage whatever code you need. In the meantime, I'll check that article's download source.
Marc
Help! I'm an AI running around in someone's f*cked up universe simulator. Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus Every line of code is a liability - Taka Muraoka Microsoft deliberately adds arbitrary layers of complexity to make it difficult to deliver Windows features on non-Windows platforms--Microsoft's "Halloween files"
|
|
|
|
|
Marc,
Thanks a lot man, I really appreciate, and your code is really great. It's
exactly what I was looking for, and precisely what MS should make in the
future version for the windows application menus. anyway my email address as
you can see is Emmanuel_Soheyli@Hotmail.com
Thanks again and keep up the nice work that you're doing.
Emmanuel
|
|
|
|
|
Hello there,
I'm getting crazy.
Maybe somebody of you guys have some good ideas how to solve this.
Imagine, There is an existing API in C++.
The main idea of this interface is to exchange byte arrays with a server.
In the example below you see the principle.
I create e.g. a object myCommand from the 'Command' struct which again consists of enumns and structs.
The idea is to to send these binary data of the enumns and structs down to the server.
In c++ i create this object and send it like that:
Socket.send(&myCommand, sizeof(blabla));
As you see i just need to give a pointer to my data.
I decided to translate this interface to C#, since writing a wrapper would be about the same work(?!).
Anyway, the biggest problem i have is:
How can i create a byte[] from myCommand object, containg all the numerical enum and struct data to be able to send it to the server???
Sockets in C# are only accepting byte[] as parameter, but how to convert 'myCommand Object' to byte?
There are dozens of examples how to decode strings to byte arrays but I did not found any material abot converting my stuff!
One thing i thought about is to use serializing, but since the future app should run on a PocketPC on the Compact Framwork,
this is not possible since the .NET CF is not supporting serializing......
Any help, ideas tips and tricks would be greatly appreciated!
regards,
stonee
<br />
enum DataType<br />
{<br />
Command = 0,<br />
Error = 1,<br />
Result = 2,<br />
};<br />
<br />
enum Command<br />
{<br />
Exit = 0, <br />
GetValueA = 1, <br />
GetValueB = 2, <br />
GetValueC = 3, <br />
GetValueD = 4, <br />
};<br />
<br />
<br />
struct Header<br />
{<br />
public long lPacketSize;<br />
public DataType type;<br />
}<br />
<br />
<br />
struct Command<br />
{<br />
public Header packetHeader;<br />
public Command command;<br />
}<br />
<br />
<br />
<br />
<br />
Command myCommand = new Command();<br />
<br />
Socket.send(&myCommand, sizeof(blabla));
|
|
|
|
|
If you can't use serialization, your best option is to use unsafe. You can do:
byte[] buffer = new byte[sizeof(Command)];
fixed (Byte* pBuffer = buffer)
{
*((Command*) pBuffer) = command;
}
socket.Send(buffer, buffer.Lenth);
|
|
|
|
|
thanks a lot for this idea, should look like below about?
The problem is the compiler states:
The type or namespace name 'Command' could not be found
(are you missing a using directive or an assembly reference?)
Sorry, can you bring me in the right direction please?
And what means fixed?
thanks again,
stonee
public unsafe override bool Send(void* Command, long lSize) <br />
<br />
{ <br />
System.Console.WriteLine("Send() called!");<br />
<br />
try<br />
<br />
{<br />
byte[] buffer = new byte[lSize];<br />
fixed (Byte* pBuffer = buffer)<br />
{<br />
*((Command*) pBuffer) = Command;<br />
}<br />
m_socket.Send(buffer, buffer.Length,SocketFlags.None);<br />
<br />
return true; <br />
<br />
<br />
}<br />
catch(SocketException se)<br />
{<br />
System.Console.WriteLine(se.Message); <br />
return false; <br />
}
|
|
|
|
|
Two ways to do this:
1) Pass in the real type:
Send(MyStruct s)
{
...
// inside fixed...
*((MyStruct) pBufer) = s;
2) pass in a byte* pointer, and pass things across directly. If you do this, you'll need to have the fixed statement in the calling routine, so that you can get the pointer.
|
|
|
|
|
Sorry, I guess I act as stupid but I still havent' understood 100% how to go.........
1) Can I pass void* PacketStart, long PacketSize or Do I need to go the managed way?
2) Are those two method declarations equivalent?
3) If i pass void*, what would be the substitute for MyStruct?
4) *((object) pBuffer) = PacketStart; ( results in Cannot convert type 'byte*' to 'object')
5) Could you give an example how to go, to receive the respective byte[] in this specific case?
your help is greatly appreciated!!
regards,
newbie stonee
public unsafe override bool SendPacket(void* PacketStart, long PacketSize) <br />
{ <br />
System.Console.WriteLine("Virtual SendPacket() called!");<br />
<br />
try<br />
<br />
{<br />
byte[] buffer = new byte[PacketSize];<br />
<br />
fixed (Byte* pBuffer = buffer)<br />
{<br />
<br />
*((MyStruct) pBuffer) = PacketStart; <br />
<br />
}<br />
m_socket.Send(buffer, buffer.Length,SocketFlags.None);<br />
<br />
return true; <br />
<br />
<br />
}<br />
<br />
catch(SocketException se)<br />
{<br />
System.Console.WriteLine(se.Message); <br />
return false; <br />
} <br />
}
|
|
|
|
|
I'll try to be clearer:
1) Can I pass void* PacketStart, long PacketSize or Do I need to go the managed way?
You need to get from the the managed world to the pointer world somehow to use the pointer operations. If you pass in a MyStruct (for example), you would do the fixed statement in your routine. If you want to pass in a pointer, you'd have to do the fixed statement in the caller of the routine.
While you'll have to write a separate routine for each struct, I think that writing the managed version and putting all the ugliness in your routine is the rigth
2) Are those two method declarations equivalent?
No, though they are similar. I'm not sure the object version is feasible, though I've never tried it myself.
3) If i pass void*, what would be the substitute for MyStruct?
If you pass a byte* (on reflection, I don't think void* will work), you would need to copy the bytes over one by one to the byte buffer, based on the length you'd pass in.
4) *((object) pBuffer) = PacketStart; ( results in Cannot convert type 'byte*' to 'object')
Object won't work here, because the compiler does't know how big the actual object is.
5) Could you give an example how to go, to receive the respective byte[] in this specific case?
Here's some code I wrote up. It compiles, but I haven't actually tested it:
using System;
using System.IO;
using System.Net.Sockets;
namespace ConsoleApplication7
{
struct MyStruct
{
int i;
float j;
}
///
/// Summary description for Class1.
///
class Class1
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
}
public unsafe void SendMyStruct(MyStruct myStruct, Socket socket)
{
byte[] buffer = new byte[sizeof(MyStruct)];
fixed (byte* pBuffer = buffer)
{
*((MyStruct*)pBuffer) = myStruct;
}
socket.Send(buffer);
}
public unsafe MyStruct ReadMyStruct(Socket socket)
{
byte[] buffer = new byte[sizeof(MyStruct)];
socket.Receive(buffer);
MyStruct myStruct;
fixed (byte* pBuffer = buffer)
{
myStruct = *((MyStruct*)pBuffer);
}
return myStruct;
}
}
}
|
|
|
|
|
Hi,
I'm building an application where I'd like to put icons in resources. However as I don't use VS.net but the free IDE SharpDevelop, this is not quite so simple. I created a new resource file by the name SharpPrivacyMain.resource with the Resourcer and saved it in the same directory as SharpPrivacyMain.cs.
In SharpPrivacyMain.cs I added the followin code:
<br />
MessageBox.Show(String.Join("\n\r", Assembly.GetExecutingAssembly().GetManifestResourceNames()));<br />
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SharpPrivacyMain)); <br />
this.bmpMnuClipboard = ((Bitmap)resources.GetObject("$this.menuClipboard"));<br />
I get only get to resources.GetObject, then a MissingResourceException is thrown. The messagebox shows me "SharpPrivacyMain.resources", so it seems that the resource was correctly compiled into the assembly. And the Bitmap $this.menuClipboard is also in the resource file. When I doubleclick it in the resource editor of SharpPrivacy, I can even see the image.
I don't know if the problem comes from the IDE, or if I just can't write working programs .
Any help would be really appretiated!
The exact Exception message is:
[thread 0xd04] Unhandled exception generated: (0x04a96490) <System.Resources.Mis
singManifestResourceException>
_className=<null>
_exceptionMethod=<null>
_exceptionMethodString=<null>
_message=(0x04a96508) "Could not find any resources appropriate for the specif
ied culture (or the neutral culture) in the given assembly. Make sure "SharpPri
vacyMain.resources" was correctly embedded or linked into assembly "SharpPrivacy
".
baseName: SharpPrivacyMain locationInfo: SharpPrivacy.SharpPrivacyMain resourc
e file name: SharpPrivacyMain.resources assembly: SharpPrivacy, Version=0.1.0.0
, Culture=neutral, PublicKeyToken=null"
_innerException=<null>
_helpURL=<null>
_stackTrace=(0x04a9685c) array with dims=[96]
_stackTraceString=<null>
_remoteStackTraceString=<null>
_remoteStackIndex=0x00000000
_HResult=0x80131532
_source=<null>
_xptrs=0x00000000
_xcode=0xe0434f4d
Thanks in Advance,
Daniel
|
|
|
|
|
Man, I play around with this &$§& for half a day and 5 minutes after I finally post the question, I find the answer:
It has to be
System.Resources.ResourceManager resources = new System.Resources.ResourceManager("SharpPrivacyMain", Assembly.GetExecutingAssembly()); <br />
instead of
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SharpPrivacyMain)); <br />
Could maybe someone explain the difference between those two to me?
Greets,
Daniel
|
|
|
|
|
The first yeilds this:
public ResourceManager(string baseName, Assembly assembly)
{
StackCrawlMark mark1;
base..ctor();
if (baseName == null)
{
throw new ArgumentNullException("baseName");
}
if (assembly == null)
{
throw new ArgumentNullException("assembly");
}
this.MainAssembly = assembly;
this._locationInfo = null;
this.BaseNameField = baseName;
this.CommonSatelliteAssemblyInit();
mark1 = 1;
this._callingAssembly = Assembly.nGetExecutingAssembly(mark1);
if ((assembly == typeof(object).Assembly) && (this._callingAssembly != assembly))
{
this._callingAssembly = null;
}
}
The second:
public ResourceManager(Type resourceSource)
{
StackCrawlMark mark1;
base..ctor();
if (resourceSource == null)
{
throw new ArgumentNullException("resourceSource");
}
this._locationInfo = resourceSource;
this.MainAssembly = this._locationInfo.Assembly;
this.BaseNameField = resourceSource.Name;
this.CommonSatelliteAssemblyInit();
mark1 = 1;
this._callingAssembly = Assembly.nGetExecutingAssembly(mark1);
if ((this.MainAssembly == typeof(object).Assembly) && (this._callingAssembly != this.MainAssembly))
{
this._callingAssembly = null;
}
}
Maybe I'm not reading it right, but both of them look identical. Not quite sure why the second one didn't work...
Hawaian shirts and shorts work too in Summer.
People assume you're either a complete nut (in which case not a worthy target) or so damn good you don't need to worry about camouflage...
-Anna-Jayne Metcalfe on Paintballing
|
|
|
|
|
If C# is Microsoft's answer to SUN's Java,
then is C# Platform independent? Is C# more
similar to Jave to C++ or the opposite?
|
|
|
|
|