3 Replies - 885 Views - Last Post: 25 June 2011 - 04:03 AM Rate Topic: -----

#1 JizzaDaMan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 139
  • Joined: 23-May 11

changing the value of a variable in a sub

Posted 25 June 2011 - 03:01 AM

I have a variable declared in the 'Form1' class and I want to change it in a sub in different class to a different value, and then use it agin in the 'Form1' class. However, I don't seem to be able to change the value in a different class. Is there any way I can rectify this?
The variable I am using is declared as public and I am working in Visual Basic.NET 2010
Is This A Good Question/Topic? 0
  • +

Replies To: changing the value of a variable in a sub

#2 youngone  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 60
  • Joined: 12-June 11

Re: changing the value of a variable in a sub

Posted 25 June 2011 - 03:16 AM

It seems like you want to change the variable in a different instance though.
class Form1 {
public:
  void SetVar(int x) { m_Var = x; }
  int GetVar(void) { return m_Var; }
protected:
  int m_Var;
};
class SubForm : public Form1 {
};

Form1 f1;
SubForm sub;
f1.SetVar(3);
sub.SetVar(4);
cout << f1.GetVar() << endl; // prints 3
cout << sub.GetVar() << endl; // prints 4
sub.SetVar(5);
cout << f1.GetVar() << endl; // prints 3
cout << sub.GetVar() << endl; // prints 5


Other words, if you create a new instance of SubForm called sub, it automatically created an instance of Form1 at the same time, but it does not affect your instance called f1 (which is another instance of Form1). Sorry for C++ code but it the point stays.

This post has been edited by youngone: 25 June 2011 - 03:18 AM

Was This Post Helpful? 0
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: changing the value of a variable in a sub

Posted 25 June 2011 - 03:50 AM

First, it is not advised to make your form variables(Instance of class) public since you reduce encapsulation. There are many ways to get a variable from one form to be used or changed its value in another form.
- First, point it using its form name, like formName.FormVariable
- Second, create global variable that holds the value by creating a public variable in module which can be used in the whole project.

This issue is mostly asked and has been explained in VB.NET FAQs thread which is pinned in VB.NET home page.

This post has been edited by smohd: 25 June 2011 - 04:03 AM

Was This Post Helpful? 0
  • +
  • -

#4 Jeff H  Icon User is offline

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 308
  • Joined: 30-January 11

Re: changing the value of a variable in a sub

Posted 25 June 2011 - 04:03 AM

Bad design idea,
but are you are trying to create a sub in the other class that you want to pass the variable ByRef,
which if it is not a data type will pass by reference anyways.

    Public Shared Sub SubToAdd1ToVarible(ByRef num As Integer)
        num += 1
    End Sub


This post has been edited by Jeff H: 25 June 2011 - 04:04 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1