|
I'm glad I could help!!
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005
|
|
|
|
|
Often I have functions, typically math, formatting, or database, where I'd like to use them in many different places in my application. What is good-form for instantiating and providing scope across many classes?
As a (semi)concrete example:
public class ContainerClass {
public T MuchUsedFunction( args ) {
// foo
}
}
public class A {
private ContainerClass cc = new ContainerClass();
public A_method() {
T result = cc.MuchUsedFunction( my_args );
}
}
public class B {
private ContainerClass cc = new ContainerClass();
public B_method() {
T result = cc.MuchUsedFunction( my_args );
}
}
So here classes A and B both instantiate a ContainerClass. I'd rather not do this.
Also, I'm not too keen on passing a handle to ContainerClass to A and B because, in a scaled-up version, I would eventually be passing many such handles. Finally, I appreciate that I may not want 'global' scope for ContainerClass and would like to know how I can control its scope.
What's the best way to handle this?
Thanks.
|
|
|
|
|
You basicly have two options.
One is to make a singelton class (do a search on CP for tutorials) or use static methods.
In the later case you can call a method like this: ClassName.MethodName()
Of course the second has it's limitations, as you can't use non-static methods of that class...
Q:What does the derived class in C# tell to it's parent?
A:All your base are belong to us!
|
|
|
|
|
A singleton makes 1 instance of the class -- but how do I provide scope to its methods to other classes? I don't see how a singleton solves the pb.
|
|
|
|
|
class Base
{
protected ContainerClass cc = new ContainerClass();
}
class A : Base {}
class B : Base {}
|
|
|
|
|
Is this the best way? Say A hits a database and B does a calculation. My MuchUsedFunction()might convert string formats. When I inherit I typically think of inheriting related properties. But converting a string format is not heuristically related to accessing a dB or doing a calculation. So isn't there a way to reflect this relationship better?
|
|
|
|
|
What i do with frequently used functions is make a class and use static functions, so it wouldnt require an object reference to use them, for instance:
<br />
public class utilility<br />
{<br />
static void MuchUsedFunct1() { }<br />
static double MuchUsedFunct2() { }<br />
}<br />
Then you could access these functions from anywhere in the program.
|
|
|
|
|
Hi, would you pls elaborate a bit. Where do you instantiate utility (utilility is quite creative!) and how do you use it in another class's method?
|
|
|
|
|
You can name it whatever you want, i usually use "utility".
With static methods, you dont need an object reference to call them, so if you have a class with static methods, for instance
<br />
public class test<br />
{<br />
private static string helloworld = "Hello World!";<br />
public static void HelloWorld()<br />
{<br />
MessageBox.Show(helloworld);<br />
}<br />
}<br />
you could call that method from anywhere in the program, like this:
test.HelloWorld();
Since static methods require no object reference, you call them using the class name, instead of a variable name. Also, when using static methods, anything else in the class that they use also has to be static.
Classes with static methods are kind of like modules in VB.net
Hope this helps.
|
|
|
|
|
This helps very much. Does 'protected' rather than 'public' provide scope for static methods as it does for non-static methods?
|
|
|
|
|
nyc_user wrote: This helps very much.
Glad to help.
nyc_user wrote: Does 'protected' rather than 'public' provide scope for static methods as it does for non-static methods?
Yes. For instance:
protected static string teststring = "A String";
|
|
|
|
|
Again, thank you for your help today.
-nyc_user
|
|
|
|
|
Have you tried declaring the function you want to be able to use everywhere
as a public static.
i.e.
public static void doThisEveryWhere()
{
...
}
You can now call this function from any of your other classes.
I usually have a common functions class called common.cs and I define all my common functions in it. And as it is static you don't have to create a common object to reference the function.
You would just type:
common.doThisEveryWhere();
Hope this helps.
|
|
|
|
|
hi guys,
i have a client/server application and i need to create installers for both the applications. i have a database (MSSQL2000) connected in my serverApp, how can i create an installer that includes the hostname of the PC(Server) that it is being installed.
|
|
|
|
|
Hi ,
my application is 1 EXE file only. and i want the user to run it.
if the user do not have Framework i want the installation to get it from the internet.
but again i dont want it to be web based.. just 1 file (windows installer/CAB..whatever.. EXE too) that the user run and it will download framework if needed and then run my EXE (my program).
again,no installation is needed (except from framework).
Thnaks,
Ran.
R.Z
|
|
|
|
|
Hello,
I want to know that how can i 'Print' textbox and richTextbox values in Windows Forms using C# against "Click" Event of Button?? I like to show Crystal Report and not too, I want both solution please reply me soon through some tutorial OR Sample Code I will be really grateful to u.
Regards,
Abubakar
|
|
|
|
|
Add a PrintDocument Control to your Windows Form from Toolbox and name it "printDocumentLetter", and
your print button name : "btnPrintLetter"
your textbox name : "txtMyTextBox"
Add PrintPage event handler of printDocument Control (printDocumentLetter) named "printLetter".
Now,
private void btnPrintLetter_Click(object sender, EventArgs e)
{
printDocumentLetter.Print();
}
private void printLetter(object sender, PrintPageEventArgs e)
{
String text2Print = txtMyTextBox.Text;
Font printFont = new Font("Verdana", 12);
e.Graphics.DrawString(text2Print, printFont, Brushes.Black, 10, 10);
}
|
|
|
|
|
Hi there.
I'm created a borderless window, on which I put 8 panels containing my custom window border. I want the window to be resized when the user clicks+ drags from the right border. I created 3 event handlers for the right border panel...mousedown, mouseUp and mouseMove. On mouseDown, a flag is set and I get the location of the click. On MouseUp I unset the flag. On mouse move I do the following:
<br />
if (this.resizeRDown) <br />
{<br />
int Xdiff;<br />
Xdiff = e.X - this.ResizeRXY.X;<br />
this.Width = this.Width + Xdiff;<br />
} <br />
But the resizing is acting very weird...making the window a single pixel wide.
Can anyone give me a hint on this(or an article for that matter) that can help out.
Thanks
------------------------------------------------------------------------------
Programming......THE DEVINE GIFT!
|
|
|
|
|
What does the debugger say when you examine e.X and ResizeRXY.X? What values do they have at runtime?
Maybe you just calulate them wrong.
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Well, that's what I actually prompted me to write here. I discovered that hovering was affecting this so I handled mousehover, so that it would unset the flag. It improved, but still acting kind of funny.
Well...for XDiff..the value is small.
What I want to know is whether e.X, e.Y (mouse location) is the coordinate with respect to the top,left (first location of window) or with respect to 0,0 of screen?
--------------------------------------------------
Programming...the golden price!
|
|
|
|
|
Depends on what e is. If it's a point from Cursor.Position , then MSDN says: A Point that represents the cursor's position in screen coordinates.
But you can use the PointToClient method to convert them into client coordinates.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Hi,
the code seems good at first glance but some parts are missing to clear this out. I assume you are setting ResizeRXY not correctly. Be beware of the affect that the X and Y coordinate of the MouseEventArgs are relative to the control which generated the event in relation to its parent (to the whole screen when working with a Form).
I quickly put together the following (where panel1 is a Panel docked to the right of a Form - but it shouldn't matter what kind of control it is):
private Point _resizePoint;
private void panel1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
_resizePoint = new Point(e.X, e.Y);
}
private void panel1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
_resizePoint = Point.Empty;
}
private void panel1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (_resizePoint != Point.Empty)
{
this.Width += (e.X - _resizePoint.X);
}
}
|
|
|
|
|
Hi, Thanks. Helped a lot.
--------------------------------------------
Programming is a devine gift
|
|
|
|
|
Hi
I am beginner at C#.
I need to create two classes which will represent two picture models(like JPG and bitmap).It should look something like this
class JPGImage
{
..............
}
class BMPImage
{
..............
}
I don't know how to describe this classes.
In winForm I need 2 panels where I'll put this 2 models.
Can you help me with this?
Thanks
|
|
|
|
|
It depends what you want to do.
In .NET, you use the Bitmap class to handle not only BMP images but also GIF, PNG and JPG - so from most points of view there would be no difference as to what members you would use to describe them.
|
|
|
|