Solved

Importing my own class in vb.net

Posted on 2013-10-25
13
709 Views
Last Modified: 2013-10-28
Hi,

I've worked with VBA a fair bit, but I'm fumbling around with getting VB .net to work.  

I can't get my class to be recognized in my main form nor can I get the Debug.Print statement to be recognized in the class.


I have a project with MainForm.vb.  In the Project menu I chose to Add Class, which I assumed would automatically be added to the project, though I realize I may need an Imports statement.  In my new class Rtu (in file Rtu.vb), I defined both public methods and constants as well as private ones.  However, the Debug.Print statement is not recognized.  I thougth maybe I needed an additional reference, but when I go to the Projects menu it doesn't even seem to recognize Rtu as part of the main project. Is that a simple Imports statement?  I have the same imports in Mainform.vb as in my class, and debug.print is recognized there.


When I go to my MainForm.vb, I tried both 'Imports Rtu' and 'Imports Rtu.Rtu' and variations, but it doesn't like that.

Suggestions?

Thanks!
0
Comment
Question by:ugeb
  • 6
  • 4
  • 3
13 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39601417
nor can I get the Debug.Print
Add "Imports System.Diagnostics" to the top of your class file.

Can you post a screenshot of what your Solution Explorer hierarchy looks like?
0
 
LVL 11

Author Comment

