Click here to Skip to main content
15,894,314 members
Home / Discussions / Windows Forms
   

Windows Forms

 
GeneralRe: hi everyone! Pin
Shameel19-Jan-12 19:09
professionalShameel19-Jan-12 19:09 
AnswerRe: hi everyone! Pin
ProEnggSoft18-Feb-12 5:53
ProEnggSoft18-Feb-12 5:53 
Questionc# Winforms with classes. Pin
Cliffordagius29-Dec-11 6:56
Cliffordagius29-Dec-11 6:56 
AnswerRe: c# Winforms with classes. Pin
Eddy Vluggen29-Dec-11 8:57
professionalEddy Vluggen29-Dec-11 8:57 
AnswerRe: c# Winforms with classes. Pin
Luc Pattyn30-Dec-11 0:06
sitebuilderLuc Pattyn30-Dec-11 0:06 
AnswerRe: c# Winforms with classes. Pin
Shameel2-Jan-12 2:12
professionalShameel2-Jan-12 2:12 
GeneralRe: c# Winforms with classes. Pin
BillWoodruff18-Jan-12 13:02
professionalBillWoodruff18-Jan-12 13:02 
AnswerRe: c# Winforms with classes. Pin
BillWoodruff18-Jan-12 11:56
professionalBillWoodruff18-Jan-12 11:56 
Note: having the information about exactly where 'mainClass, and admin.cs instances are created would, imho, focus this question.

This type of question involving exposing fields, methods, etc. across Form boundaries is one of the most frequently asked and answered ones on the C# QA forums.

There are a variety of techniques you can use.

1. injection : have a public variable in admin.cs that holds the instance of mainClass: and when mainClass is created/instantiated: set that public variable in the instance of admin.cs. Note the requirements here: some entity (your main form's code ?) will have to have access to instances of both admin.cs and mainClass.

2. expose the instance of mainClass via a public static property in whatever Form it is created in; using a private 'setter, and a public 'getter is a common practice.

3. create a static class to function in the same way as #2, but, then, why do this, if you only need to expose one instance of one object ? However, if mainClass is instantiated in one semantic entity, and admin.cs is instantiated in another, then this strategy may be necessary.

4. raising events: not relevant here, I think.

My own preference is to keep the "scope" of what is exposed as minimal as possible (which is I why I shudder in horror every time I see .NET code that injects the instance of an entire Form into another). I see this as being consistent with the principle of "separation of concerns."

In this case, if the only data "admin.cs" must have is the 'PacketSize data from mainClass: then consider just exposing a static property, or variable of whatever Type PacketSize is in whatever Form creates the instance of mainClasss, and setting it immediately after mainClass is instantiated.

Then admin.cs can access the property by something like mainClass.PacketSize. If PacketSize is something dynamic that's changing, then use a public static method in mainClass.cs, perhaps to access the current value from the instance of admin.cs.

You'll note I have not mentioned interfaces here: I have a bias against using them in cases where the real use of them is only to expose limited access to data via casting to the interface.

edit ... see response to Shameel's code above for an example of what this post talks about in code ...

best, Bill
"It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle


modified 18-Jan-12 19:03pm.

QuestionHow can a display msg file to user in Reply/ReplyAll/Forward mode just like outlook mailitems ? Pin
Tanuja12316-Dec-11 2:20
Tanuja12316-Dec-11 2:20 
QuestionScreen scraping C# Pin
Richard MacCutchan13-Oct-11 5:41
mveRichard MacCutchan13-Oct-11 5:41 
AnswerRe: Screen scraping C# Pin
Luc Pattyn13-Oct-11 6:23
sitebuilderLuc Pattyn13-Oct-11 6:23 
GeneralRe: Screen scraping C# Pin
Richard MacCutchan13-Oct-11 7:39
mveRichard MacCutchan13-Oct-11 7:39 
GeneralRe: Screen scraping C# Pin
Ravi Bhavnani13-Oct-11 17:24
professionalRavi Bhavnani13-Oct-11 17:24 
GeneralRe: Screen scraping C# Pin
Richard MacCutchan13-Oct-11 22:48
mveRichard MacCutchan13-Oct-11 22:48 
GeneralRe: Screen scraping C# Pin
Richard MacCutchan16-Oct-11 1:42
mveRichard MacCutchan16-Oct-11 1:42 
AnswerRe: Screen scraping C# Pin
Luc Pattyn16-Oct-11 5:39
sitebuilderLuc Pattyn16-Oct-11 5:39 
GeneralRe: Screen scraping C# Pin
Richard MacCutchan16-Oct-11 6:41
mveRichard MacCutchan16-Oct-11 6:41 
GeneralRe: Screen scraping C# Pin
Richard MacCutchan1-Dec-11 0:32
mveRichard MacCutchan1-Dec-11 0:32 
GeneralRe: Screen scraping C# Pin
BillWoodruff11-Dec-11 9:22
professionalBillWoodruff11-Dec-11 9:22 
GeneralRe: Screen scraping C# Pin
Richard MacCutchan11-Dec-11 22:18
mveRichard MacCutchan11-Dec-11 22:18 
Questiontry to learn Pin
smokindinesh7-Oct-11 18:02
smokindinesh7-Oct-11 18:02 
AnswerRe: try to learn Pin
DaveyM697-Oct-11 23:58
professionalDaveyM697-Oct-11 23:58 
AnswerRe: try to learn Pin
RobCroll10-Oct-11 12:00
RobCroll10-Oct-11 12:00 
AnswerRe: try to learn Pin
Eddy Vluggen22-Oct-11 0:00
professionalEddy Vluggen22-Oct-11 0:00 
AnswerRe: try to learn Pin
BillWoodruff22-Oct-11 0:08
professionalBillWoodruff22-Oct-11 0:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.