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? -
Public Class Form1
-
Public varPub as String = "PUBLIC"
-
Private Sub Main
-
varPub = "PUBLIC"
-
subClass = New SecondClass()
-
subClass.IsIT()
-
End Sub
-
End Class
-
-
Public Class SecondClass
-
Inherits Form1
-
Public Sub IsIT()
-
MessageBox.Show(varPub) 'Shows blank
-
MessageBox.Show(Form1.varPub) 'shows value
-
End Sub
-
End Class
-
8 1026
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.
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.
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. -
public class Program
-
{
-
public string varPub = "PUBLIC";
-
-
static void Main(string[] args)
-
{
-
subClass c = new subClass();
-
c.IsIT();
-
Console.ReadKey();
-
}
-
}
-
-
public class subClass : Program
-
{
-
public void IsIT()
-
{
-
Console.WriteLine(varPub);
-
}
-
}
-
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: -
Module Module1
-
-
Sub Main()
-
Dim c As New Class2
-
c.IsIT()
-
Console.ReadKey()
-
End Sub
-
-
End Module
-
-
Public Class Class1
-
Public varPub As String = "PUBLIC"
-
End Class
-
-
Public Class Class2
-
Inherits Class1
-
Public Sub IsIT()
-
Console.WriteLine(varPub)
-
End Sub
-
End Class
-
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?
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: -
Module Module1
-
-
Sub Main()
-
Dim c As New Class2
-
c.IsIT()
-
Console.ReadKey()
-
End Sub
-
-
End Module
-
-
Public Class Class1
-
Public varPub As String = "PUBLIC"
-
End Class
-
-
Public Class Class2
-
Inherits Class1
-
Public Sub IsIT()
-
Console.WriteLine(varPub)
-
End Sub
-
End Class
-
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.
Plater 7,872
Recognized Expert Expert
I did the following: -
secondclass cd = new secondclass();
-
cd.testit();
-
-
public class baseclass
-
{
-
protected string somestr = "INIT VALUE";
-
-
public baseclass()
-
{
-
somestr = "baseclass constructor";
-
}
-
}
-
public class secondclass : baseclass
-
{
-
public void testit()
-
{
-
System.Windows.Forms.MessageBox.Show(somestr);
-
}
-
}
-
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.
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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
|
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...
| |
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...
|
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?
...
|
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...
|
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...
|
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
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |