Random Images from Resources

Socarsky

Well-known member
Joined
Dec 27, 2012
Messages
173
Location
Jakarta/Indonesia
Programming Experience
Beginner
As you can see there is a Select Case expression to choose an image which is in Recources,
so far so good and actually the project works well but I wanted it first with single line code as an expression of Random.
But I could not find a way to add my expression after "My.Recourse" so do you know a solution of this?


99934565.png
 
Each resource is accessed via a property so, just like any property, you have to specify the name at design time. If you navigate into the code for any one of those resources (which you can do right-clicking and selecting Go To Definition) you will see that they are implemented by passing a String to a ResourceManager. You can do the same. You can access that ResourceManager via My.Resources and then do just as the auto-generated code does, using a String that you constructed to specify the resource.

By the way, the second line in each Case statement is exactly the same, which is something that you should never do. One of the most basic principles of good programming practice is DRY: don't repeat yourself. That second line is going to be executed no matter which Case is matched so it doesn't belong in the Select Case. It should appear once after the End Select.
 
Each resource is accessed via a property so, just like any property, you have to specify the name at design time. If you navigate into the code for any one of those resources (which you can do right-clicking and selecting Go To Definition) you will see that they are implemented by passing a String to a ResourceManager. You can do the same. You can access that ResourceManager via My.Resources and then do just as the auto-generated code does, using a String that you constructed to specify the resource.
Which one of those property's item that you mentioned and how do I add my random expression?
image1smx.png


jmcilhinney said:
By the way, the second line in each Case statement is exactly the same, which is something that you should never do. One of the most basic principles of good programming practice is DRY: don't repeat yourself. That second line is going to be executed no matter which Case is matched so it doesn't belong in the Select Case. It should appear once after the End Select.
You mean this "Label1.Text = strLabel & strScale" then How can I make it like your suggestion dude? Give me example please if possible.
 
You might try doing what I actually said. In your code, right-click on one of the properties of My.Resources that refers to one of your resources and select Go To Definition and look at how the property is implemented and then do the same in your own code.
 
Sorry, but I cannot overcome the difficulties to make it as your offered. I tried a few combination with codes but fail.
 
Sorry, but I cannot overcome the difficulties to make it as your offered. I tried a few combination with codes but fail.
Then you did it wrong. If you're waiting for me to guess what you did so I can tell you what's wrong with it then you'll be waiting a long time. Show me the code that you think you should be using and then I can tell you how to fix it.
 
Useless.. :(

Public Class Form1
    Private resourceCulture As Global.System.Globalization.CultureInfo

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim rndEngine As New Random
        Dim strLabel As String = "Selected Image-> "
        Dim strScale As String = "Scale"
        Dim iNumber As String = 0

        iNumber = rndEngine.Next(1, 13)
        strScale += CStr(iNumber)

        Dim obj As Object = My.Resources.ResourceManager.GetObject("strScale", resourceCulture)
        picMyImage.Image = obj

   End Sub
End Class
 
You seem to be completely ignoring the basic principles of VB.NET programming. If you wanted to display the contents of that 'strScale' variable to the user, would you do this:
VB.NET:
MessageBox.Show("strScale")
Of course you wouldn't, because would just display the text "strScale" to them. You would do this:
VB.NET:
MessageBox.Show(strScale)
That's obvious, right? So what exactly are you expecting this to do:
VB.NET:
Dim obj As Object = My.Resources.ResourceManager.GetObject([B][U]"strScale"[/U][/B], resourceCulture)
Also, either assign a value to that 'resourceCulture' variable or don't use it at all.
 
Ingnore that dude sorry its a mistake that how I put that double quotes there. They dont be in the code, because its not a string.
 
How can you make mistakes with the code you post when you should be simply copying and pasting? This is wasting my time and trying my patience. I'm not going to keep trying to drag every little piece of information out of you when it's what you should have posted in the first place without prompting. EVERY time you post a question there are three basic things that MUST provide:

1. What you're trying to accomplish.
2. What you've done, which ALWAYS includes code if there is any.
3. What happens, which ALWAYS includes error messages if there are any.

So, if you have code that doesn't work then either you are seeing unexpected behaviour or you are seeing an error message. If you want to keep that information from the people who are trying to help you then you're not going to get any help.
 
How can you make mistakes with the code you post when you should be simply copying and pasting? This is wasting my time and trying my patience. I'm not going to keep trying to drag every little piece of information out of you when it's what you should have posted in the first place without prompting. EVERY time you post a question there are three basic things that MUST provide:

1. What you're trying to accomplish.
2. What you've done, which ALWAYS includes code if there is any.
3. What happens, which ALWAYS includes error messages if there are any.

So, if you have code that doesn't work then either you are seeing unexpected behaviour or you are seeing an error message. If you want to keep that information from the people who are trying to help you then you're not going to get any help.

You must assume that I never in idea that out of usage string value's expression. Of course I know how I use of string and a string's value first! But you don't assume that and keep that idea that I dont have an ability to write it with right way.
Nevermind, I dont need your consultancy anymore.. You better to ignore my post to to answer anymore. Sorry for giving occupation to you.
 
Hi,

I am an advocate of trying to help people but that does not always mean that it has to be in the realm of programming as this forum suggests.

So, my advice for today would be, if you do not FULLY and TRULY understand the advice of a PROFESSIONAL then keep REASEARCHING what you have been told until it starts to make sense. If it still does not make sense then ask more pertinent questions based on your research until you understand what had been said to you.

As it is, do you know what the term, "SHOOTING YOURSELF IN THE FOOT" means? If not, then I strongly suggest that you do some more research until you do know what this means and then make another post to jcmilhinney.

You are still a young programmer from what I can see and to cut off your avenues of help at this point in your development is a MISTAKE. Believe me, it's true, and you will REGRET it in the future if you do not do something about this.

Hope that helps and good luck,

Cheers,

Ian
 
Last edited:
Back
Top