locked
Is it possible to define one variable on top of another? RRS feed

  • Question

  • User-158480776 posted

    Going back to my earliest days of computer programming I used PL/1.

    In that language there was an ability to define one variable on top of another so to say.

    I cannot remember the exact syntax but in essence, using VB syntax (plus in square brackets an english phrase to explain the idea), the facility was something like this: -

    Dim Series_String as String
    Dim Series_Array(6) [redefining Series_String] As Char
    
    

    Is this possible in VB, and if so what is the correct syntax, please?

    Wednesday, January 2, 2013 1:12 AM

Answers

  • User-1716253493 posted
            Dim Series_String As String
            Dim Series_Array() As Char = Series_String.ToCharArray

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 2, 2013 1:50 AM

All replies

  • User-1716253493 posted
            Dim Series_String As String
            Dim Series_Array() As Char = Series_String.ToCharArray

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 2, 2013 1:50 AM
  • User-158480776 posted

    Thank you  Smile

    Wednesday, January 2, 2013 7:05 AM
  • User-158480776 posted

    Dear Oned

    I have incorporated your definition in the following code and, on running, I receive the error shown below.

         Dim HWB_Database As Database = Database.Open("How_and_Why_Wonder_Books")
         Dim HWB_Publishers_All_sqlCommand = "SELECT * FROM Publishers ORDER BY Publisher_Code"
         Dim HWB_Series_sqlCommand = "SELECT Image_Name FROM Series WHERE Series_Code = This_Series_Letter"
    
         Dim Publishers_Data = HWB_Database.Query(HWB_Publishers_All_sqlCommand)
         Dim Publishers_Grid = New WebGrid(Publishers_Data)
         Dim Publishers_Count As Integer = Publishers_Data.Count
         Dim Publishers_Series_Table = New Dictionary(Of String, String)()
    
         Dim Series_String As String
         Dim Series_Array() As Char = Series_String.ToCharArray
         Dim Num_Series_This_Publisher As Integer
         Dim This_Series_Letter As Char
         Dim Publisher_Series_Dictionary_Index_Word As String
         Dim Series_Thumb_URL As String
    
         Dim I, J As Integer
    
         For Each Publisher_Name As String in Publishers_Data
    
             For Each Series_Published as String in Publishers_Data
    
                 Series_String = Series_Published
                 Num_Series_This_Publisher = Len(Series_String)
    
                 For I = 1 to Num_Series_This_Publisher
    
                     This_Series_Letter = Series_Array(I)
                     Series_Thumb_URL = HWB_Database.Query(HWB_Series_sqlCommand)
                     Publisher_Series_Dictionary_Index_Word = Publisher_Name & This_Series_Letter
                     Publishers_Series_Table(Publisher_Series_Dictionary_Index_Word) = Series_Thumb_URL
                    
                 Next I
    
             Next
    
         Next
    

    The error received is as follows -

    Server Error in '/' Application.


    Object reference not set to an instance of an object.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    Line 31: 
    Line 32:      Dim Series_String As String
    Line 33: Dim Series_Array() As Char = Series_String.ToCharArray
    Line 34: Dim Num_Series_This_Publisher As Integer Line 35: Dim This_Series_Letter As Char


    Source File: C:\Users\PhilipMark\Documents\My Web Sites\How_and_Why_Wonder_Books\Publishers.vbhtml    Line: 33

    Stack Trace:

    [NullReferenceException: Object reference not set to an instance of an object.]
       ASP._Page_Publishers_vbhtml.Execute() in C:\Users\PhilipMark\Documents\My Web Sites\How_and_Why_Wonder_Books\Publishers.vbhtml:33
       System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
       System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +69
       System.Web.WebPages.WebPage.ExecutePageHierarchy() +151
       System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
       System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext) +114
    



    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18010

    *****

    If you have any suggestions to fix this error I would be most grateful.

    Best regards

    Philip

    Wednesday, January 2, 2013 11:08 PM
  • User-489377026 posted

    Line 32: Dim Series_String As String Line 33: Dim Series_Array() As Char = Series_String.ToCharArray

    Please set the value to Series_String

    Best Regards

    Thursday, January 10, 2013 10:14 PM
  • User-1983115724 posted

    Try This code

     Dim HWB_Database As Database = Database.Open("How_and_Why_Wonder_Books")
         Dim HWB_Publishers_All_sqlCommand = "SELECT * FROM Publishers ORDER BY Publisher_Code"
         Dim HWB_Series_sqlCommand = "SELECT Image_Name FROM Series WHERE Series_Code = This_Series_Letter"

         Dim Publishers_Data = HWB_Database.Query(HWB_Publishers_All_sqlCommand)
         Dim Publishers_Grid = New WebGrid(Publishers_Data)
         Dim Publishers_Count As Integer = Publishers_Data.Count
         Dim Publishers_Series_Table = New Dictionary(Of String, String)()

         Dim Series_String As String
         Dim Series_Array() As Char
         Dim Num_Series_This_Publisher As Integer
         Dim This_Series_Letter As Char
         Dim Publisher_Series_Dictionary_Index_Word As String
         Dim Series_Thumb_URL As String

         Dim I, J As Integer

         For Each Publisher_Name As String in Publishers_Data

             For Each Series_Published as String in Publishers_Data

                 Series_String = Series_Published
                 Series_Array()  = Series_String.ToCharArray
                 Num_Series_This_Publisher = Len(Series_String)

                 For I = 1 to Num_Series_This_Publisher

                     This_Series_Letter = Series_Array(I)
                     Series_Thumb_URL = HWB_Database.Query(HWB_Series_sqlCommand)
                     Publisher_Series_Dictionary_Index_Word = Publisher_Name & This_Series_Letter
                     Publishers_Series_Table(Publisher_Series_Dictionary_Index_Word) = Series_Thumb_URL
                    
                 Next I

             Next

         Next

    Friday, January 11, 2013 5:24 PM