|
Well, thanks anyway for the answer! Meanwhile I've joined this forum, because it's one of the best I've seen.
Ludwig
|
|
|
|
|
Gday All,
I have a performance /design question regarding whether it
is better to implement helper functions for a class as
static members or as global/namespace functions.
For example:
namespace MyObjCtrl {
class MyObject {
public:
//static member that uses a MyObject ( option 'A' )
static HelperStyleA( MyObject* pObj );
};
//namespace helper function of option 'B'
HelperStyleB( MyObject* pObj );
};
//implementation of option 'B'
MyObjCtrl::HelperStyleB( MyObject* pObj )
{
}
If anyone could give me a run down on the pros and cons of
these two methods or of any website or tutorial that might
help, it would be much appreciated
|
|
|
|
|
This is actually a C++ question, but I'll answer it.
There is not performance issue here just a stylistic one. I personally prefer static functions over global functions as they imply a link to a given context.
Cheers,
Tom Archer
Author, Inside C#
|
|
|
|
|
I created a collection class using the .NET help files, but when I add objects to the collection, it appears as though the each new object copies itself throughout the collection--all objects in the collection become equal to the new added object. Anyone run into this before?
Here's a snip of code to give you a idea of what I'm doing...
namespace NatesKentuckyRook.Classes
{
public class RookCards : System.Collections.CollectionBase
{
// Constructors
public RookCards()
{
}
// Functions
public void Reset()
{
List.Clear();
}
public void AddCard(RookCard Card)
{
List.Add(Card);
}
}
}
|
|
|
|
|
Take a look at CollGen2 on GotDotNet. It will create a collection for you using the type you specify.
As to your problem I've not seen it before.
James
Sonork: Hasaki
"I left there in the morning
with their God tucked underneath my arm
their half-assed smiles and the book of rules.
So I asked this God a question
and by way of firm reply,
He said - I'm not the kind you have to wind up on Sundays."
"Wind Up" from Aqualung, Jethro Tull 1971
|
|
|
|
|
There's nothing in the code you posted that would cause the problem you're describing. Can you post a sample app - especially the client code?
Cheers,
Tom Archer
Author, Inside C#
|
|
|
|
|
I want to know how can I send command via socket to another computer on my LAN like take a screenshot, send a message, Controls the computer, something with the same concept as Netbus but for my LAN monitoring ?
Thanks
P.S Source code will be very appreciated
|
|
|
|
|
I just wanna transfer a piece of code from cpp code to csharp.
To my surprisse, csharp doesn't have the union key word.
Then how can cope with the union in cpp to keep the code change minimized.
any suggestion?
|
|
|
|
|
C# is a type safe language, and a union is every thing that a typesafe language is not.
Therefore you will have to create a conversion operator or function to cast from type A to type B.
|
|
|
|
|
If you need to interop with a C/C++ object that uses a union you can do that, but there isn't a way to create a union in C# that behaves the same way (well easily create a union, you can re-create the VT_VARIANT type by using an enum and a lot of casting).
James
Sonork: Hasaki
"I left there in the morning
with their God tucked underneath my arm
their half-assed smiles and the book of rules.
So I asked this God a question
and by way of firm reply,
He said - I'm not the kind you have to wind up on Sundays."
"Wind Up" from Aqualung, Jethro Tull 1971
|
|
|
|
|
Well, I try to use set and get to simulate the union. It seems work in my eye.
class A
{
public int a;
}
class B
{
public char b;
}
class union
{
object obj;
public A aa
{
get
{
return (A)obj;
}
set
{
obj=value;
}
}
public B bb
{
get
{
return (B)obj;
}
set
{
obj=value;
}
}
}
|
|
|
|
|
Can a component obtain a reference to its container object?
I'm writing a component that needs two pieces of information about its container:
1. Is the container a form?
2. If so, what is its window handle? (.Handle property)
The "Container" object only lets me access a very limited interface that doesn't do what I want.
--Thanatos
|
|
|
|
|
You should be able to test the Container object for what type it is, then once you know it is a specific type you can cast it to that type and access the property you want.
ie
if( Container is System.Windows.Form ) {<br />
System.Windows.Form containerForm = (Form) Container;<br />
}
HTH,
James
Sonork: Hasaki
"I left there in the morning
with their God tucked underneath my arm
their half-assed smiles and the book of rules.
So I asked this God a question
and by way of firm reply,
He said - I'm not the kind you have to wind up on Sundays."
"Wind Up" from Aqualung, Jethro Tull 1971
|
|
|
|
|
That sounded good, so I tried this test:
I built a simple component with this method:
public void ChangeParentColor()
{
if( Container is Form )
{
Form containerForm = (Form)Container;
containerForm.BackColor = Color.Aqua;
}
}
This built just fine. I placed this component on a new form, along with a button. When the button was clicked, this method fired:
private void buttonclick(object sender, System.EventArgs e)
{
containerTestComponent1.ChangeParentColor();
button1.Text="I was clicked";
}
This project also built just fine, but even though the caption of the button changes properly (demonstrating that the button event fired), the color of the form refused to change to aqua.
|
|
|
|
|
While i'm not going to offer any advice in the container issue... I just thought you might like to know that that code is not the best way to do what you are doing.
It's better* to do this:
Form containerForm = Container as Form;
if( containerForm != null ) {
// Use containerForm
}
*
"better" meaning that the produced IL is smaller and faster.
If you don't believe me, make a simple executable and examine the IL yourself.
The general rule-o-thumb is:
If you are going to do a cast directly after an "if-is" and use that casted object, use "as" instead.
If you just want to test the type and not do something with the object as that type, use "is".
|
|
|
|
|
Hmm...
I haven't done any tests to see if (Container is Form) evaluates to true; I just assumed it would if the container was a form. If it doesn't, that could be the reason why the code isn't running.
Your method may work where the other did not. I'll try it out when I get home.
|
|
|
|
|
Nope, it doesn't work. Upon further inspection, ( containerForm != null ) is evaluating as false, indicating that something is wrong with the typecasting.
I'm thinking it may not be possible to cast a Container to a Form...
...in which case I'll have to do things the hard way: pass the form to the component as a parameter. I'd rather not, though, if I don't have to.
|
|
|
|
|
why don't you do a:
Type containerType = Container.GetType();
and see what the container really is?
|
|
|
|
|
In addition to what Andy wrote your best bet is to code for the lowest common denominator. If you only need to use properties/methods that a control has, then compare to System.Windows.Forms.Control.
The container may not be a form but it could be a Panel, or a UserControl or some other class.
James
Sonork: Hasaki
"I left there in the morning
with their God tucked underneath my arm
their half-assed smiles and the book of rules.
So I asked this God a question
and by way of firm reply,
He said - I'm not the kind you have to wind up on Sundays."
"Wind Up" from Aqualung, Jethro Tull 1971
|
|
|
|
|
I thought about that after I had written the code, but I assumed that 'as' was implemented as Container is Form ? (Form) Container : null
Apparently I made an ass of myself :-P
James
Sonork: Hasaki
"I left there in the morning
with their God tucked underneath my arm
their half-assed smiles and the book of rules.
So I asked this God a question
and by way of firm reply,
He said - I'm not the kind you have to wind up on Sundays."
"Wind Up" from Aqualung, Jethro Tull 1971
|
|
|
|
|
well, don't think too badly of yourself.
From the SDK Docs:
-----------------------
The as operator is like a cast except that it yields null on conversion failure instead of raising an exception. More formally, an expression of the form:
expression as type
is equivalent to:
expression is type ? (type)expression : (type)null
except that expression is evaluated only once.
-----------------------
so it is logically exactly as you claim... but the actual IL implementation is different, and not describable with c#... which explains the requirement of the new "as" keyword.
|
|
|
|
|
ah, maybe thats why I thought that
James
Sonork: Hasaki
"I left there in the morning
with their God tucked underneath my arm
their half-assed smiles and the book of rules.
So I asked this God a question
and by way of firm reply,
He said - I'm not the kind you have to wind up on Sundays."
"Wind Up" from Aqualung, Jethro Tull 1971
|
|
|
|
|
Does anyone know what the equivalent would be in C#? System.Drawing.Graphics does not seem to have a means of doing what I want, which is a recangle with rounded corners.
|
|
|
|
|
Charles Petzold's Programming Microsoft Windows with C# has an example demonstrating how to create one.
Unfortunately the code from the book isn't available on his website, nor MSPress' website so I can't give you his exact code.
But I don't think there would be any harm in giving you the algorithm since it is common sense when you think about it
First break up your rounded rectangle up into its distinct sections. You have the four sides, and 4 rounded parts. The rounded parts if you put them together form an ellipse. So all you have to do to create your own rounded rect function is to use the DrawArc and DrawLine methods of the Graphics object to position each part where it needs to be.
HTH,
James
Sonork: Hasaki
"I left there in the morning
with their God tucked underneath my arm
their half-assed smiles and the book of rules.
So I asked this God a question
and by way of firm reply,
He said - I'm not the kind you have to wind up on Sundays."
"Wind Up" from Aqualung, Jethro Tull 1971
|
|
|
|
|
I have written an article showing how it's done, but as the other poster said, it's pretty obvious. I provide a function that allows you to set % rounded, just like GDI did.
Christian
The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
"I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?!
- Jon Hulatt, 22/3/2002
|
|
|
|