Denaes Posted March 17, 2005 Posted March 17, 2005 I have a control I'll call GameBoard, which contains numerous 4-100 (right now) controls. This is set as a property in GameBoard and they're always amounts that are equally squared: 4, 9, 16 - which would translate into 2x2, 3x3, 4x4 grids when displayed. Obviously this lends incredibly easily to a 2d Array. So I tried this 2d Array for like 45 minutes and couldn't get it to work. I followed the MSDN and an example on this site I found searching and nothing. I tried to declare the array with my other private class level variables outside of a procedure: private arrGrid(,) as GridSquare Then: Sub New() ' TotalSqrt is the SquareRoot of the number of squares in total needed ' in the GameBoard. ' ie 16 squares would have a TotalSqrt = 4. arrGrid(TotalSqrt, TotalSqrt) = new GridSquare ' Or arrGrid = new GridSquare(TotalSqrt, TotalSqrt) End Sub I've tried this a few different ways, but this is the jist of it. Can anyone help me declare this properly? Quote
IngisKahn Posted March 17, 2005 Posted March 17, 2005 Your second example would work just fine in C#, but in the crazy world of VB you have to use ReDim arrGrid(TotalSqrt, TotalSqrt) BTW Using individual controls for the sqaures in your game is usually not a good idea. Controls are expensive to use and you'd be better served to do all the drawing on a single board control. Quote "Who is John Galt?"
Denaes Posted March 17, 2005 Author Posted March 17, 2005 Your second example would work just fine in C#' date=' but in the crazy world of VB you have to use ReDim arrGrid(TotalSqrt, TotalSqrt)[/quote'] You have to Redim your array to instantiate it and set it's size? Thats just silly... well, not intuitive. I thought Redim was for changing an existing array. Strangely enough I use arrays more in C# and collections more often in VB.Net. BTW Using individual controls for the sqaures in your game is usually not a good idea. Controls are expensive to use and you'd be better served to do all the drawing on a single board control. I'm going to need these "Squares" to display (and keep track of) 0-6 pieces of information, interact with the (up to) 4 squares touching them, allow interactivity with mouse (over and click) events and possibly display an image on each one as well. This is something I can create in a control in like 30 seconds, I can't even imagine the headache of keeping track of all this information in their own arrays and drawing and detecting where the mouse is moving/clicking... Besides, right now this is just somethings to get my application logic in the view and running. I can change this at a later point if it ever becomes a factor. Thank you for your help, I'll give it a try! Quote
Leaders snarfblam Posted March 17, 2005 Leaders Posted March 17, 2005 Nooooo! ReDim arrGrid(TotalSqrt, TotalSqrt) Nooooo! I use VB, and I hate and do not use ReDim. There is a better way! You do it just like c#. private arrGrid(,) as GridSquare Sub New() ' TotalSqrt is the SquareRoot of the number of squares in total needed ' in the GameBoard. ' ie 16 squares would have a TotalSqrt = 4. arrGrid = new GridSquare(TotalSqrt - 1, TotalSqrt - 1) {} '<--Note the curly braces 'ALSO NOTE that a new array(4, 4) will yeild an array with 25 elements 'The bounds will be (0-4, 0-4) instead of (0-3, 0-3) End Sub The difference is that in vb, since arrays use parentheses () instead of square brackets [] for the array indecies, you just need to throw on some empty curly braces {} to tell vb that you are creating an array and not calling a constructor. Well... you also specify the upper bound instead of the amount of elements in VB, as I noted in the comments in the code above. X = New GridSquare([i]parameters[/i]) 'Object creation syntax, calls constructor Y = New GridSquare([i]bounds[/i]) {} 'Array creation syntax, requires empty initializer to disambiguate Quote [sIGPIC]e[/sIGPIC]
Denaes Posted March 17, 2005 Author Posted March 17, 2005 That's fricking annoying :mad: We should just use brackets in VB! And I'm a VBer, not a C#er wanting to turn VB into C#. This is just silly. So I have to include the curley braces even though I'm not going to assign anything... That would explain the problem. But it allows me to do this MUCH easier! Quote
Leaders snarfblam Posted March 17, 2005 Leaders Posted March 17, 2005 That's fricking annoying Hey, chill out. Its two keystrokes and basic has been doing arrays with parentheses since the 1980s. Change it now and a lot of people will get mad. Quote [sIGPIC]e[/sIGPIC]
Denaes Posted March 17, 2005 Author Posted March 17, 2005 Hey' date=' chill out. Its two keystrokes and basic has been doing arrays with parentheses since the 1980s. Change it now and a lot of people will get mad.[/quote'] I'm annoyed, not "turning the table over angry" :p I used to be able to create arrays without "{}" in VB6. It just looks like VB.Net got caught by poor planning and rigged it to work. I'm sure I'll have forgotton it by tomorrow, I'm just annoyed that I spent over an hour trying to get this working thinking "well I wouldn't need braces there, I'm not assigning any values... why isn't this working?" Change it to [] and it's four less keystrokes and no confusion with the "()'s". I like VB.Net much better than C# and VB6, but I'm not going to blindly love and adore everything they choose to do. Some things will annoy me. :D Quote
Tygur Posted March 21, 2005 Posted March 21, 2005 I used to be able to create arrays without "{}" in VB6. It just looks like VB.Net got caught by poor planning and rigged it to work. ???? You still can. Just use ReDim. That's what VB6 used anyway. The empty braces were recommended by someone who dislikes ReDim. By the way, if you don't want ReDim, and you don't want empty braces, there is a third way: 'if you start with this: Dim A(,) As Object '..then doing this: A = Array.CreateInstance(GetType(Object), 2, 2) '..is the same as this: ReDim A(1, 1) By the way, brackets have already had a use in VB for quite some time. In VB.NET, they allow you to give variables any name you want: Dim [Dim] As Integer Quote
Leaders snarfblam Posted March 21, 2005 Leaders Posted March 21, 2005 By the way, brackets have already had a use in VB for quite some time. In VB.NET, they allow you to give variables any name you want: Dim [Dim] As Integer Brackets? We were just talking about curly braces. And admittedly, ReDim works just fine, but it is considered the old way of doing things, not the new .NET way. Seems like everyone wants to do things the .NET way. But I think ReDim is really just there for portability between vb6 and VB.NET. Quote [sIGPIC]e[/sIGPIC]
Tygur Posted March 21, 2005 Posted March 21, 2005 Brackets? We were just talking about curly braces. And admittedly' date=' ReDim works just fine, but it is considered the old way of doing things, not the new .NET way. Seems like everyone wants to do things the .NET way. But I think ReDim is really just there for portability between vb6 and VB.NET.[/quote'] The post I quoted from, which is actually the post right before mine, was also lamenting the use of ()'s and saying they should've started using []'s for arrays. The fact is that []'s already had a use in VB, even in VB6, though it wasn't very well known (and probably still isn't). ReDim is really the way of doing it, and it works. It's a part of the VB.NET language, and there's nothing wrong with using it. People seem to dislike anything that looks like it was in VB6, for some reason. I've even seen people say to stay from CInt, CBool, and the rest of those, and to use Convert.ToInteger, Convert.ToBoolean, and similar functions, just because the VB.NET type conversion functions look like VB6. That's the worst advice you can give, because CInt and the rest of them are actually compiled inline. The actual purpose of the curly braces in VB.NET is so you can initialize the array on the same line that you use to create it. You could never do that in VB6, so they added it to make VB.NET more powerful. However, changing/setting the size of an array was always possible in VB6, so they have you do that the same way as always. It is, after all, VB.NET. People seem to forget that. It's the next version of VB . ReDim's been in VB since well before .NET even became an idea in the back of someone's mind. Why take it out? Quote
Denaes Posted March 21, 2005 Author Posted March 21, 2005 Is Redim in the VB Compatability namespace? I've heard that namespace is only wrappers for .Net methods with more VB Friendly names/ways of doing things. I suppose you can't really say that there is a proper .Net way when C# does it a different way and I have no clue about J#. I forgot I was so annoyed about this until I just re-read it :D I told you that i was just venting because I spent like an hour working on this and it not working! Does C# also use {} to assign values? My knowledge of arrays in .Net isn't really up to snuff. I learned them in VB6 and then every time I try to do something in .Net with Arrays I end up getting stuck somewere and just use an ArrayList or Collection and it's much easier :D It's just that this is a grid so it lends itself much better to a double array rather than a collection. Thank you for your help, I really would like to see more on ReDim and how it compares to others in performance. As for converting I use either cType(,) or the actual class: Integer.ToString() Integer.Parse(), etc. I forgot that cint, cstr, etc were still here. You say they have better performance also? Quote
Tygur Posted March 21, 2005 Posted March 21, 2005 Is Redim in the VB Compatability namespace? I've heard that namespace is only wrappers for .Net methods with more VB Friendly names/ways of doing things. ReDim can't be in the VB Compatibility namespace because it's not even a function. It's a statement, just like Dim. Does C# also use {} to assign values? Yes, it does. But why do you ask? It's just that this is a grid so it lends itself much better to a double array rather than a collection. Thank you for your help' date=' I really would like to see more on ReDim and how it compares to others in performance.[/quote'] Actually, using ReDim and using {} are pretty much the same thing. The code might look different, but after it's compiled, it looks exactly the same. One is not faster or slower than the other. As for converting I use either cType(' date=') or the actual class: Integer.ToString() Integer.Parse(), etc. I forgot that cint, cstr, etc were still here. You say they have better performance also?[/quote'] Yeah, CInt and the rest of them are better. Quote
Leaders snarfblam Posted March 21, 2005 Leaders Posted March 21, 2005 The post I quoted from, which is actually the post right before mine, was also lamenting the use of ()'s and saying they should've started using []'s for arrays. Sorry. My bad. ReDim is really the way of doing it[/Quote]Depends on who you ask, really. Ultimately it is a matter of preference. And I don't think that everything that resembles VB6 is bad, I do prefer the New Type() {} syntax because it appeals more to my way of thinking. As far as CInt vs. Integer.Parse... I wouldn't be so certain that CInt is better than Integer.Parse. Integer.Parse is less ambiguous; you are parsing a string as an integer, not converting a long, double, etc. CInt works fine, however in the case of parsing a string, this is not inlined... they wouldn't inline an entire string parsing function everytime you want to get an int from a string (CInt calls the Microsoft.VisualBasic.CompilerServices.IntegerType.FromString() method) . My guess is that they would perform approximately equally, and ultimately, again it is a matter of preference. As far as converting from other numerical formats to an integer, yes CInt will inline, and I do use it in such cases. There are so many things in VB.Net that cause other .Net developers to moan and groan because of confusion as to which VB functions and Microsoft.VisualBasic members are there for that sake of compatibility and which are there because they are still part of the VB language. I personally recommend that one stay away from the Microsoft.VisualBasic namespace (I almost never use it and don't import the namespace into my projects) but only for the sake of portability and readability for C# developers. As long as you stay out of Microsoft.VisualBasic.Compatibility, it is up to the developer, and I suppose that it is bad advice to say either that the VB6 way of doing it is outright wrong or that the VB6 way is better. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.