PlayKid Posted August 18, 2005 Posted August 18, 2005 Hi all, I am having 2 forms, and I have declared an integer variable in form 1, then I assign something from from2 to that integer value, but it just didnt pass my value, can somebody tell me why??But when I am passing a textbox value, it can be passed. PlayKid Quote
neodammer Posted August 18, 2005 Posted August 18, 2005 One way to do it is just add a module to the project and declare the variables there publicly. Quote Enzin Research and Development
IceAzul Posted August 18, 2005 Posted August 18, 2005 Difference between Reference and Value types http://msdn.microsoft.com/msdnmag/issues/1200/dotnet/ Quote
*Experts* Nerseus Posted August 18, 2005 *Experts* Posted August 18, 2005 @PlayKid, Maybe you could show us some code snippets of what you're doing? You describe Form2 referencing a variable on Form1 and I'm curious how you've set this up - it will get you a better answer if we know the full picture of what you're doing. Include anything you may have tried, whether it worked or not, as that may help us understand as well. -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
vbMarkO Posted August 20, 2005 Posted August 20, 2005 It was mentioned already, but thought I might add.. Create a Module and in the module declare the variable you wish to pass with make it public Public myInt As Integer ' This would go into the module Then in Form1 you could assign something to the variable myInt = "123" ' in Form1 then in Form2 intLBL.Text = myInt ' Result in the label would be 123 vbMarkO Quote Visual Basic 2008 Express Edition!
Administrators PlausiblyDamp Posted August 20, 2005 Administrators Posted August 20, 2005 (edited) Modules are not a particularly OO way to approach the problem - what happens if you have several forms that 'share' information this way? Sooner or later you will end up introducing bugs. For a better approach to passing information between forms etc you may want to give clicky a read. Edited September 8, 2005 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
PlayKid Posted August 21, 2005 Author Posted August 21, 2005 Thanks for all of your replies, here is my code. In Form 1: Private Sub MorepersonButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MorepersonButton.Click Dim MDIChildMCPersons As New MCPersons MCPersons.SupplierNumber = 2 MDIChildMCPersons.MdiParent = ParentForm MDIChildMCPersons.Show() End Sub In Form2: Public Shared SupplierNumber As Integer Private Sub MCPersons_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Label1.Text = SupplierNumber.ToString End Sub And it doesn't doing good. The result is 0 instead of 2. Quote
Administrators PlausiblyDamp Posted August 21, 2005 Administrators Posted August 21, 2005 Firstly is there a reason why you declared SupplierNumber as shared? It probably isn't required or offering any real benefit in this case. If you step through the code in a debugger is SupplierNumber actually being assigned to correctly? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
PlayKid Posted August 21, 2005 Author Posted August 21, 2005 This is one of my method, another method is without the shared, still either ways, they still not working. Is there something wrong with my code? Quote
Administrators PlausiblyDamp Posted August 21, 2005 Administrators Posted August 21, 2005 Did you try stepping through in a debugger to see what happens? Have you tried it without the shared keyword and stepped through the relevant code? Also rather than just using a public variable have you trioed using a property instead and placing a breakpoint within the property set to see if it is getting called correctly? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
PlayKid Posted August 21, 2005 Author Posted August 21, 2005 Yes, I did, but when I clicked the form, the debugger is not responding, what can I do? Quote
Souma Posted September 7, 2005 Posted September 7, 2005 In the first form, u declare ' This is the variable u wanted to be used by other form(s) Public Shared myVal as Integer In the next/other form(s), u need to have the following ' So that u can use the variable u declared in first form Dim firstfrm as new FirstForm firstfrm.myVal = 2 Quote
Administrators PlausiblyDamp Posted September 7, 2005 Administrators Posted September 7, 2005 It would be a lot easier if you posted a more complete code sample like Nerseus suggested earlier. I would definately argue against all the suggestions to use shared / module level variables as a way to pass information between forms. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.