Answered by:
declare a variable calss with dim

Question
-
why we can't declare a variable on some class such as math
for example?
dim s as new math
?
but we should imports them..
Friday, March 18, 2011 1:10 PM
Answers
-
1. Imports doesn't add a reference to a class library or project - it simply enables you to bring specific namespaces into scope and allow you to enter items with fully qualifying them. To add a reference to a dll you need to add a reference. If the class/method is not accessible either it will not allow access to them.
2.Yes - add reference and Imports are NOT the same thing. One adds the reference to the project (Add Reference) and on brings it into scope allowing shortened names (Imports) .
You can add a references without Importing and it would work - you would just have to fully qualify all you types/memers.
If you import without an add - then no types/members would be found.
- Marked as answer by Baby_Tehran Sunday, March 20, 2011 6:18 PM
Sunday, March 20, 2011 4:13 PM
All replies
-
Because there's no need to; all members are shared (static);
you can go ahead and use all these functions e.g. debug.print(math.Sin(x).ToString)
- Proposed as answer by Cor Ligthert Friday, March 18, 2011 1:47 PM
Friday, March 18, 2011 1:34 PM -
I'm assuming you are wanting to use these functions
http://msdn.microsoft.com/en-us/library/system.math.aspx
Use of imports statement will enable you to use the functions without fully qualifying them (ie. Having to type System.Math.??????)
But as the documentation states
"Provides constants and static methods....."
So there is no need to create an instance of this class. This is similar to if the methods were to be declared in a Module in VB. You don't need to instance a new module to use the methods.
- Proposed as answer by John Anthony Oliver Friday, March 18, 2011 11:31 PM
Friday, March 18, 2011 4:36 PM -
Hi spotty,
Another way I'm reading the OP question is;
"Why has the MATH class got no constructor method and doesn't require an instance object"?
I.E: Even though System.Math as a CLASS has SHARED methods by what mechanism is the constructor hidden please?
Is it an attribute on the NEW method or the opening line of the CLASS itself?
Intellisense states; "Type System.Math has no constructors."
if you mouse-over the "s" should actually try;
Dim s As New Math
Yet something like this still works in VB.Net ( 2008 ):>>
Option Strict On Option Explicit On Option Infer Off Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim s As Math Dim result As Integer = s.Abs(-34) MessageBox.Show(result.ToString) End Sub End Class
Regards,
John
Click this link to see how to insert a picture into a forum post.
Installing VB6 on Windows 7
Friday, March 18, 2011 11:57 PM -
I believe that this example will provide you with a warning
- Unused local variable: 's'.
- Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.
Both indicate your not using an instance (S has been instantiated anywhere) as such - and are calling the shared member through an instance.
The warnings are highlighting potential flaws in the code.
Saturday, March 19, 2011 12:15 AM -
why this code work correctly?
Dim s As Math
Dim result As Integer = s.Abs(-34)
if we assume the math class doesn't have no constructors.it opposite to that vb create a hidden new
constructors. and why we can declare a variable on math class without new
constructors?
Saturday, March 19, 2011 5:34 AM -
The warnings are telling you that its not using the instance in order to execute the
- Unused local variable: 's'.
- Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.
It will execute using the shared method not as an instance method - the smart tag and auto correct will even help you in fixing this up.
The reason it works is because VB will do this stuff and warn you of the problem. The code is incorrect, but this is just one of many things that VB will do to try and make badly written code work.
Saturday, March 19, 2011 5:41 AM -
Hi Baby_Tehran,
What may be even more confusing is if you were to comment out the first line:>>
' Dim s As Math Dim result As Integer = s.Abs(-34)
Regards,
John
Click this link to see how to insert a picture into a forum post.
Installing VB6 on Windows 7Saturday, March 19, 2011 3:23 PM -
Its not unexpected as Its as simple as the following example
Option Strict On Option Explicit On Option Infer Off Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim s As SharedClass Dim result As Integer = s.Abc(-34) MessageBox.Show(result.ToString) End Sub End Class Class SharedClass Shared Function Abc(x As Integer) As Integer End Function End Class
If you comment out the declaration line (Dim s as sharedclass) it has no idea of what type s is ? It doesn't know its a Structure/Class/Module - nothing and therefore it will fail to compile period....
With the line commented in - it is telling you that you are trying to access a method on the type that is a shared method but trying to access it through an instance. This is why this is the warning. And provides you with the ability to correct this call usign the AutoCorrect.
This is simply an example showing what is goin on with the Math Class but using a user defined class - there is not really much of a difference and either way the compiler is warning you of the problem - or potential problem.
- Proposed as answer by John Anthony Oliver Saturday, March 19, 2011 6:26 PM
Saturday, March 19, 2011 4:13 PM -
Hi spotty,
Thanks for your explanation of this to the OP. :-) I was thinking of posting a similar example.
Do you have an explanation for these two questions I posed earlier please? >>
- Even though System.Math as a CLASS has SHARED methods by what mechanism is the constructor hidden please?
- Is it an attribute on the NEW method or the opening line of the CLASS itself?
Regards,
John
Click this link to see how to insert a picture into a forum post.
Installing VB6 on Windows 7
Saturday, March 19, 2011 6:30 PM -
questions:
1-i create a class and methods why can't i add it to my project with import?
can i assume that when we use .net framework we should use import?
2-when i import a .net class with import command is it necessary to add it to my project with (project->add refrence)?
when should we add .net refrence with this path(project->add refrence)
Saturday, March 19, 2011 6:34 PM -
no one is here?Sunday, March 20, 2011 8:20 AM
-
questions:
1-i create a class and methods why can't i add it to my project with import?
can i assume that when we use .net framework we should use import?
2-when i import a .net class with import command is it necessary to add it to my project with (project->add refrence)?
when should we add .net refrence with this path(project->add refrence)
Hi,To do that you would need to start a CLASS LIBRARY PROJECT which creates a DLL file.
Then when you create a NEW project simply go to the PROJECT menu, select ADD REFERENCE and find the DLL file in your previous project.
Use the BROWSE tab in the window that opens. Select the DLL file then click on OK.
It will be in the BIN\DEBUG or BIN\RELEASE folder depending on your compile settings.
I hope this helps. :-)
For the 2nd part of your question, you only need to add an IMPORTS statement when;
- You want to add functionality to a program that is not immediately available and for this you do not always need to add a reference as some .Net CLASSes are imported by default, especially in the Express Edition.
- You do not want to use fully qualified code E.G:
If you use;
Imports System.IO
you can then say
Dim sr As New StremReader("SomeFileName.txt")
'rather than:>>
Dim sr As New System.IO.StreamReader("SomeFileName.txt")
or similar such code available via the System.IO namespace.
Regards,
John
Click this link to see how to insert a picture into a forum post.
Installing VB6 on Windows 7
Sunday, March 20, 2011 12:49 PM -
1. Imports doesn't add a reference to a class library or project - it simply enables you to bring specific namespaces into scope and allow you to enter items with fully qualifying them. To add a reference to a dll you need to add a reference. If the class/method is not accessible either it will not allow access to them.
2.Yes - add reference and Imports are NOT the same thing. One adds the reference to the project (Add Reference) and on brings it into scope allowing shortened names (Imports) .
You can add a references without Importing and it would work - you would just have to fully qualify all you types/memers.
If you import without an add - then no types/members would be found.
- Marked as answer by Baby_Tehran Sunday, March 20, 2011 6:18 PM
Sunday, March 20, 2011 4:13 PM -
and final question
is string integer and other variable type are shared member?
Sunday, March 20, 2011 7:45 PM -
and final question
is string integer and other variable type are shared member?
Hi,
SHARED keyword means shared between every instance.
However you do not need to create an instance variable to access a SHARED Sub or Function or indeed any SHARED member.
Variable types are not SHARED in that sense as one STRING can have a different value from another STRING.
One integer variable can have a different value from another integer variable and so on....
All types in VB.Net inherit from System.Object
Regards,
John
Click this link to see how to insert a picture into a forum post.
Installing VB6 on Windows 7
Sunday, March 20, 2011 8:12 PM -
according this address http://visualbasic.about.com/od/learnvbnet/a/sharedinstance_2.htm
some class that have something special and constant for every thing they called shared member.
i know shared instance variable are share between all object that created with the same class.
Thanks anyway
best regards
Monday, March 21, 2011 8:28 AM -
Hi spotty,
Thanks for your explanation of this to the OP. :-) I was thinking of posting a similar example.
Do you have an explanation for these two questions I posed earlier please? >>
- Even though System.Math as a CLASS has SHARED methods by what mechanism is the constructor hidden please?
- Is it an attribute on the NEW method or the opening line of the CLASS itself?
I didn't see these points answered:
The constructor is hidden by making the constructor Private.
Stephen J WhiteleyMonday, March 21, 2011 12:03 PM