Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Beginning VB 6
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 26th, 2007, 06:07 PM
Authorized User
 
Join Date: Feb 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default Retrieving resources in sequence

I have several hundred strings stored in string resources. I need to go through them all and select various ones based on other criteria. In VB5 I could sequence the resource identifier and use a for next loop to retrieve the appropriate strings.

Example
  for i = 1000 to 2000
    if somevalue > anothervalue then
      LoadResString(i)
    endif
  next

That's 5 lines of code to go through 1000 strings. Is there any way to do this in VBExpress? If so, how?

 
Old March 1st, 2007, 06:56 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

I looked at VBExpress’s site. It does not look like VBExpress is a programming language, but a tool for generating boilerplate to fit your environment. Is that right?

Did you mean to ask “Is there any way to do this in VB.NET? If so, how?”

Could you expound on what you need a little?
 
Old March 8th, 2007, 06:38 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Lowell,

Since you posted this question again at http://p2p.wrox.com/topic.asp?TOPIC_ID=57372, I presume that this answer didn’t suffice.

I did ask you to expound. Would you please?

Could you clarify the environment you are working in, the way you have tried to accomplish this, and what the problems are you have encountered (raises an error or not; partially cucceeds; etc.)?

Dialog is better than just posting a question then abandoning it...
 
Old March 9th, 2007, 12:18 PM
Authorized User
 
Join Date: Feb 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry about the duplicate posts.

I may not know enough to provide an adequate answer to you questions. But I'll try.

As far as I know VBExpress is a reduced version of VB.net. You can write complete programs and compile them in VBExpress. When I go to About under the Help menu it shows: Microsoft Visual Basic 2005 Express Edition. It also shows: Microsoft Visual Studio 2005 Version 8.0.50727.762 ... and Microsoft .NET Framework Version 2.0.50727 ...

Back to my original question.
In VB5 strings in the Resource File were numbered for identification purposes. With a For … Next loop I could retrieve the needed strings by using a variable (the string identification number) to sequence through the stings and only load those I needed at the moment.

In VBExpress or VB.NET I can't use numbers to identify the stings in the Resource File. I also can't retrieve strings with My.Resources.{variable name}.

So I guess I am asking how you sequence through a series of string resources in VB.NET.


 
Old March 9th, 2007, 02:45 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

OK.

I created a new resource file named Resource1.resx.
I added three strings to it using the resource designer.
I named them String1, String2 and String3.
The contents were (respectively): "This is string 1", "This is string 2" and "This is string 3"

Then I added a module with the following Sub:
Code:
Public Sub ReadResources()

    ' The resource filename is in the format "RootNameSpace.FileName"
    Dim TheFile As String = "DIS_Database.Resource1"

    ' Create a resource manager:
    Dim mgr As New _
        System.Resources.ResourceManager(TheFile, _
                           System.Reflection.Assembly.GetExecutingAssembly() _
                                        )

    For i As Integer = 1 To 3
        Dim CurResIdx As String = "String" & i.ToString("0")
        Dim CurResVal As String = mgr.GetString(CurResIdx)
        Debug.Print(CurResVal)
    Next

End Sub
Though the resources are not indexed per se, I myself effectively made an index for the strings by naming them with a contained sequential digit.

The routine put the strings into the immediate window in sequence, as the code indicates.

Does this get you where you want to be?
 
Old March 12th, 2007, 12:38 PM
Authorized User
 
Join Date: Feb 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The code you provided is a start. However, when I ran the program I got an unhandled exception. So far I have not been able to solve the unhandled exception.

MissingManifestResourceException was unhandled

Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "DIS_Database.Resource1.resources" was correctly embedded or linked into assembly "WindowsApplication1" at compile time, or that all the satellite assemblies required are loadable and fully signed.

I clicked "Copy exception detail to the clipboard" and got the mess below.

System.Resources.MissingManifestResourceException was unhandled
  Message="Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "DIS_Database.Resource1.resources" was correctly embedded or linked into assembly "WindowsApplication1" at compile time, or that all the satellite assemblies required are loadable and fully signed."
  Source="mscorlib"
  StackTrace:
       at System.Resources.ResourceManager.InternalGetResour ceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents)
       at System.Resources.ResourceManager.InternalGetResour ceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents)
       at System.Resources.ResourceManager.InternalGetResour ceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents)
       at System.Resources.ResourceManager.GetString(String name, CultureInfo culture)
       at System.Resources.ResourceManager.GetString(String name)
       at WindowsApplication1.Module1.ReadResources() in C:\Documents and Settings\Lowell\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Module1.vb:line 15
       at WindowsApplication1.Form1.Button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\Lowell\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb:line 4
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventAr gs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationCo ntext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsF ormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsF ormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsF ormsApplicationBase.Run(String[] commandLine)
       at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context( Object state)
       at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()


 
