String to Image for a Picturebox - vb.net

I have an ArrayList, each string of the ArrayList has as an entry the path of an exact image stored into my Resources (ex. Arraylist(0) = "My.Resources.img1").
I want to display some images in Pictureboxes, I thought that this code should do the trick... But there is a conflict, the app runs without errors but not a single image isn't displayed. I also checked the entries, the path are correct...
Dim res As Resources.ResourceManager
res = New Resources.ResourceManager("Picture_Quiz.Resources", System.Reflection.Assembly.GetExecutingAssembly)
'AnswersList is the ArraryList that contains the paths'
PictureBoxA.Image = resGetObject(AnswersList(0))
'I tried to use is as a String Variable instead of string from ArrayList (for no reason)'
Dim img As String
img = My.Resources.img1
PictureBoxB.Image = res.GetObject(img)
PictureBoxC.Image = res.GetObject(AnswerList(2))
PictureBoxD.Image = res.GetObject(AnswerList(3))
Any ideas what is going on?

have you considered re-writing the code so that you store an array of Enums?
Enum ResourcePics
Pic1
Pic2
Pic3
End Enum
then use a select to convert the Enum to the resource Image
Select Case AnswersList(index)
Case ResourcePics.Pic1
Return My.Resources.Pic1
Case ResourcePics.Pic2
Return My.Resources.Pic2
Case ResourcePics.Pic3
Return My.Resources.Pic3
End Select

Related

VB Loading a list of variables from a text document

I'm currently trying to load a list of variables that are formatted like this:
5,
6,
3,
3,
etc, and I'm trying to output them to variables like this:
Strength = variablesList(1)
Agility = variablesList(2)
But so far, I've not been able to find a solution that seems to work for what I'm trying to do.
I'm currently working with:
Dim destination As String = Environment.GetFolderPath("C:\Roll20Output\Class" + outputClass + "2.txt")
Dim FileReader1 As New StreamReader(destination)
Dim Contents1 As String
Dim index As Integer = 0
While FileReader1.Peek <> -1
Contents1 = FileReader1.ReadLine
Dim array As New ArrayList
array.AddRange(Contents1.Split(","))
variablesList.Add(array)
End While
Strength = variablesList(1)
Agility = variablesList(2)
But so far I can't seem to get anything to output.
Would anyone be able to help?
Thanks
You are using a lot of outdated stuff in your code (reading a file with StreamReader, ArrayList instead of List<T>, etc.). I would suggest the following (untested):
' Returns an array with one string per line
Dim lines = File.ReadAllLines("C:\...\SomeFile.txt")
' Remove trailing `,` - LINQ magic
lines = (From s In lines Select s.TrimEnd(","c)).ToArray()
Dim strength = CInt(lines(0))
Dim agility = CInt(lines(1))
...
If you get rid of the useless trailing commas, you can skip the second step. If you use only commas instead of new lines, the first step becomes:
Dim lines = File.ReadAllText("C:\...\SomeFile.txt").Split(","c)

Cant get image from resources

My resource1 contains graphic files named dot1, dot2 , dot3 etc. and each represent image of dice wall.
My game class have method updateView()
Public Sub updateView()
Dim imageName As String
image = "dot" & ToString(dice1.getDots())
Form1.PictureBox1.Image = CType(My.Resources.ResourceManager.GetObject(imageName ), Image)
image = "dot" & ToString(dice2.getDots())
Form1.PictureBox2.Image = CType(My.Resources.ResourceManager.GetObject(imageName ), Image)
... etc.
End Sub
I want to update 5 pictureboxes on form1 with images from reasource box depending on rolled number. This code isnt working and i dont know why.
ps. sorry for bad english
It appears that you're trying to GetObject(imageName), where imageName is declared as an empty string. You can solve this two ways, either define imageName, or search for the string you create in the image variable:
Dim imageName As String
imageName = "dot" & ToString(dice1.getDots())
Form1.PictureBox1.Image = CType(My.Resources.ResourceManager.GetObject(imageName ), Image)
or,
image = "dot" & ToString(dice1.getDots())
Form1.PictureBox1.Image = CType(My.Resources.ResourceManager.GetObject(image), Image)
In either case, using Option Strict would help to solve problems like these.

Values in list of structures don't change

