locked
Variable not declared

    Question

  • I have been working with my chess utility for over a year now with no coding issues.  Today I wanted to place all my utilities into a mdi form so that all code for all utilities is under one roof.  I started a new project and added a form to it.  No probelm with that <S>.  Then I added an existing form called EPform.vb to it and all of a sudden I got a long list of variables which are being viewed as not declared.  The actual error statement says for one variable says "iCol is not declared. It may be inaccessible to its protection level".  Now I went back into the EP code and saw I did in fact declare the varaible as an integer???.  Besides which it runs fine on its own until I add it as an existing ....  Can someone explain the probelm here.  BTW dont quote me on this but I think when I did this same thing on my laptop (Windows 7) there was not an issue but I am not positive.

    Thanks,

    Les

    Friday, July 27, 2012 6:57 AM

Answers

  • You're right, that's the difference.

    The error is because you can't store a negative number in an ULong (unsigned long). If you are sure the Integer value will never be negative, you can convert to an ULong:

         Dim u As ULong
         Dim l As Long

         u = CULng(l)


    Armin

    • Marked as answer by Les2011 Sunday, July 29, 2012 4:26 AM
    Saturday, July 28, 2012 12:22 PM
  • I don't understand why you are confused. It's the same as with the other questions you had before. The Val function returns a Double whereas tarRow is declared as an Integer. You can not assign a Double to an Integer.

    In addition, I strongly recommend not making use of the very ancient Val function. Use Integer.Parse instead:

        tarRow = Integer.Parse(Microsoft.VisualBasic.Right(move, 1))

    EDIT: Also you should start using the method of the objects instead of using the VB6 style functions. A String has a Length property:

        Select case move.Length


    Armin


    • Edited by Armin Zingler Saturday, July 28, 2012 1:24 PM
    • Marked as answer by Les2011 Sunday, July 29, 2012 4:25 AM
    Saturday, July 28, 2012 1:23 PM
  • Perhaps you can recommend where I can find out what the various functions return as far as the data type.

    Hover the mouse over the function. The tooltip displays the type of the return value. In this case, i.e. if there's an error, error information has priority over function information, so use one of the following alternatives.

    Press Ctrl+I on the word Val.

    Intellisense also shows function information while typing the word Val.

    Or go to the definition of the function  (press Shift+F2 on the word Val (or whichever shortcut it is on your machine))

    Or press F1 and read it in the documenation.


    Armin


    • Edited by Armin Zingler Saturday, July 28, 2012 1:40 PM
    • Marked as answer by Les2011 Sunday, July 29, 2012 4:24 AM
    Saturday, July 28, 2012 1:39 PM

  •         Dim TotalFileSize As Long
            Dim RecordSize As Long
    TotalFileSize = fi.Length 'this is long and no problem
    but here is the problem:
    ProgressBar1.Value = RecordSize * 100 / TotalFileSize
    apparently ProgressBar1.Value is an Integer
    TotalFileSize has to be a long because of fi.Length and thats where the conflict happens

    How do I get around this issue?

      ProgressBar1.Value = CInt(Math.Floor(RecordSize * 100 / TotalFileSize))

    I use Math.Floor here because I do not like progressbars that show 100%  even though progresss is < 100%. That means, 99.6% will be rounded to 99%. As Math.Floor returns a Double and the Value property is type Integer, CInt is necessary.


    Armin


    • Edited by Armin Zingler Saturday, July 28, 2012 6:24 PM 99.3 changed to 99.6 (makes more sense)
    • Marked as answer by Les2011 Sunday, July 29, 2012 4:24 AM
    Saturday, July 28, 2012 6:16 PM
  • BTW in a prior post you mentioned I should get away from using the Val function and use the integer.parse, is there a similar suggestion for a replacement for the Len function?

    Hi Les,

    a String has a Length property. For example:

          If StringVar.Length > 5 Then

    Be aware that String is a reference type, i.e. a String variable can be Nothing. If 'StringVar' is Nothing, the line above would fail (NullReferenceException) as there is no String of which to determine the length. You would have to verify this before checking the Length:

          If StringVar IsNot Nothing Then
               If StringVar.Length > 5 Then
                     '....
               End If
          End If

    Or in short:

          If StringVar IsNot Nothing AndAlso StringVar.Length > 5 Then
                     '....
          End If

    In another case you may want to check if the variable is Nothing or, otherwise, the String length is zero. Then you can either write

          If StringVar Is Nothing OrElse StringVar.Lenth = 0 Then

    or

          If String.IsNullOrEmpty(StringVar) Then


    Armin


    • Edited by Armin Zingler Tuesday, July 31, 2012 3:40 AM
    • Marked as answer by Les2011 Tuesday, July 31, 2012 3:59 AM
    Tuesday, July 31, 2012 3:40 AM

