Go Back  Xtreme Visual Basic Talk > General Discussion > Random Thoughts > Targa tga code translation -- why does the VB.Net IDE have to be so stupid!


Reply
 
Thread Tools Display Modes
  #1  
Old 07-13-2012, 09:14 PM
surfR2911 surfR2911 is offline
Contributor
 
Join Date: Oct 2009
Posts: 719
Default Targa tga code translation--why does the VB.Net IDE have to be so stupid(unhelpful)!


I was trying to convert some C# to VB.Net code using several online
converters. They all gave me the same VB.Net code result and I pasted it into the VB.Net and immediately got an error:
Quote:
'IsNot' requires operands that have reference types
The code:
Code:
Protected Overridable Sub Dispose(disposing As Boolean)
      ' Check to see if Dispose has already been called.
      If Not Me.disposed Then
        ' If disposing equals true, dispose all managed 
        ' and unmanaged resources.
        If disposing Then
          ' Dispose managed resources.
          If Me.bmpTargaImage IsNot Nothing Then
            Me.bmpTargaImage.Dispose()
          End If
          If Me.bmpImageThumbnail IsNot Nothing Then
            Me.bmpImageThumbnail.Dispose()
          End If
          If Me.ImageByteHandle IsNot Nothing Then
            If Me.ImageByteHandle.IsAllocated Then
              Me.ImageByteHandle.Free()
            End If
          End If
          If Me.ThumbnailByteHandle IsNot Nothing Then
            If Me.ThumbnailByteHandle.IsAllocated Then
              Me.ThumbnailByteHandle.Free()
            End If
          End If
        End If
      End If
      disposed = True