I have defined a structure in my code and have a list of this structures"
Structure Parcel
Public name As String
Public type As String
End Structure
Dim ParcelList As New List(Of Parcel)
Then I'm trying to set some values to an element of the list which name is known to me
For Each myParcel As Parcel In ParcelList
If (myParcel.name = "Parcel1") Then
myParcel.type="Type1"
End If
Next
Unfortunately values in my list don't change at all. what am I doing wrong?
As Parcel is a Structure, it is passed by value so when iterating through collection, you are modifying a copy of your structure.
To better understand this case, you should understand what For Each really is. Your code can be translated into:
Dim enumerator As List(Of Parcel).Enumerator = ParcelList.GetEnumerator()
While enumerator.MoveNext()
' Here you have a local copy of your Structure
Dim myParcel As Parcel = enumerator.Current
Dim flag As Boolean = Operators.CompareString(myParcel.name, "Parcel1", False) = 0
If flag Then
' Here you modify your local copy
myParcel.type = "Type1"
End If
End While
If Parcel was a Class, it would be passed by reference so no local copy would be created and line myParcel.type = "Type1" would change proper object existing in your collection.
As already Stated this is because you are modifying a local copy of a value type. One way round this is to access the items in the list by ordinal and replace the ordinal value type with a new type:
For i As Integer = 0 To ParcelList.Count - 1
If ParcelList(i).name = "Parcel1" Then
ParcelList(i) = New Parcel With {.name = ParcelList(i).name, .type = "Type1"}
End If
Next
But really you should change the Sturcture to a Class
When checking for strings use Equals instead of '='.
If (myParcel.name.equals("Parcel1")) Then
myParcel.type="Type1"
End If
Strings are in fact 'Objects'. When you compare Strings (example StringA = StringB), you check the allocation of the String in Memory instead of the contents of the string.
Even better would be:
If (myParcel.name.ToUpper().equals(Cstr("Parcel1").toUpper())) Then
myParcel.type="Type1"
End If
That way you ignore any difference case-wise.
example:
myParcel.name = "teST"
myParcel.name.equals("test")
is False
myParcel.name.ToUpper().equals(Cstr("test").toUpper())
is true

How to assign a value to an array from a combobox

The code I have is:
Dim Dbase() As String = Nothing
Dbase(0) = Db_ComboBox.Text
I have declared Dbase as array and assigned Nothing, Db_ComboBox is a combobox.
For that assignment statement, I'm getting the following error: "Reference 'Dbase' has a value of 'Nothing'"
What is the reason for this error, and how can I take the value from the combobox and save it in the array?
You need to change this:
Dim Dbase() As String = Nothing
to this (declare an array of 1 element):
Dim Dbase(0) As String
And then this line will work:
Dbase(0) = Db_ComboBox.Text
If you need to change your array size you can use Redim or Redim preserve, as required.
If you anticipate contents of Dbase to change often, I am all with #Joel's suggestion about switching to List(Of String) instead of handling array sizes manually.
Let's look at your code:
Dim Dbase() As String = Nothing
Dbase(0) = Db_ComboBox.Text
Especially the first line. That first line creates a variable that can refer to an array, but the = Nothing portion explicitly tells it, "Do not create a real array here yet". You have, effectively, a pointer that doesn't point to anything.
I get here that what you really need is a List collection that you can append to over time:
Dim Dbase As New List(Of String)()
Dbase.Add(Db_ComboBox.Text)
Dbase() IS NOTHING. Look at this example:
cargoWeights = New Double(10) {}
atmospherePressures = New Short(2, 2, 4, 10) {}
inquiriesByYearMonthDay = New Byte(20)()() {}
That's how you declare arrays.
More examples: http://msdn.microsoft.com/en-us/library/vstudio/wak0wfyt.aspx

Why won't this list of struct allow me to assign values to the field?

Public Structure testStruct
Dim blah as integer
Dim foo as string
Dim bar as double
End Structure
'in another file ....
Public Function blahFooBar() as Boolean
Dim tStrList as List (Of testStruct) = new List (Of testStruct)
For i as integer = 0 To 10
tStrList.Add(new testStruct)
tStrList.Item(i).blah = 1
tStrList.Item(i).foo = "Why won't I work?"
tStrList.Item(i).bar = 100.100
'last 3 lines give me error below
Next
return True
End Function
The error I get is: Expression is a value and therefore cannot be the target of an assignment.
Why?
I second the opinion to use a class rather than a struct.
The reason you are having difficulty is that your struct is a value type. When you access the instance of the value type in the list, you get a copy of the value. You are then attempting to change the value of the copy, which results in the error. If you had used a class, then your code would have worked as written.
try the following in your For loop:
Dim tmp As New testStruct()
tmp.blah = 1
tmp.foo = "Why won't I work?"
tmp.bar = 100.100
tStrList.Add(tmp)
Looking into this I think it has something to do with the way .NET copies the struct when you access it via the List(of t).
More information is available here.
Try creating the object first as
Dim X = New testStruct
and setting the properties on THAT as in
testStruct.blah = "fiddlesticks"
BEFORE adding it to the list.