Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • *Experts*
Posted

@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

"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
Posted

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

Visual Basic 2008 Express Edition!
  • Administrators
Posted (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 by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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.

Posted

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?

  • Administrators
Posted

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?

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • 3 weeks later...
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...