In a Java file I have this
Can I do something similar in VB, like thisCode:public int images_X_Y[] = {0, 0, 119, 166, 183, 147, 235, 133, 288, 118};
Code:public images_X_Y() As Integer = (0, 0, 119, 166, 183, 147, 235, 133, 288, 118)
Printable View
In a Java file I have this
Can I do something similar in VB, like thisCode:public int images_X_Y[] = {0, 0, 119, 166, 183, 147, 235, 133, 288, 118};
Code:public images_X_Y() As Integer = (0, 0, 119, 166, 183, 147, 235, 133, 288, 118)
No, you'd have to create the array and then initialize the values.
Of course one option might be something like:
If you can live with a Variant containing an array that's fine, otherwise you might use a loop to copy that into an actual Integer array.Code:Public images_X_Y As Variant
:
:
Public Sub Init()
images_X_Y = Array(0, 0, 119, 166, 183, 147, 235, 133, 288, 118)
End Sub
Yikes!
I have about 20 arrays that contain hundreds of pre-calculated numeric values. The example I posted was shortened just to conserve space but it actually contains 200 integer values.
How does one initialize these arrays with each value different than the others? Would it be like this
Would it be better to go ahead and use it as a Variant then in the code where it is called convert to integer like thisCode:Public images_X_Y() As Integer
:
:
Public Sub Init()
imgsXY = Array(0, 0, 119, 166, 183, 147, 235, 133, 288, 118)
For n = 0 To Ubound(imgsXY)
images_X_Y(n) = imgsXY(n)
Next n
End Sub
Code:Public images_X_Y As Variant
:
:
Public Sub Init()
images_X_Y = Array(0, 0, 119, 166, 183, 147, 235, 133, 288, 118)
End Sub
Private Function GetArrayValue() As Integer
GetArrayValue = CInt( images_X_Y(5))
End Function
"Better" is pretty subjective and situational.
If you have a ton of this stuff you could consider other ugly stuff, for example:
Create one program that sets up the values and writes them out as binary files. Then in your "real" program, compile these files in as custom resources, at runtime load them into Byte arrays and use CopyMemory to move them into the actual arrays (since LoadResData() calls return Byte arrays).
That would be faster but extra effort to maintain.
BTW: Those colons I used were just a sort of "vertical ellipsis" to show a gap that might have other statements in between, and they don't need to be there.
Byte Array? Many of the arrays have strings, and other values greater than a byte.
Bit of scope creep here .....?Quote:
Originally Posted by jmstrickland
If this is a one-off conversion then you could write some VB code to parse your Java source code to pull out the Array definitions and values and re-format them for VB. Write the re-formatted code to a file and copy and paste into VB. The Java statements specify the variable type so you can 'type' the VB Arrays as you go. A few InStr, InStrRev, Mid$ and Split statements should do it.
Don't understand your post, Doogle.
Anyway I think I will use the method suggested by dilettante in post #2
Code:Public images_X_Y As Variant
'
'
Public Sub Init()
images_X_Y = Array(0, 0, 119, 166, 183, 147, 235, 133, 288, 118)
'
' Other arrays here
'
End Sub
Public Function images_X_Y_GetValue(Index As Integer) As Integer
images_X_Y_GetValue = CInt( images_X_Y(Index))
End Function
'
'
JMS
I think Doogle (and Dilletante too) is suggesting you essentially create a sub
to read the Java code and create a text file containing the 20 arrays and their
200+ values. Thus, your text file would end up with 20 lines.
You could then, in your VB app, read said textfile (on demand) and populate
the array as needed. Thus, you would not be "hard-wiring" the 20 arrays.
It will "cost" some time to write the extraction app, but that seems to be less than
"hard-wiring" your actual app.
Spoo
I already have the hard coded values of the arrays. They are in the Java source code so it isn't like I have to re-write the values. All I have to do is to copy them into a sub. I don't see the advantage of reading the code and create an external file then later read in that file and populate the arrays other than it will save me from having to make the sub like dillettante suggested in post #2 but that is just as easy as making a sub to read the Java code, etc.
Java Source Code (Example of one array)
So all I do is copy the values and put them in a sub like thisCode:'
'
public int imagesXY[] =
{
0, 0, 119, 166, 183, 147, 235, 133, 288, 118,
98, 132, 155, 119, 206, 105, 255, 93, 80, 105,
132, 92, 180, 81, 226, 71, 64, 81, 114, 71,
159, 61, 203, 53, 119, 152, 183, 135, 236, 121,
288, 106, 98, 120, 155, 106, 206, 93, 255, 82,
80, 93, 132, 82, 181, 71, 226, 61, 64, 71,
114, 61, 159, 51, 203, 44, 119, 139, 182, 123,
'
'
'
'
236, 109, 288, 94, 99, 107, 155, 95, 206, 82,
255, 71, 80, 81, 132, 72, 181, 60, 227, 51,
64, 61, 114, 51, 159, 42, 203, 34, 119, 124,
183, 110, 236, 95, 289, 81, 98, 93, 155, 83,
206, 70, 255, 59, 79, 69, 132, 60, 180, 50,
227, 40, 64, 51, 114, 41, 159, 32, 204, 24,
119, 152, 183, 135, 236, 121, 288, 106, 98, 120,
155, 106, 206, 93, 255, 82, 80, 93, 132, 82,
181, 71, 226, 61, 64, 71, 114, 61, 159, 51
};
'
'
'
VB code
Now, I have a Sub for each array so I can get the valuesCode:Private imagesXY() As Variant
'
'
'
'
'
' I call the Sub from Form_Load
'
Public Sub InitArrays()
imagesXY = Array(0, 0, 119, ...............51)
bigNumbersScore = Array(66, 67, 0, 0, 67, 67, ............ 68)
'
' the rest of the arrays
'
End Sub
'
'
Then in the code where ever it called for one of the arrays I just call the function insteadCode:'
'
Public Function imagesXY_GetValue(Index As Integer) As Integer
imagesXY_GetValue = CInt(imagesXY(Index))
End Function
Public Function bigNumbersScore_GetValue(Index As Integer) As Integer
bigNumbersScore_GetValue = CInt(bigNumbersScore(Index))
End Function
'
' Functions for the rest of the arrays
'
Code:'
'
'
Public Sub drawBallsOnBoard(g1 As Graphics, i As Integer, j As Integer)
'public void drawBallsOnBoard(Graphics g1, int i, int j){
Dim k As Integer
k = 1 ' int k = 1;
Do ' do
If ballsOnPoles_GetValue(k) <> 0 Then ' if(ballsOnPoles[k] != 0)
g1.drawImage images(imagesXY_GetValu(k * 2)) ' g1.drawImage(imagesXY[k * 2]);
End If
k = k + 1
While k < 65 ' while(++k < 65);
End Sub '}
'
'
I see that I do not need to helper functions as I can reference the array directory so all I have to do is to initialize the arrays using this code
Then in the code just reference the array instead of calling the GetValue SubsCode:Public Sub InitArrays()
imagesXY = Array(0, 0, 119, ...............51)
bigNumbersScore = Array(66, 67, 0, 0, 67, 67, ............ 68)
'
' the rest of the arrays
'
End Sub
So, why is creating an external file then loading that file back into the program and initializing the arrays any better than what I'm doing?
Yes.. AT first you indicated that you were dealing with numeric values. The implication was that you were only dealing with numeric values and not dealing with strings. Then when someone gave some advise you say you have strings in some of them which of course changes things.
Ideally if you were dealing with strings and numeric values then you should have said so in the first post rather than implying only numeric values.
Have you read your sig?
Here's why and I know others do not understand.
It's simple: On a need to know basis
In my original post I simply asked if I can take an array like it is declared in Java and do the same in VB and I gave a simple example. Why would I think that telling I had arrays of strings and values larger than a byte have anything to do with my first question. It simply didn't make any difference what was in the arrays.
When I open a thread I use the least amount of info I feel is needed so that I do not over state or add a lot of stuff that doesn't apply at the time I ask my question because I see a lot of posts where others comment that it is too long or too much stuff posted which tends to complicate the issue. So, I keep mine at a minimum and only add to it if it becomes necessary. If I get an answer that satisfies my request then good, everything is kosher, I closes the thread and I am on my way. However, when certain unforeseen things pop up I simply handle them as they occur like when dillettante suggested the byte array (post 2) which never occurred to me since I was satisfied with his first reply so I didn't say I had arrays that contained strings and values greater than a byte until he brought it up. To my way of thinking it simply didn't apply prior to his post #2.
Oh, that's message is for you:)
In this case it did. Dilettante's suggestion in Post #4 would have been ideal if the data was all numeric (i.e. you know the length of each variable). You'd just use CopyMemory to copy the correct number of bytes from the byte array into a suitably 'typed' array.
e.g.
Dim lngArray(3) As Long
CopyMemory lngArray(0), bytArray(0), 16
would result in elements 0 to 3 of lngArray being populated with values from bytArray.
With strings involved, you don't necessarily know the length of each string, so CopyMemory would not be suitable, implying that Dilettante wasted some time in replying.
BTW My earlier comment regarding scope creep was an (obviously failed) attempt at light scarcasm - should have inserted a smilie - DM's interpretation of what I was getting at was exactly correct.
I almost understand your 'need to know' strategy by not 'littering' a question with what might be unrelated information, but the problem with that is, if you don't understand something how do you know what's 'litter' and what's important to include?
All to often we see threads here with 20+ responses and then OP comes up with "Oh, by the way ........" which changes the complete context of the original problem. I personally prefer too much, rather than not enough information - it's then up to me to filter it and see if I can come up with a solution.
The thing is I wasn't interested in Dillettante's post #4. His post #2 was ideal in my opinion and it is the one that I am using. It's the easiest approach and it's straightforward so why do anything else. I can not think that any other approach would be any better and I do not agree with you about it did make a difference as to what is in the arrays as my original question had nothing to do with contents but rather can it be done in VB as it was done in Java was my only concern when I started this thread so why say more than is necessary when what I posted as an example was sufficient enough for the question I asked at that time. It must have been as dillettant answered it in post #2. Dillettante had no idea exactly what kind of data was in the array other than what I posted and yet he had a good answer without any other information. That there alone tells me I gave enough information. I think the mistake was when I made post #3 which prompted dillettane to respond with post #4. If I had given more thought to his first post I would not have had to ask in post 3. So, in view of this the information I gave was enough to warrant his 1st and best answer. Like I stated when the occasion calls for it I will add further comments to my request but not if it goes no further whether it's me or someone else who carries it on further.
It's going to happen more often than we would like when someone starts a thread that the OP cannot think of everything and for sure you are going to get that "Oh, by the way ......." bit. That's just human nature. I don't always think of everything that I should include and I will probably wind up saying "Oh, by the way...." or "I forgot to mention.....". And yet life goes on anyway.......