Visual Basic
|
|
 |

|
Use your debugger to locate the exact point at which the error occurs and then show just that portion of code (within <pre> tags), and the content of all the local variables. Although, the debugger will clearly show you which object reference has not been initialised.
Use the best guess
|
|
|
|

|
Evord wrote: Hi everybody, am going to show you a sample program We have samples on datagridprinters in the Article-section. This isn't a sample, it's a code-dump with a question. Let's focus on the question and solving it, shall we?
Instead of showing "exc.Message", try "exc.ToString()"; that way the messagebox will show on which line the error occured. That'd be easier in tracing the exception than reading the entire code and checking each line.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|

|
My class is now over. I had an issue with the the assignment. I couldn't get the program to find and display the duplicate random numbers. I've looked for almost two weeks for an answer and I'm hoping that someone here can help me.
This was the assignment:
Present the following report – (you will modify this based on user input and the actual rolls.
Number of dice:2
Number of sides:6
Number of rolls:2
Dice 1 Roll 1 = 5
Dice 2 Roll 1 = 6
Dice 1 Roll 2 = 1
Dice 2 Roll 2 = 1
Number of rolls where all dice were the same: 1
I got everything but the number of rolls where all the dice were the same. Can someone please advise me as to how this should have been done?
Here is a snippet of what I did:
Dim RandomClass As New Random()
Dim vRoll() As Integer ReDim vRoll(nDice)
Dim cntDice As Integer = 0
Dim myArray As New List(Of Integer)
Dim temp As Integer
myArray.Clear()
For i As Integer = 1 To rRoll
For j As Integer = 1 To nDice
temp = RandomClass.Next(1, dSize + 1)
LST_Items.Items.Add("Dice " & j & " Roll " & i & " = " & temp.ToString)
If Not myArray.Contains(temp) Then
cntDice += 1
LBL_Same.Text = cntDice
Else
LBL_Same.Text = "0"
End If
Next
Next
So if anyone is willing to give me advice I would greatly appreciate it.
Thanks!
Troy E Bouchard
|
|
|
|

|
First, you don't need that List. You're not trying to remember every single die roll.
Second, you never compare the roll of the first die to the roll of the second. Once you do that, you simply increment a counter that tracks how many times both dice are equal.
Third, you don't need two for loops, just one.
|
|
|
|

|
You could also sort the list first, then duplicates would show up next to each other.
|
|
|
|

|
Don't need the list at all. Gen the first number on the roll and remember it. For each subsequent die, check it's value against the first die. Finding a non-dupliate is very easy. If you get through the loop withouting finding a non-duplicate, well, you know.
|
|
|
|

|
Hi Guys,
Is it possible to have a list/collection of objects that inherit from a single base object without losing Intelli-sense?
Here is an example of what I mean...
Public Class baseblob
Private _position As PointF
Property position
Get
Return _position
End Get
Set(value)
_position = value
End Set
End Property
Sub New()
position = PointF.Empty
End Sub
End Class
Public Class blob
Inherits baseblob
Sub moveRight(ByVal Distance As Single)
position = New PointF(position.X + Distance, position.y)
End Sub
End Class
Public Class Form1
Dim blob As New blob
Dim blobs As New List(Of baseblob)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
blob.moveRight(10)
MsgBox(blob.position.x)
blobs.Add(blob)
MsgBox(blobs(0).position.x)
blobs(0).moveright(10)
MsgBox(blobs(0).position.x)
End Sub
End Class
... but of course in the above example i get 'moveright' is not a member of 'oopTest.baseblob'
So it wants me to use
Dim blobs As New List(Of Object)
So it will compile and work as intended...
I just wanted to know is there something I'm missing here? Why can't I just let it know that it's a base object and to list all the possible methods/properties etc of the base class and also any objects that inherit the base class? Give me the lot!
To me it doesn't sound like such a hard thing to ask of the compiler/dev environment... why cant it do it ?
:(
modified 14 May '13 - 0:36.
|
|
|
|

|
The method shows up.
In VB.NET, the default access modifier for a class method is Public. In C# it's private.
You'd be better served by not depending on the defaults but explicitly declaring your access levels. Also, your naming convention isn't exactly clean and understandable either. In your example, how do to tell the difference between a variable called blob and your class called blob?? It should look more like:
Public Class BaseBlob
Private _position As PointF
Public Property Position
Get
Return _position
End Get
Set(value)
_position = value
End Set
End Property
Sub New()
Position = PointF.Empty
End Sub
End Class
Public Class Blob
Inherits BaseBlob
Public Sub MoveRight(ByVal distance As Single)
Position = New PointF(Position.X + distance, Position.y)
End Sub
End Class
Public Class Form1
Private blob As New Blob
Private blobs As New List(Of BaseBlob)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
blob.MoveRight(10)
MsgBox(blob.Position.x)
blobs.Add(blob)
MsgBox(blobs(0).Position.x)
blobs(0).MoveRight(10)
MsgBox(blobs(0).Position.x)
End Sub
End Class
You can't tell Intellisense to show all members from every class inheriting from a base class. It's not showing you the members of Blob because you told it the collection was a collection of BaseBlob, not Blob. Do you see a member on BaseBlob called MoveRight?? Nope. So it's not going to show any members that inherit from classes that inherit from BaseBlob because none of them are valid! MoveRight is not applicable to a collection member of blobs because you're are treating the objects in the collection as if they were BaseBlob object. It's telling you that if you try to use one of those members, the compile will fail!
|
|
|
|

|
Thanks for the naming scheme advice. What I use for my project is a lot easier. It was just a quick example. I should have made it easier to read, sorry.
My primary issue is that I need to have multiple class objects that behave differently (playership, enemy drone, missile etc) held in a single container so that my main loop can enumerate through them all.
For example.
For i as integer = 0 to blobs.count - 1
blobs(i).AiTick
blobs(i).Move
blobs(i).Draw
Next i
The only way I could find to do this was to create a base class with the ai/move/draw methods in it and have different classes inherit from the base class and over-ride the base class's methods.
The main issue i have is that these classes that inherit from the base class occasionally have extra methods. I would like to see those when i'm coding things like blobs(0).ExtraMethod like i can for the base class.
I guess what i'm asking isn't possible.
If ClassA inherits BaseClass and ClassB inherits BaseClass but ClassA also has an extra method called "ExtraMethod" then I already run the risk of accidently executing ExtraMethod on the item in the collection that is ClassB and causing an exception. I deal with that issue already in my code, so i don't understand why the dev environment is perfectly fine with me making a list(of Objects) and running List(0).ExtraMethod ... however when I ask it to have a list of BaseClass it doesnt goto the effort of listing what's availiable in any classes that inherit from BaseClass.
I guess I will just continue to have a list(of Object) instead of a list(of BaseClass) and continue running extra methods from classes that inherit from BaseClass as I currently do by first checking the object type.
|
|
|
|

|
Purge1t wrote: I guess what i'm asking isn't possible.
It's not possible because you're trying to violate Object Oriented Programming principles.
You're putting the Move method in one object, when it actually applies to all of the objects. You have to Move the Player, Move the Missle, Move the EnemyDrone, ... Don't use a rather specific MoveRight method. It should be Move and take some kind of value(s) to tell it where to or how far to move. That should go in the Base object that these other objects inherit from.
The same is true for the Draw method. Every object should draw itself, so you pass in a Graphics object that it uses to draw itself on. This is common for every object you mentioned, so it should go in the Base object they are inheriting from, not the specific objects.
By the way, in your case, if you try to execute a method on an object that doesn't implement it, you don't get an exception. You get a compile-time error as the code will not compile.
|
|
|
|
 |
|
|
General
News
Suggestion
Question
Bug
Answer
Joke
Rant
Admin