![]() |
|
[C#] Changing properties with variables - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C#] Changing properties with variables (/showthread.php?tid=5572144) |
Changing properties with variables - VirtualFrost - Mar 11, 2017 I already have a variable which stores the result of a random object, now I want to use that number to load the image. The images are labelled1, _2, _3 etc etc To explain it, I want it something like this: diceBox1.Image = VirtualGame.Properties.Resources. + diceVal1; So say the variable diceVal1 has a value of 2, I want it to load image _2. How can I go about doing this? * - ShadowCloudGH - Mar 11, 2017 (Mar 11, 2017 12:35 AM)VirtualFrost Wrote: I already have a variable which stores the result of a random object, now I want to use that number to load the image. I'm not sure what you are struggling with? Back before string interpolation I would do this with string.format("{0}_{1}.{2}", VirtualGame.Properties.Resources, diceVal, imgExtensions ) Where imgExtension is jpg or gif or whatever you are using. It's not clear to me what you are struggling with? Did you want an extensionMethod on some class so you could go diceVal1.GetImagePath() or did you just want to wrap the above in a method? This is rather easily achieved with string interpolation, although I'm not sure if the multiline issue has been fixed... I know this compiled to string + string previously for multiline values which ended up causing performance hickups with large amounts of iteration, I am sure it has been fixed though, so feel free to use that instead of string.Format * - VirtualFrost - Mar 11, 2017 (Mar 11, 2017 12:56 AM)ShadowCloudGH Wrote: I'm not sure what you are struggling with? I'm not that great at programming so I'll explain it the best I can without using the wrong terminology diceBox1.Image = VirtualGame.Properties.Resources._ + diceVal1; ^^ This does not work. Basically to put strings and variables together, you'd generally go like "The value is " + randomValue; right? I'm trying to do the same here basically diceBox1.Image = VirtualGame.Properties.Resources._ + diceVal1; would become diceBox1.Image = VirtualGame.Properties.Resources._1; if diceVal1 = 1. I have a roll button, each time I click it, diceVal1 will change to a random number between 1-10, then I want to use that value so it can load the correct image. Does that help? * - ShadowCloudGH - Mar 11, 2017 (Mar 11, 2017 01:15 AM)VirtualFrost Wrote: I'm not that great at programming so I'll explain it the best I can without using the wrong terminology Can you let me know what you get when you change from diceBox1.Image = VirtualGame.Properties.Resources._ + diceVal1 to diceBox1.Image = VirtualGame.Properties.Resources + "._" + diceVal1.ToString() * - VirtualFrost - Mar 11, 2017 (Mar 11, 2017 01:44 AM)ShadowCloudGH Wrote: Can you let me know what you get when you change from http://prntscr.com/eikpj6 * - Domyos - Mar 11, 2017 I think the way i uses to load images in c# the first semester was using something like DiceBox1.image = Properties.Resources.ResourceManager.GetObject("_" + diceVal1. ToString() Not exactly sure anymore tho * - ShadowCloudGH - Mar 11, 2017 (Mar 11, 2017 01:51 AM)VirtualFrost Wrote: http://prntscr.com/eikpj6 Ah, now it makes sense. Currently you are pointing to the class Resources, rather than a specific property in the resources. You are using Properties.Resources You should be using Properties.Resources.ResourceName There's a nice example from http://stackoverflow.com/questions/90697/how-to-create-and-use-resources-in-net Code paused = !paused;You would use GetObject if the resource was the actual image, GetString for a string with the image path rather than the image object ;) * - VirtualFrost - Mar 11, 2017 (Mar 11, 2017 01:58 AM)ShadowCloudGH Wrote: Ah, now it makes sense. Currently you are pointing to the class Resources, rather than a specific property in the resources.The end result I want is diceBox1.Image = Properties.Resources._1; (If diceVal1 = 1, so yes you're right) That's what I'm trying to work out though, I can't have that always be static, I need the image to change depending on the result of diceVal1. How can I change 1 to be the result of diceVal1? I'm not asking how to use Properties.Resources, I'm asking how can I change the image using that so it changes each time the value changes. (Mar 11, 2017 01:58 AM)Domyos Wrote: I think the way i uses to load images in c# the first semester was using something like Error for me, cannot convert object to System.Drawing * - ShadowCloudGH - Mar 11, 2017 (Mar 11, 2017 02:09 AM)VirtualFrost Wrote: The end result I want is O, for some reason I was under the impression you have the resource pointing to a base path, like "/images/dicepicturefor" And in the actual Images folder you have dicepicturefor_1.jpg dicepicturefor_2.jpg dicepicturefor_3.jpg And so on and so forth. From the last message I understand you have different properties in your resources that you would like to access dynamically like this: Code ResourceManager rm = new ResourceManager(typeof(YourResourceClass));* - BahNahNah - Mar 11, 2017 (Mar 11, 2017 02:09 AM)VirtualFrost Wrote: Error for me, cannot convert object to System.Drawing Cast it to a Bitmap/Image. * - Owl - Mar 11, 2017 (Mar 11, 2017 02:09 AM)VirtualFrost Wrote: The end result I want is Try a cast Code var r = new Random();* - VirtualFrost - Mar 11, 2017 (Mar 11, 2017 02:18 AM)Orka Wrote: Try a cast Brilliant, this is what I needed, works perfectly. Thanks |