End Sub
Of course with a little research I found this old MSDN VS.Net 2008 page.
(there should be a VB.Net 2010 page also -- because the issue is still occurring
in the VB.Net 2010 IDE, but I didn't find one..)

The longer explanation (yes more internet research,
(and this info should have been included the MSDN IsNot article):

In C#, the == and != operators are used for both value types,
(structures and enums) and reference types (classes and delegates).

In VB, the = and <> operators are used only for value types
while the Is and IsNot operators are used only for reference types.

There are some exceptions to this rule in VB,
where the = and <> operators have been explicitly defined for a reference type.
The most visible case of this is the String class.
You can use the = and <> operators on String objects to test value equality
where the Is and IsNot operators test reference equality.

That means that this:
Code:
    If str1 = str2 Then
will be True if the two Strings contain the same text,
even if they are two different objects.

As an example of how the two conditions differ,
try creating a form with two TextBox controls,
then type the same text into both and then execute this code:
Code:
    MessageBox.Show((TextBox1.Text = TextBox2.Text).ToString())
    MessageBox.Show((TextBox1.Text Is TextBox2.Text).ToString())
The first one is True because both Strings contain the same text
while the second one is False because they are two different String objects.

The thing that is annoying is that the VB.Net IDE offers no auto-correction options..
(like "Would you like to change the IsNot to <> in order to fix the error?")

Here's another gotcha:
You paste your converted from C#.Net to VB.net code into a Class Library project
(intending to have a nice VB.Net dll), and you get this error:
Quote:
Namespace or type specified in the Imports 'System.Drawing' doesn't contain
any public member or cannot be found. Make sure the namespace or the
type is defined and contains at least one public member.
Make sure the imported element name doesn't use any aliases.
The error points to this line:
Quote:
Imports System.Drawing
I've added that line to my VB.Net Winforms projects hundreds of times and never got an error!

So you backspace over the ".Drawing" part, leaving
Quote:
Imports System
..and the error goes away (the VB.Net IDE likes you again..hmmm..)

However, them you type in the period after "Imports System" and the VB.Net IDE intellisense gives you a whole bunch of options starting with AttributeUsageAttribute
and ending with XML but "Drawing" is nowhere to be found!!!!

Of course the VB.Net IDE auto-correction and help offers no clue as to why this strange behavior is occurring.

More internet research turns up this StackOverflow thread.

I had fun reading the long explanation in that thread
that explains the really awkward way that Microsoft differentiates
between assemblies and namespaces,
but the short answer is:
Quote:
The System.Drawing namespace "lives" in another dll
that is not referenced initially in the template project for a class library.
You will have to add a reference to System.Drawing (right click on the project -> add reference; System.Drawing is in the GAC).
What's really aggravating is that the developers of the VB.Net IDE
knew this and didn't provide something helpful that might actually say:
Quote:
Adding this namespace to a class library project requires an assembly reference would like to add this reference now?

Here's yet another error (gotcha):
Quote:
Variable '<variablename>' hides a variable in an enclosing block
The VisualStudio.Net IDE provides absolutely no auto-correction options.

Her's the thing I've seen.
A lot of C#.Net programs like to a line like:
Quote:
byte[] data = null;
into something like
Quote:
Dim data As Byte() = Nothing
..nothing wrong with that is there..but wait..

Beneath this seeming innocent line is another C#.Net line like
Quote:
using (msData = new MemoryStream())
..which translates to the VB.Net code of:
Quote:
Using msData = New MemoryStream()
See the problem?

The solution is to delete the initial Dim,
or rename the "Using msByte" to something like "Using msData2",
however the .Net IDE offers neither of these options.

It doesn't really give you any idea that which two lines are causing the conflict (and I think it definitely would be helpful if it did..).

Here's another Dim/Using combo causing problems:
Quote:
filebytes = System.IO.File.ReadAllBytes(Me.strFileName)
Using binReader = New BinaryReader(FileStream)
had to change to:
Quote:
Dim mystream As New System.IO.FileStream(Me.strFileName, IO.FileMode.Open)
Using binReader = New BinaryReader(mystream)
..and it clears the error and works but I have no idea
what was wrong or why this corrects it?

AAAAAAAAAAAAAARRRRRRRRGGGGGGGGGHHHHHHH!!!!!!!!!!!!
Maybe I'm just having a bad day climbing the learning curve,
but thanks for letting me get that off my chest.

The C#.Net dll in question is for using Targa (.tga) files from this CodeProject.
But anyway..I'm ready to give up.

Because of the forum's post length limitations Ill attach the code
(with all errors cleared) as a text file,
but who knows if I garbled the translation too badly for it to ever work
Quote:
Edit: Maybe I'll try working with a little later when I'm less upset/discouraged..
(still haven't figured out the type of dll & exe merging-into-one-solution IDE patching described here,
but did find this shorter application.settings explanation was more helpful).
Attached Files
File Type: txt Targa_tga_reading_VB_dot_net_code.txt (57.4 KB, 0 views)

Last edited by surfR2911; 07-13-2012 at 09:34 PM.
Reply With Quote
  #2  
Old 07-20-2012, 03:30 PM
Cerian Knight's Avatar
Cerian Knight Cerian Knight is offline
Multi-Technologist

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 3,734
Default

It seems you might benefit from reading through my earliest account of collision with the .NET IDE (obviously I was flustered):
http://www.xtremevbtalk.com/showthread.php?t=281952
... then you might more easily brush off the dust and keep moving.
__________________
"May the code that you write never work in ways that you didn't expect; and may the code that you didn't write never require you to maintain it". - Ancient Chinese Proverb
Reply With Quote
  #3  
Old 07-20-2012, 04:08 PM
surfR2911 surfR2911 is offline
Contributor
 
Join Date: Oct 2009
Posts: 719
Default Insuring build integrity

Thanks for the link Cerian Knight.
The situation PrOpHeT describes in that thread is indeed
one of the difficulties I have run into.

I know how to a VB.Net dll project together with a regular VB.Net WinForms demo project,
that uses (and properly references the dll created at runtime),
as well as setting the build order dependencies properly inside the IDE,
but sometimes the whole solution just goes "blows up"
and suddenly it starts thinking that the dll project is the start-up project,
(even though under the Project properties, the "Startup object" is "Fortm1")
and giving me (when I try to build/run the solution), the dreaded error:
"A project with an Output Type of Class Library cannot be started directly."

At which point I can re-create the solution from the VB.Net dll class library project
and the VB.Net WinForms window application project saved separately.
Then for a while it will works, but eventually (later) the error comes back.

I've swapped around some of the non-dll and non-exe files inside
the bin and debug sub-folders and in a few cases have been able to
clear the error for a while but it always comes back eventually for some unknown reason.

Just deleting the raw solution files (.sln and .sou) doesn't ever brings things back.
Deleting all the files in bin and debug folders sometimes bring
things back but sometimes not.
I've had a lot of issues with corruption of files in the
"My Project" folder (like Application.myapp, Settings.setting, AssemblyInfo.vb, etc).

I'm learning more and more every day about patching around the IDE
(and the messes it creates).

Hopefully I will one day get a non-Express version of VB.Net.
Then I can design and use a non-template type custom visx
to fix/patch the IDE semi-permanently.

Until then hoping maybe for a service pack update..who knows.. <*shrug*>

Last edited by surfR2911; 07-20-2012 at 04:24 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->