All replies

  • Hi Les2011,

    I only guess.
    You write you are using different forms. When you use a special form yor get the described failure check the kind of declaration.
    Declare all variables as "Public" in a module. This enables your application to use them in different forms whitout an additional declaration.

    Good luck


    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 and VB 2010 Express Be a good forum member - mark posts that contain the answers to your questions or those that are helpful. c# in vb Translator: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Friday, July 27, 2012 11:03 AM
  • Declare all variables as "Public" in a module. This enables your application to use them in different forms whitout an additional declaration.

    Hi Giftzwockel, :)

    I really recommend not to use modules to avoid these problems. It creates dependencies between different parts of your application so that it's very hard to unscramble them later. OOP does have it's benefits. :)

    @Les2011: By encapsulating all your code in classes, it should be easy to pick out the classes you need in a different application. If you haven't done so yet, you should consider redesigning the code.


    Armin

    Friday, July 27, 2012 11:59 AM
  • "Today I wanted to place all my utilities into a mdi form so that all code for all utilities is under one roof."

    I agree with Armin.  You should explore some basic object oriented programming techniques and principles.  A form has one purpose, which is to serve as an interface for the user to see data and/or input data.  That's it.  One of the most basic of OOP principles is called "Separation of Concerns".

    Chapter 2: Key Principles of Software Architecture  

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Friday, July 27, 2012 2:40 PM
  • Gift I am not using these variables across forms.
    Friday, July 27, 2012 3:04 PM
  • Hi Armin,

    Here is a few of the errors that were reported.  I do not understand what the probelm is since I have Dim the variables at the top of the sub and yet the error talks about the fact I am doing a conversion.  Here are a few examples and if you could be kind enough to show me what I am missing I would appreciate it.

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            Dim TotalFileSize As ULong
            Dim RecordSize As ULong
    	Dim indexcnt As Long
    	Dim EPDcnt As ULong
    TotalFileSize = fi.Length 'option strict on disallows implicit conversions from Long to uLong [fix = CULng(fi.Length)]
    RecordSize = RecordSize + Len(tmp)'option strict on disallows implicit conversions from Decimal to uLong [fix = CULng(RecordSize + Len(tmp))]
    ReDim Preserve GameHolder(indexcnt)'option strict on disallows implicit conversions from Long to Integer [fix = GameHolder(CInt(indexcnt))]
    EPDcnt = EPDcnt + 1'option strict on disallows implicit conversions from Decimal to uLong
    End Sub

    Thanks,

    Les

    Saturday, July 28, 2012 6:26 AM
  • Hi Armin,

    Here is a few of the errors that were reported.  I do not understand what the probelm is since I have Dim the variables at the top of the sub and yet the error talks about the fact I am doing a conversion.  Here are a few examples and if you could be kind enough to show me what I am missing I would appreciate it.

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            Dim TotalFileSize As ULong
            Dim RecordSize As ULong
    	Dim indexcnt As Long
    	Dim EPDcnt As ULong
    TotalFileSize = fi.Length 'option strict on disallows implicit conversions from Long to uLong [fix = CULng(fi.Length)]
    RecordSize = RecordSize + Len(tmp)'option strict on disallows implicit conversions from Decimal to uLong [fix = CULng(RecordSize + Len(tmp))]
    ReDim Preserve GameHolder(indexcnt)'option strict on disallows implicit conversions from Long to Integer [fix = GameHolder(CInt(indexcnt))]
    EPDcnt = EPDcnt + 1'option strict on disallows implicit conversions from Decimal to uLong
    End Sub

    Thanks,

    Les

    These are two issues.

    One is: Is it correct just to declare some missing variables that had a meaning on your other project and have been set before? If we are still talking about copying code from one to another project, you must analyze the original project. First step is selecting "go to definition" for the assigned variables.

    The other one is obvious:
    - Fi.Length returns a Long. You can not assign a Long to an ULong. Decalare TotaFileSize As Long.
    - RecordSize: I do not know the type of 'tmp', and I can only guess that the Len() function is from the MSVB library. Declaring RecordSize As Long should solve it.
    - Declare indexcnt and EPDcnt As Integer.


    Armin




    Saturday, July 28, 2012 10:18 AM
  • Hi Armin,

    Here we are not talking about copying code.  If Fi.Length returns a long how to I take into account when Fi.Length exceeds the size of Long which can happen?

    You are right about the Len() function but the reason I chose to make Recordsize uLong it may again exceed the Long format.

    What am I to do in these cases?

    Thx,

    Les

    Saturday, July 28, 2012 11:45 AM
  • Here we are not talking about copying code.  If Fi.Length returns a long how to I take into account when Fi.Length exceeds the size of Long which can happen?

    You are right about the Len() function but the reason I chose to make Recordsize uLong it may again exceed the Long format.

    What am I to do in these cases?

    There was a transition from Integer to Long concerning file length a while ago because there was a 4 GB size limit otherwise. However, I don't think the Long type has to be questioned nowadays because it allows file sizes of  9,223,372,036,854,775,807 bytes. That's about 9 exa bytes. (~9 Million terra bytes). 

    And I also don't think a record size, whichever record it is, will exceed this size at least in the near future. 

    :-)


    Armin


    Saturday, July 28, 2012 11:52 AM
  • This is (approximately) an exa byte:

    Let's assume...:

    • One letter in a book is one byte.
    • One page has 1000 letters.
    • The book has 1000 pages.
    • Put ten of the books into a shelf side by side.
    • Fill ten rows with ten books each.
    • Put ten of these shelves into a room.
    • The room is one of ten rooms in the same floor, each with ten shelves.
    • The house has ten of these floors.
    • There's a city, each with 10,000 of these houses.
    • There are 1,000 of these cities.
    • You now have one exa byte of letters in all books of all cities.

    Armin

    Saturday, July 28, 2012 12:15 PM
  • Hi Armin,

    I have to agree with you I am not approaching those numbers <S>.  You know let me ask you.  I thought the only difference between a long and uLong was long could have as many numbers as Ulong did but that half the numbers were to the left of 0 and the other half were to the right of the 0.  This may be one aspect I was confused about.  Is this the reason why I am getting atleast the error that says disallows implicit conversions?

    Thx

    Saturday, July 28, 2012 12:16 PM
  • You're right, that's the difference.

    The error is because you can't store a negative number in an ULong (unsigned long). If you are sure the Integer value will never be negative, you can convert to an ULong:

         Dim u As ULong
         Dim l As Long

         u = CULng(l)


    Armin

    • Marked as answer by Les2011 Sunday, July 29, 2012 4:26 AM
    Saturday, July 28, 2012 12:22 PM
  • Ok Armin so far I am finally getting this.  Here is my next issue that I cant resolve.  Whats wrong with the following:

    Dim GameHolder() As String
    Dim indexcnt As Long
    		
    	indexcnt = 0
             Do
                indexcnt = indexcnt + 1
                ReDim Preserve GameHolder(indexcnt)
                GameHolder(indexcnt) = tmp

    The above keeps giving me the message disallows implicit conversions from Long to Integer??

    How am I doing that?  I clearly make indexcnt as long and it just doesnt like it

    Saturday, July 28, 2012 12:36 PM
  • Do you expect more than 2 billion items in the array? Declare indexcnt as Integer.

    Armin

    Saturday, July 28, 2012 12:56 PM
  • Hi Armin,

    I see your point Armin.  Let me give this some thought and I will post later if there are still issues.  Thx Armin for helping develope a better understanding of data formats.

    Les

    Saturday, July 28, 2012 1:07 PM
  • VB array indexes are defined as type Integer.

    You have declared indexcnt as a Long. Using indexcnt as an array index, as in a statement like:

    ReDim Preserve GameHolder(indexcnt)

    requires that indexcnt be implicitely converted from Long to Integer. As a Long can potentially hold a value too big to fit into an Integer, Option Strict ruled disallow the potentially dangerous conversion.

    This is why you've been told to turn Option Strict on, so you don't write dangerous code and only discover the problems at run time.

    An Integer "Holds signed 32-bit (4-byte) integers that range in value from -2,147,483,648 through 2,147,483,647", so unless you need an array with more than 2 billion elements, why use a Long for an index?

    Saturday, July 28, 2012 1:09 PM
  • Armin another mystery!

    Dim tarRow as integer
    Dim move As String
    	move = "c4"
    	tarRow = Val(Microsoft.VisualBasic.Right(move, 1))
    	Select Case Len(move)
            	Case Is = 2
                         tarRow = Val(Microsoft.VisualBasic.Right(move, 1))

    In the code above why am I getting the error Disallows implicit conversions from Double to Integer.  Now I am confused

    Saturday, July 28, 2012 1:20 PM
  • I don't understand why you are confused. It's the same as with the other questions you had before. The Val function returns a Double whereas tarRow is declared as an Integer. You can not assign a Double to an Integer.

    In addition, I strongly recommend not making use of the very ancient Val function. Use Integer.Parse instead:

        tarRow = Integer.Parse(Microsoft.VisualBasic.Right(move, 1))

    EDIT: Also you should start using the method of the objects instead of using the VB6 style functions. A String has a Length property:

        Select case move.Length


    Armin


    • Edited by Armin Zingler Saturday, July 28, 2012 1:24 PM
    • Marked as answer by Les2011 Sunday, July 29, 2012 4:25 AM
    Saturday, July 28, 2012 1:23 PM
  • Thx for the thorough explanation.  I think initially part of my problem was being use to integer in VB6 to go up to 32767 but I am beginnng to understand all the complexiites of formats talking to you and Armin

    Thx

    Saturday, July 28, 2012 1:30 PM
  • Ok Armin good advice.  I had no idea that the Val function returns a Double.  Had I known that I wiould have clearly understood the conversion issues.  Perhaps you can recommend where I can find out what the various functions return as far as the data type.

    Saturday, July 28, 2012 1:34 PM
  • Perhaps you can recommend where I can find out what the various functions return as far as the data type.

    Hover the mouse over the function. The tooltip displays the type of the return value. In this case, i.e. if there's an error, error information has priority over function information, so use one of the following alternatives.

    Press Ctrl+I on the word Val.

    Intellisense also shows function information while typing the word Val.

    Or go to the definition of the function  (press Shift+F2 on the word Val (or whichever shortcut it is on your machine))

    Or press F1 and read it in the documenation.


    Armin


    • Edited by Armin Zingler Saturday, July 28, 2012 1:40 PM
    • Marked as answer by Les2011 Sunday, July 29, 2012 4:24 AM
    Saturday, July 28, 2012 1:39 PM
  • Thx will do
    Saturday, July 28, 2012 1:43 PM
  • Hi Armin,

    Thank you for the above helpful hints, wow I wish I knew that when I started learning vb.net.  I have been cleaning up my code as you suggested but came across another issue.

            Dim TotalFileSize As Long
            Dim RecordSize As Long
    TotalFileSize = fi.Length 'this is long and no problem
    but here is the problem:
    ProgressBar1.Value = RecordSize * 100 / TotalFileSize
    apparently ProgressBar1.Value is an Integer
    TotalFileSize has to be a long because of fi.Length and thats where the conflict happens

    How do I get around this issue?

    Les

    Saturday, July 28, 2012 5:53 PM

  •         Dim TotalFileSize As Long
            Dim RecordSize As Long
    TotalFileSize = fi.Length 'this is long and no problem
    but here is the problem:
    ProgressBar1.Value = RecordSize * 100 / TotalFileSize
    apparently ProgressBar1.Value is an Integer
    TotalFileSize has to be a long because of fi.Length and thats where the conflict happens

    How do I get around this issue?

      ProgressBar1.Value = CInt(Math.Floor(RecordSize * 100 / TotalFileSize))

    I use Math.Floor here because I do not like progressbars that show 100%  even though progresss is < 100%. That means, 99.6% will be rounded to 99%. As Math.Floor returns a Double and the Value property is type Integer, CInt is necessary.


    Armin


    • Edited by Armin Zingler Saturday, July 28, 2012 6:24 PM 99.3 changed to 99.6 (makes more sense)
    • Marked as answer by Les2011 Sunday, July 29, 2012 4:24 AM
    Saturday, July 28, 2012 6:16 PM
  • Hi Armin,

    I like that Math.Floor function!  Listen where do you find all these little bells and whistles?  I bought the book titled Visual BAsic 2010 Programmers Reference book and although it has a lot of info, the things you have helped me with I dont seem to be able to find.  Can you recommend a book perhaps?  I have been using VB6 and prior versions and am new as you know to vb.net.

    One last thing. I am getting just a warning for the following:

    Dim SW As StreamWriter

    sw is used before it has been assigned a value.  Since sw changes throughout several parts I am not quite sure how to properly assign a premature value to it to make the warning go away.

    Thanks for all your help and clarifying so many aspects of variable formats and those several cutsie functions you brought to my attention.

    Les

    Sunday, July 29, 2012 12:59 AM
  • Hi Les,

    I do not have a book so I can not recommend one (well, I have one called "Design patterns" but I had only a quick glance before putting it aside during my VB6 times). It's just trying, reading documentation, reading answers here, playing with the object browser ("what do we have here?"), writing code, and more code. Or I read an interesting question here in the forums, don't know how it's done, do some research (learning new things), answer.

    I think most important is knowing how to use the environment in order to quickly move around. Don't believe I always open "System", then "Runtime", then "InteropServices" in order to navigate to the "Marshal" class in the object browser. No, I enter "Marshal" in the symbol search window, then press enter in the search result. Only an example, not a secret.

    Regarding the error message: Could you please post some more code lines so that I see the context?


    Armin

    Sunday, July 29, 2012 1:23 AM
  • Hi Armin I believe I answered this postbut I do not seeit.  No need to submit codeI figured it out without picking at your brain <S> Thx for all the helpful pointers. BTW in a prior post you mentioned I should get away from using the Val function and use the integer.parse, is there a similar suggestion for a replacement for the Len function?

    Les

    Tuesday, July 31, 2012 3:30 AM
  • BTW in a prior post you mentioned I should get away from using the Val function and use the integer.parse, is there a similar suggestion for a replacement for the Len function?

    Hi Les,

    a String has a Length property. For example:

          If StringVar.Length > 5 Then

    Be aware that String is a reference type, i.e. a String variable can be Nothing. If 'StringVar' is Nothing, the line above would fail (NullReferenceException) as there is no String of which to determine the length. You would have to verify this before checking the Length:

          If StringVar IsNot Nothing Then
               If StringVar.Length > 5 Then
                     '....
               End If
          End If

    Or in short:

          If StringVar IsNot Nothing AndAlso StringVar.Length > 5 Then
                     '....
          End If

    In another case you may want to check if the variable is Nothing or, otherwise, the String length is zero. Then you can either write

          If StringVar Is Nothing OrElse StringVar.Lenth = 0 Then

    or

          If String.IsNullOrEmpty(StringVar) Then


    Armin


    • Edited by Armin Zingler Tuesday, July 31, 2012 3:40 AM
    • Marked as answer by Les2011 Tuesday, July 31, 2012 3:59 AM
    Tuesday, July 31, 2012 3:40 AM
  • Hi Armin,

    Gotcha! Easy enough, very logical. Understand clearly.

    Thx,

    Les

    Tuesday, July 31, 2012 3:59 AM