by:ugeb
ID: 39601631
Importing system diagnostics didn't seem to help.  At least intellisense didn't recognize it.  (BTW, it's always worked in Mainform.vb, just not recognized in my class)

Is this the screenshot you wanted?
vbSolutionEE.png
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39601661
Yes. Can you show the current state of your code for Rtu.vb?
0
 
LVL 11

Author Comment

by:ugeb
ID: 39601699
Here's the Rtu (actually RealTimeUpdates) class:
Option Explicit On

Imports Microsoft.VisualBasic
Imports System.Text
Imports System.Diagnostics

Public Class RealTimeUpdates

    Public IsHoliday As Boolean
    Dim WorkbookName As String
    Dim thetime As Double
    Dim TimeTill As String

    Public RunWhen As Double
    Public Const cRunIntervalSeconds = 5
    Public Const cCloseTime = #11:00:00 AM#
    Public Const cOneMinute = 0.000694444444444444
    Public Const cHalfSecond = 0.00000578703703703706
    Public Const cOneHour = 0.0416666666666667
    Public Const cMarketClose As Double = (#3:02:00 PM#) * 1   '= 0.626388888888889
    Public Const cMarketOpen As Double = (#8:30:00 AM#) * 1   '= 0.354166666666667
    Public Const cRunWhat = "Time2Close"
    Private tickerList As String
    Private tickerTypes As String
    Private fieldsByType As String

    Property tickers As String
        Get
            Return tickerList
        End Get
        Set(ByVal value As String)

        End Set
    End Property

    Public Sub readOptionCodes()
        Dim file As String = "D:\Documents\Finance\StaticReferenceData\StrikePriceCodes.csv"
        Call readTextFile(file)
    End Sub

    Private Sub readTextFile(ByVal file)
	''' most of this has been deleted for space here
	debug.print(file)

    End Sub
End Class

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39601898
I put your code into a new project. The only issue that I can see is that the date math you are trying to do (the * 1 stuff) doesn't seem to work in .NET. I'm not familiar with that notation, so what is it supposed to be doing?
0
 
LVL 11

Author Comment

by:ugeb
ID: 39602035
I copied much of that from a VBA project just as a stub to get the class started.

The biggest issue is the fact that I can't use the new class in my existing project.  I think the issues are related.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39602142
Does your form have a namespace declaration?

e.g.

Namespace SomeNamespace
    Public Class YourForm
        Inherits System.Windows.Forms.Form

...

    End Class
End Namespace

Open in new window

0
 
LVL 11

Author Comment

by:ugeb
ID: 39602275
I did not have a namespace.  However, when I added one (with the name of the project -- as Visual Studio suggested), now none of my visual elements are accessible.  In MainForm.vb, my text boxes are all now marked as undeclared.  When I go to the MainForm design page, it no longer shows my actual form.

This seems insane.  All I wanted to do was add a class to an existing project  

Is the namespace mandatory?  The project would compile and run without it, but I couldn't access the new class.
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 39603523
@Kaufmed

The *1 is a dirty trick. Since the result of a multiplication is a Double and VBA does automatic conversions (no Option Strict in there), multiplying a Date by 1 would give you a Double representation of the date (using the CDbl command would be the "right" way to do that).  Because the Date type was represented as a Double in VBA, manipulating dates and times through doubles sometimes improved performance.

There were also a number of tricks that older VB programmers used. For instance, Date + 1 was incrementing the day by 1. Doing the same in .NET would increment it by 100 nanoseconds :-)

@ugeb

And be careful about using VBA or VB6 code as a template.

The whole routine you have there is useless in .NET, because the Date variable in .NET does not record the time the same way. In VBA, the time was recorded as a fraction of the day (Noon was .5, 6PM as .75), so it was a Double, as you are doing in your code. But in .NET, the Date records the time in ticks, a Long. A tick is 100 nanoseconds, so there are 10,000 ticks in a millisecond.

None of the constants that you define will work in .NET, where the numerical representation of a half second is 5,000,000 ticks, far from your 0.00000578703703703706. The value of your constant in line 20 will end up being 541200000000 instead of the 0.626388888888889 that you are expecting.

And you are lucky that you work only with the time portion of the Date, because a date with a value of 0 is 01-01-0001 in .NET, while it was 12-31-1899 (if my memory is good) in VBA. So treating dates as numerical values does not work when you use old code. And it is not recommended for new code either.

We do not see what you do with these values, but if your aim is to work with time intervals, a TimeSpan variable might be a lot better and easier to use that doing fancy stuff with doubles.

This being said, because you seem to be working with Excel, there is a thin chance that your code might work, because it follows the conventions of Excel. But I would  I would be very surprised if it did.

Rewriting the code from scratch most often ends up being faster than trying to debug old VBA code that does not work in .NET. And you will never feel the gains that .NET offers if you keep doing things as you did them in .VBA. You would never discover the TimeSpan variable, because it did not existed in VBA. You would not discover the multiple stream classes that can be used and do a lot of stuff for you instead of the very limited ways of working with files in VBA.

-----

As for the question.

When you say that Debug.Print is not recognized, what do you mean exactly? Is it throwing an error, or is it something else such as IntelliSense that stops working? IntelliSense sometimes stops working when a line is preceded by a lot of code that the system does not understand. It works when Kaufmed paste it in his own environment, but he does not have the "most of this has been deleted for space here", so that may make a difference.
 
Have you tried with Debug.WriteLine? It does the same thing and it offers more possibilities, such as conditional output. Debug.Print was added only in version 2.0 of the framework to simplify the transition from VB6/VBA to VB.NET, when Microsoft found out that many old VB programmers were having difficulties adapting to the .NET way of doing things.

-----

As for the namespace

There is already a namespace in your project. Visual Studio creates one when you create a new project. By default, it is the name of the project, and it is defined in the project's Properties window (bottom of the Project menu), in the Application tab. This namespace is recognized by default inside of the application, so you never have to care about it.

What you did when you added a namespace command around your form was to create a sub-namespace. The namespace for your form then became ProjectName.SomeNamespace. Since it is not the default namespace, then it becomes necessary to either specify it everywhere you reference your form, or add an Import for it. The code that was already generated did not do that, so it stopped recognizing your form.

Since all your code is in the application, do not play with namespaces. Namespaces become important mainly when you are working with big dlls.

Night time for me. I won't be back for a while if you have questions.
0
 
LVL 11

Author Closing Comment

by:ugeb
ID: 39604350
Thank you both for your comments and help.

James,

Thank you very much for that detailed, insightful and forward looking explanation. Once I removed the extra namespace command I added, and changed the imports statement to the project name instead of the file name, my other module was recognized AND the debug.print statement worked too.  I'm indifferent to debug.print vs. debug.write, though I use VBA a fair bit and debug.print is the only one that works with it.

Thank you for the heads up on the time stuff.  That could have taken me a while to figure out that doubles can't be used for time anymore.  And while I don't need 100 nanosecond granularity, I will have to do some date manipulations to determine holidays etc.  I guess I will have to rewrite all those date and time functions to work in .net.  If you know of references (other than standard MS stuff), I'd appreciate that too.

Thank you again!
0
 
LVL 40
ID: 39604638
The standard MS stuff is actually quite good. The documentation for .NET is a lot better and complete than what was provided for VBA and VB6, and sometimes, right from the code itself.

.NET is object oriented. So instead of trying to find commands in the language to do stuff (old VBA commands are still supported to make it easier for amateurs to migrate to .NET), do it the object way. Simply create a Date variable, type the name of the variable followed by a dot, and explore the methods and properties that IntelliSense brings to you. From there, F1 brings you the details. That is the way to work in an object oriented environment. Each variable comes with what you need to work with it, and you will find that what is offered by a Date variable far exceed the possibilities of what the date commands could do in the old VBA.

That goes for date-time manipulations.

If however you are dealing with durations, which is what I see at first look in the code you provided, create a TimeSpan variable and do the same, explore its properties and methods.

As for the Debug.Print vs Debug.WriteLine, simply write Debug.WriteLine in your editor and press F1. You will see that WriteLine can be called in different ways, while Debug.Print has only one way to go. The need for the extra ways might not seem evident at first, because you found ways of doing what you needed to do with Debug.Print. But once you see the use of one of these only once, you understand that the extras offered by .NET as simply tools for you to use when needed, that can save you a lot of code. This does not holds only for Debug, but for everything you do as a programmer.

There are a lot of differences between .NET and VBA, but I think that the main one is that in .NET, instead of looking for a command when you want to do something, you need to search for the class that does that something. It can be intimidating at the start because of the 10,000 or so classes available. But after a while, instead of feeling lost in all those classes, you end up feeling confident that whatever you want to do, most of the time there is a class.method or class.property that can do it for you in one line of code instead of the 60 lines of code the same operation would have required in VBA.

Think of all the work you would have to write in order to Draw and work if a square portion of screen in which you want the user to type if you did not have a TextBox. In .NET, that paradigm is extended to everything, not just controls. It takes a while, but once you find you way around that profusion of tools that the .NET classes are, you start cursing when you have to go back to do maintenance on your old VBA code.

.NET is not perfect, but is a lot more powerful than VBA, and makes coding complex things a lot simpler once you get the feel for its intricacies.
0
 
LVL 11

Author Comment

by:ugeb
ID: 39604905
Thank you again for yet more detailed info.  Indeed I try to use the object browser as much as possible, but there are some quirks to both VB and .net that I'm still trying to get accustomed to.

Indeed I am dealing with durations mostly in my date/time calculations.  The info you've provided will be very helpful, so thank you for all the pointers!
0
 
LVL 40
ID: 39604927
The Object Browser is a very useful tool, but you have to be aware of its main limitation: it tells you only about classes in dlls that you have already referenced. So it basically shows you only a narrow subset of what is available.

For the sake of finding classes that can do your work, specially at the beginning of a project, F1 is the way to go. Just be sure to use the Add and Remove Help Content option in the Help menu in order to filter out what you do not need for you own purposes, otherwise you will be overwhelmed by classes that are useless to you.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now