473,473 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Simple scope question

17 New Member
I come from a Java background so this a puzzle to me. I'm wondering why this happens in .net. Why do I have use the class name in order to get the variables value in .net?

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Public varPub as String = "PUBLIC"
  3.     Private Sub Main
  4.         varPub = "PUBLIC"
  5.         subClass = New SecondClass()
  6.         subClass.IsIT()
  7.     End Sub
  8. End Class
  9.  
  10. Public Class SecondClass
  11.     Inherits Form1
  12.     Public Sub IsIT()
  13.         MessageBox.Show(varPub) 'Shows blank
  14.         MessageBox.Show(Form1.varPub) 'shows value
  15.     End Sub
  16. End Class
  17.  
Oct 27 '08 #1
8 1026
mmaslar
8 New Member
In your "SecondClass" class, varPub is not defined. (In fact, there ought to be a warning to that effect on MessageBox.Show(varPub).

So, it's necessary to specify the class where the variable is actually defined.
Oct 27 '08 #2
Plater
7,872 Recognized Expert Expert
You went from java to vbnet? I would have thought c# was the natural transition for a java person.

At any rate that is interesting that a derived class does not get the default value of the base class. Hmm

I think you doctored your source code(possibly to make it be an "example") when you copied it over, because the C# version of that doesn't even compile.
Oct 27 '08 #3
Curtis Rutland
3,256 Recognized Expert Specialist
Well, part of the problem is the way VB.NET likes to hold people's hands, trying to make it easy for you, but instead making it easy to misunderstand and make mistakes.

Technically, you shouldn't be able to call Form1.varPub in SecondClass. The only way you should be able to do that is if it were declared as Shared (static). What VB is doing for you is creating an instance of Form1 and getting the public member varPub for you.

As to the other side of the problem, I'm not too sure. I put together a sample C# version of your program to test your problem, and it works fine for me.
Expand|Select|Wrap|Line Numbers
  1. public class Program
  2. {
  3.     public string varPub = "PUBLIC";
  4.  
  5.     static void Main(string[] args)
  6.     {
  7.         subClass c = new subClass();
  8.         c.IsIT();
  9.         Console.ReadKey();
  10.     }
  11. }
  12.  
  13. public class subClass : Program
  14. {
  15.     public void IsIT()
  16.     {
  17.         Console.WriteLine(varPub);
  18.     }
  19. }
  20.  
Oct 27 '08 #4
Curtis Rutland
3,256 Recognized Expert Specialist
In your "SecondClass" class, varPub is not defined. (In fact, there ought to be a warning to that effect on MessageBox.Show(varPub).

So, it's necessary to specify the class where the variable is actually defined.
That shouldn't be right, because his SecondClass inherits Form1. It should inherit all it's members and methods as well.

EDIT:
I know that's not correct, because I just put this test together in VB.NET:
Expand|Select|Wrap|Line Numbers
  1. Module Module1
  2.  
  3.     Sub Main()
  4.         Dim c As New Class2
  5.         c.IsIT()
  6.         Console.ReadKey()
  7.     End Sub
  8.  
  9. End Module
  10.  
  11. Public Class Class1
  12.     Public varPub As String = "PUBLIC"
  13. End Class
  14.  
  15. Public Class Class2
  16.     Inherits Class1
  17.     Public Sub IsIT()
  18.         Console.WriteLine(varPub)
  19.     End Sub
  20. End Class
  21.  
And it works like expected. I'm not quite sure what's wrong with OP's code. I'd try to imitate it, but it seems like a strange combination of a console app and a windows forms app.

Can you clarify what you are doing?
Oct 27 '08 #5
bobneedshelp
17 New Member
That shouldn't be right, because his SecondClass inherits Form1. It should inherit all it's members and methods as well.

EDIT:
I know that's not correct, because I just put this test together in VB.NET:
Expand|Select|Wrap|Line Numbers
  1. Module Module1
  2.  
  3.     Sub Main()
  4.         Dim c As New Class2
  5.         c.IsIT()
  6.         Console.ReadKey()
  7.     End Sub
  8.  
  9. End Module
  10.  
  11. Public Class Class1
  12.     Public varPub As String = "PUBLIC"
  13. End Class
  14.  
  15. Public Class Class2
  16.     Inherits Class1
  17.     Public Sub IsIT()
  18.         Console.WriteLine(varPub)
  19.     End Sub
  20. End Class
  21.  
And it works like expected. I'm not quite sure what's wrong with OP's code. I'd try to imitate it, but it seems like a strange combination of a console app and a windows forms app.

Can you clarify what you are doing?
C# was my choice but the boss said vb. The setup is a generic service form calling a class
to run applicaitons.

I have several applications that use the same fields to insert data into a database. I created a separate class to do this. The initial idea was to pass a structure and that
didn't work at all. It would be easier on the eyes to see the structure passed and then manipulate the data instead of passing ten variables. The function gets called multiple times.

The next step was to get the variable information from the original class. I thought you could inherit a class and access the variables the same way you could in java. At least that's what my vb book says. I added this to my test application to see how to get the data over. The only way to do it was to add the class name in front of the variable.
Oct 27 '08 #6
Plater
7,872 Recognized Expert Expert
I did the following:
Expand|Select|Wrap|Line Numbers
  1. secondclass cd = new secondclass();
  2. cd.testit(); 
  3.  
Expand|Select|Wrap|Line Numbers
  1. public class baseclass
  2. {
  3.     protected string somestr = "INIT VALUE";
  4.  
  5.     public baseclass()
  6.     {
  7.       somestr = "baseclass constructor";
  8.     }
  9. }
  10. public class secondclass : baseclass
  11. {
  12.     public void testit()
  13.     {
  14.       System.Windows.Forms.MessageBox.Show(somestr);
  15.     }
  16. }
  17.  
And the output was "baseclass constructor", so even in an inherited class, the base class's constructor is called. If that helps at all.
I had to declare the member variable as "protected" to keep it hidden(not public) but to allow it to be visible to the derrived class.
Oct 27 '08 #7
bobneedshelp
17 New Member
That works for me in an application but for some reason not in a service. I am using a namespace for the service and those seem to affect how variables can be accessed. The fix was making the variable shared. I'll try to figure out why later but it's working now.

Thanks for the help.
Oct 28 '08 #8
Plater
7,872 Recognized Expert Expert
Oooo some of those terms public/private/static/protected/internal and etc effect if a class with another namespace (and/or different "assembly") can access the member.
I don't know much about using them in "fancy" ways
Oct 28 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: nos | last post by:
I have two files in the same directory which is '.' I have a method in file1 that I invoke from a method in file2. Why do I have to use class1.method1() instead of just method1(). The...
2
by: Trimbitas Sorin | last post by:
Hello I have a simple syntax question : What does the following line mean: 1: %checkType; ?? I know that @test="" is an array and $test="" is a simple variable. Thank you With best regards...
6
by: Arthur J. O'Dwyer | last post by:
I was paging through Coplien's book "Advanced C++ Programming Styles and Idioms" this afternoon and found some code that looked something like void sort(vector<foo> a) { int flip; do { for...
13
by: Jason Swett | last post by:
I want to do graphics with C++. Surprisingly, so far nobody has been able to tell me anything helpful. How do I do it? Any input would be greatly appreciated. Jason
13
by: Mike P | last post by:
I have, what should be, a simple scope problem. Can you help me fix this? I'm trying to end up like this: originalArray = and newArray = . Instead I wind up like this: originalArray = and...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
3
by: msnews.microsoft.com | last post by:
Hello All, In the "Find Dialog" (Ctrl-F) of the IDE there is an option called "Mark All". When I click "Mark All", this marks the occurences of my search text. How will I clear this mark? ...
7
by: Scott Frankel | last post by:
Still too new to SQL to have run across this yet ... How does one return the latest row from a table, given multiple entries of varying data? i.e.: given a table that looks like this: color...
3
by: Chrism2671 | last post by:
I'm new to XSLT/XML and I have a very simple, quick question. i've been trying to convert simple xml files into CSV files and have made a simple XSLT template using the w3 tutorials, but it doesn't...
5
by: Oriane | last post by:
Hi, With Asp.net 2.0, when a internet user logs in with a "login authentication form", is the password encrypted when it is sent to the server ? Is is hashed ? Best regards
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.