Old March 13th, 2007, 04:02 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

You need to read the contents of answers you get; it will save you trouble down the road.
I put in a comment:
Code:
        ' The resource filename is in the format "RootNameSpace.FileName"
Since the root namespace of your application is probably not "Dis_Database," it is equally unlikely that using that namespace will function right for you.

Read the comments I put in the code I suggested. If you do not understand the comments, come back here and I will try to clarify.

If you open your project's properties, you should find therein the root namspace. You will need to use that name and the name of [u]your</u> resource file, rather than the name of my namspace, and my resource file.
 
Old March 14th, 2007, 08:47 PM
Authorized User
 
Join Date: Feb 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I fear that neither you nor I realize how little I know. I did read your comments but don't know what a RootNameSpace is. However, I did find it under the Application tab of the project properties and correct it. This didn't solve all the problems.

In the post in which you provided the code you state that you created a "resource file named Resource1.resx". I don't know how to create a resource file in VB.NET. I just put the resources under the Resource tab in the project properties.

When I run the program I still get a MissingManifestResourceException was unhandled at the line:
Dim CurResVal As String = mgr.GetString(CurResIdx)

The exception states:
Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "WindowsApplication1.Resource1.resources" was correctly embedded or linked into assembly "WindowsApplication1" at compile time, or that all the satellite assemblies required are loadable and fully signed.

This doesn't make a lot of sense to me. I don't know what is meant by "specified culture or the neutral culture". I don't know what "satellite assemblies" are. Nor do I know what is meant by "loadable and fully signed".

I have managed to write a fairly complex program with VB5 by modifying code from various examples. Once I see a complete working example, I can usually modify it to suit my needs. Unfortunately, with VB.NET there is a lot more to making code work than just writing the code. This is where I am having trouble as you can see by what I've said I don't understand.

Any further help would be greatly appreciated.

 
Old March 15th, 2007, 02:10 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

OK. I was not aware of the tab in the project's properties, but I built a test to investigate it.

I created a new console application project named “TestResources.” that creates a root namespace of “TestResources.”

I added 2 strings to its resources tab called String1 & String2.

I added this routine in the default module that creating a new app makes for me:
Code:
Module Module1

    Sub Main()

        Dim RM As _
            New Resources.ResourceManager("TestResources.Resources", _
                                          Reflection.Assembly.GetExecutingAssembly() _
                                         )

        For i As Integer = 1 To 2
            Dim CurResIdx As String = "String" & i.ToString("0")
            Dim CurResVal As String = RM.GetString(CurResIdx)
            Debug.Print(CurResVal)
        Next

    End Sub

End Module
As with the previous code I posted, this too put the strings into the Immediate Window.
I guessed at the filename in the statement that creates the New ResourceManager().
I lucked out, and got it right.

So try that, and see what happens.
 
Old March 15th, 2007, 03:40 PM
Authorized User
 
Join Date: Feb 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Wonderful. That works just as I wanted. Thank you so much.

I wonder how many years it would have taken me to come up with the line:
        Dim RM As _
            New Resources.ResourceManager("TestResources.Resources ", _
                                          Reflection.Assembly.GetExecutingAssembly() _
                                         )
It may take me quite a while to figure out exactly what it all means.

Again, I thank you for the time and effort you spent helping me.







Similar Threads
Thread Thread Starter Forum Replies Last Post
Conditional Resources Jose Silva Visual Studio 2005 0 March 16th, 2008 04:55 PM
Retrieving strings in sequence lowell VB.NET 1 March 8th, 2007 06:31 PM
Resources mike72 ASP.NET 2.0 Professional 0 January 3rd, 2006 08:25 AM
Cleaning up resources jbenson001 ASP.NET 1.x and 2.0 Application Design 2 January 22nd, 2004 11:50 AM
Resources vijay VB.NET 2002/2003 Basics 2 November 3rd, 2003 11:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.