|
so i am writing a simple app to get the hang of .net and winforms... if i write some common functions as part of my "program" class how can i call them from a form? in mfc if was easy to get a ref to the app object and call from there but so far im not fining the equivalent here
any help appreciated
"mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"
|
|
|
|
|
If i understand your question properly you can just use static functions, no?
My opinion is... If someone has already posted an answer, dont post the SAME answer
|
|
|
|
|
I wasn't sure about his question either, is he trying to execute code from an event on a form or..?
|
|
|
|
|
Not sure, from the question thou I gather its just calling a method within the Program class, which itself is a static class.
Personally if I have alot of common functions such as conversion algorithms or something, i just have a static 'Function' and call them whenever and whereever needed
My opinion is... If someone has already posted an answer, dont post the SAME answer
|
|
|
|
|
thats kinda what i'm doing and was wondering if thats "ok in .net" so to speak
"mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"
|
|
|
|
|
Yeah, I dont see why you shouldnt do it.
But as i said, prob best in another static class and leave the main Program class to do just what it needs, of course if you ever need login or splash screens, then you might be adding to the Program class
My opinion is... If someone has already posted an answer, dont post the SAME answer
|
|
|
|
|
sweet! thnx a lot
"mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"
|
|
|
|
|
Dont do any coding in the "Program" class for now.
The "Program" class has the Main method which in turn runs an instance of your Form.
Application.Run(new Form1());
The above line takes the control to the constuctor of Form1
public Form1()
{
InitializeComponent();
}
So if you write any method you can run them here for a start
public Form1()
{
InitializeComponent();
MyMethod()
}
|
|
|
|
|
Hi,
all methods are part of one class or other; there are no global methods.
A public static method is called by ClassName.MethodName(parms) .
A public non-static method is called by MyClassInstance.MethodName(parms) .
You normally pass MyClassInstance to whomever is interested, and/or you define an interface (which promises availability of some methods, but does not specify a particular class implementing them),
or, best of all, you pass a delegate (that is how you keep dependencies low).
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Friday, June 10, 2011 11:39 AM
|
|
|
|
|
got it thnx ... i was still in web world (where i've been for too long it seems) where most member functions are public by default ... these were private by default so making them public worked
"mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"
|
|
|
|
|
so a question about general approaches...
in a desktop app (winforms say) is it better to open and close database connections with each form as it loads / unloads OR is it better to open a connection when the app starts and leave it open for the duration?
this is a low volume scenario with no more than 50 users expected simultaneously fyi
thnx
"mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"
|
|
|
|
|
You could do either. I think the general approach is to create a connection each time because of something called Connection Pooling which is a cache of connections.
Either way, you want to seperate your DB calls from you GUI code - this is a good abstraction. Wrap up the DB access stuff in a Data Access Layer.
Regards,
Rob Philpott.
|
|
|
|
|
ok cool thnx ... when you say separate the db calls do you mean the open / close connection stuff or even further and use a whole class for the data calls for each form?
my approach would be to use stored procs / views and call them from the code ... is that not good?
thnx again
"mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"
|
|
|
|
|
Well, I'd be inclined to create a seperate project in your solution which provides class(es) which do the data access stuff. This project would reference System.Data, System.Data.SqlClient etc.
Your form code would reference this project and would not have any references to System.Data. This way, you seperate presentation from data acccess. This way if you change one data access function you only need to do it in one place and not in different forms etc.
The next 'step' would be to have some sort of client server architecture where the forms apps would talk to a server which in turn would forward the class on to the database, but this may be heavy-handed for what you want to do.
Regards,
Rob Philpott.
|
|
|
|
|
so a project is just a collection of classes?
"mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"
|
|
|
|
|
A project is a collection of classes, but it's also more than that. Typically in Visual Studio, a Solution is a collection of projects. Each project would normally be compiled into an assembly (which is a DLL).
It's normal to break up an application in this way. You might have your front end in its own project. Maybe you've got some custom controls, they'd be in another project (so that they could be reused elsewhere). You might have some business functionality in another project, and certainly data access would be another one. All these seperate projects comprise your solution.
When you build your solution, each project is compiled into its own assembly and they are all placed in the same folder. (This is private deployment if you want to look it up).
Projects obviously need to know about each other, and this is where references come in. If you right click on one in VS, there's an add reference option. Here you can reference your other projects and also system wide assemblies from the GAC.
Hope that makes some sense.
Regards,
Rob Philpott.
|
|
|
|
|
As mentioned by Rob, there is a Connection Cache in ADO.NET. They are cached based on the connection string. So as long as you keep your connection strings the same, there is almost no overhead closing your connections and reopening them.
|
|
|
|
|
thnx
"mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"
|
|
|
|
|
Hi all !
i'm trying to do something with one of my button. I got a logon form and i want to change de "visible" status tu true if the logon is accepted. The button is in the main form so i tried something like this :
<br />
if (str33 == encpassword)<br />
{<br />
PassTextBox.Clear();<br />
<br />
ChangeButton.Show();<br />
<br />
Main open = new Main();<br />
open.ChauffButton.Text = "dfg";<br />
<br />
<br />
}<br />
but it dosent worked. I jsut dont get it. Theres no error, nothing seem to be wrong but the status dont change. I even tried to change de text and got no result
Can someone help me on this please ?
BTW i'm using VS express 2008
Thanks !
|
|
|
|
|
from your code i'm not sure which bit isn't working or what you want to work?
"mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"
|
|
|
|
|
l a u r e n wrote: from your code i'm not sure which bit isn't working or what you want to work?
the
Main open = new Main();
open.ChauffButton.Text = "dfg";
part.
the text dosent change and if i change this by "open.ChauffButton.Visible = true;" it's not working either
|
|
|
|
|
|
Ok, you need to pick up a beginners book on C#. Firstly, Welcome to CP, check out the post at the top for some etiquette, specifically on "text speak", everyone hates it. It's the only universal truth. Now without knowing which control is what, and judging off your hungarian notation (naming variables based off their control type) I'm going to assume the following...
Firstly, that this code is executed on a button press.
Secondly str33 = sometextbox.Text which has a password in it
Thirdly, that encpassword is a plaintext password
Fourthly, that PassTextBox is a textbox
Fifthly, that Changebutton is a button that you wish to show ONLY when the password is correct
Sixthly, that open should pop up if the password is correct
Now this goes against all convention of not posting code, but this is just one that needs dire attention.
I'm going to go line by line and address some issues
if (str33 == encpassword)
{
PassTextBox.Clear() should be PassTextBox.Text = "" or you can use PassTextBox.Enabled = false or PassTextBox.Visible = false , they will all do some sort of desired effect, clearing the textbox, disabling it or simply wiping it off the form
ChangeButton.Show() should be ChangeButton.Visible = !ChangeButton.Visible assuming Visibility is set to false (which I assume because you are trying to show it)
Main open = new Main();
you have initialized a form here, but you never showed it
open.Show(); is what you need
From here open.ChauffButton.Text should work, I don't really remember because I never access controls like this, consider placing this code elsewhere like on the load event of your form 'open'.
Those modifications, and you'll be off to a start.
modified on Tuesday, February 3, 2009 11:44 AM
|
|
|
|
|
thanks for your answer EliottA
i will take notes of all you say
i realize theres a lot of infos i didn't wrote so that was not easy to help me. I will do better next time.
I will try to find a way to make it small and clear and understandable for all of you (i will find a good language translater LOL)
btw, i didn't get my answer (as i say, i realize you hadd no chance to understand what i try to do) but i will work on a way to put it more clear and more in standard form
|
|
|
|
|
Your answer is in there, I threw in the code you will need to make it work, infact I gave you multiple options based off the multiple assumptions I had to make. Most importantly, you forgot open.show() to show the form!!
|
|
|
|