Programming in .Net

Status
Not open for further replies.
Justin, you need to do it like this:

Code:
Public AnyArray(99) As CustomClassName
And when you need to use an element you need to declare it first:
Code:
AnyArray(1) = new CustomClassName()

Declare the array as a whole...... then declare each element as you need it.
 
Awesome I'll give that a try. They sure don't make finding stuff like this easy so that is why I asked. What about creating a button that can lanuch a hyperlink or another file though? Like say I want to include a link to a website or an e-mail link for contact, etc.

Addendum:

Never mind... I guess the Shell() function is used to do it and IE and the hyperlink can be used and in asp.net pages a linkbutton could be used for hyperlinks.
 
Last edited:
The method you gave me worked! Just put it to practice and it works great. Half way done to actually finishing a useful application thanks to you :smile:

I have a question about generating an integer randomally within a range. I've tried searching for the simplest method possible to achieve this task. Say for a range between 1 and 6 this is what I found in .net help on msdn:

Code:
[COLOR=green]' Initialize the random-number generator.[/COLOR]
[COLOR=blue]Randomize[/COLOR]()
[COLOR=green]' Generate random value between 1 and 6.[/COLOR]
[COLOR=blue]Dim[/COLOR] value [COLOR=blue]As[/COLOR] [COLOR=blue]Integer[/COLOR] = CInt(Int((6 * Rnd()) + 1))

Is there a simplier way to do this? Its only 2 lines but I still don't understand it all. Like for example why CInt() has to be used unless the generated number is considered an object rather than a value or what Int() is for. I get the rnd() part but I still don't see how the mathematical function works to produce a number within the range. In other programs I've been able to grab random numbers in a range easily by specifying the lower and upper bounds along with the call to the random function. I'm suprised that there doesn't appear to be a similar simple way to do this in VB.

I could easily copy/paste like any code kidde out there, but I want to actually understand what it is doing.
 
I don't know the specific language, but the logic is the same as in Basic, or in a spreadsheet function.
The random number generating function gives an output in the range 0 - 1. e.g. .276491996430275
If you want a random integer between 1 and 150, you have to multiply the random function by the maximum value in your range. This will give you a value 41.4737994 etc, which you must round to the nearest integer value (41) with the CInt function (or macro) (or whatever they call it in that language.
Presumably Cint is a truncate function (round down), so the plus 1 will prevent the range being 0-149 instead of 1 -150
 
Last edited:
Thanks for your help. It kind of explains things better than what I was getting from the documentation. I am somewhat moderate with functions like these in spreadsheets, but either cannot or do not know how to use the VB equalvilents yet.

CInt is for converting to the integer data type. My guess for its purpose here is what you just confirmed... rounding up the rough decimal to make a whole number. I roughthly at high level view understand what the code is doing. As much as I don't like copy/paste I've already started to implement it into the program. Just hope it works. This is a simple piece to the kind of complex routine I need to write to get this application finished.

Addendum:

Maybe it would be better if I explain what I want to do with random numbered arrays...

I want to have two arrays. One to get random numbers and sort it and another to hold information the application will use. Now what I need is to:

a) Create and initailize the first array with random numbers within a given range
b) Sort the first array, and have the contents of the second (parallel) array follow its index. Like for eample in a spreadsheet where when you sort, the fields next to the random field follow wherever thier numbers go, so that all of the fields comprising a record stay together.

One way I thought of accomplishing this task was using a two dimensional array instead, storing the random numbers in the first column and the accompanying data in the second, thinking I could use the Array.Sort function to pick the dimension I want sorted. This stuffs still kinda new to me so I don't know how to procceed (barely started looking into the art of randomness in vb today).
 
Last edited:
Are you writing a game ?
The only reason I can guess to sort a list into a random order would be shuffling a pack of cards.
If the absolute value of the random number is unimportant, i.e. you just want to use the randomness to arbitrarily sort another list into a random sequence, then you don't need to do the multiplication, rounding down, or adding one.
The code will be a lot more efficient if you just use the default value of rnd() in the random column and sort by that alone.
The maths processor will just be filling a string of bits (probably 64, but I've no real idea of the architecture - the more, the randomer) with 1 or 0, based on some other, hopefully unpredicable variable.
In the very early days of DOS 1.0 (yes I really was around then), the Basic compiler was included and the random number function was anything but.
If you booted the machine, and ran a program that printed a random number, it would always be the same one.
They generated "random" numbers in a way that had the appearance of randomness, but always started by using some fixed point in the architecture. This made it extremely shaky if the function was being relied on for encryption, a bit like the sloppy operators who allowed us to break German Enigma codes in WW2, by always starting their messages with the same "Hello Otto, it's me again"

For the sake of example. If the register used to generate the random value were 8 digits, then the possible values would be the 256 numbers between
0000 0000 and 1111 1111
Having generated this string of bits, the value 1 0000 0000 would be assigned the arbitrary value of 1 (decimal) and the value would be presented to you as a decimal value between 0 and 255/256 (.996)
The larger the number of bits in the string, the finer the gradation of randomness, between 0 and .99999999999999999999999999999999999999999999999999999999 etc.

If you're just using the random number as a means of shuffling another list, there's no point in mutiplying rounding and adding.
If you're trying to randomly sort them into 6 groups, then you'll need to use the code as quoted.
 
The numbers generated are important that they stay within a paticular range. Back during the times I'm been training for the certs that I have I've come accross some very nicely polished study software but it contains "sets" of tests you buy related to the certs you want to study for. What I'm doing is writing my very own, with a full editor where new tests can be created. I got the editor part all done. I am just working on the actual test part of it, where questions and the answers should be randomally sorted.

I think I'll take the two-demensional approach if I can't find anything from the link CG provided. The problem though is the data the application will be using will be of string data type, so I'm going to haft to convert back and forth just to make this work. Can Array.Sort be used to sort a specifc deminson in a mutli-dimensioned array?
 
What do you want to sort in that array? More importantly, why do you have a two-dimensional array?
 
What will be sorted is the random numbers that are generated so that the content is randomnized. The reason for a two dimension array is hopefully so the content that actually will be in use by the application will follow the order of however it is sorted with the random numbers. I'm about out of any other ideas to achieve this...
 
I don't understand what you're saying.

I'm totally lost here, but if I had to make a testing program, I'd do something like this:

class: Question
Members: string question, string answer

Array of questions.

Pick a random number from 1 - # of questions. Pick that no. from the array and show it.
 
You know, that may work, but what about the answers? I would need to display all of the answers for the question and I would need them in random order...

I've easily achieved this in a template I put together for excel in the past for this purpose. All I had to do was have two columns. The first column looked up the answers for the current question via vlookup and displayed them. The actual test sheet had hard-coded paths to these cells (ie answer a always referenced the same cell.) Now the second column would constantly generate random numbers. When I needed to the answers randomnized all I had to do was use the macro that was "generating" a new test to sort the random column. All the other fields followed so that each answer would stay together with its number on sort.

Addendum:

I will be back a little later with the template if I can find it/screenshots of how it works. Its only for office 07 though due to the lack of some sorting features previous versions didn't have. Its pretty nice, but I'd much rather take the knowledge I have now and turn it into a program of its own...
 
Last edited:
you could have an array of possible answers in the question class, and an integer indicating which is the right one.

the same method you use to pick questions in random order can be used to show the answers in random order, too.
 
Well here's the template I was talking about... I will take to account everything here and see if I can make this work.

Anyone that wants to try the template should remove its .xls extension and give it a .xltm one. If would have been this way already but it wouldn't allow me to upload .xltm files.

Basically it grabs 20 questions at random and tests you on them. The ammount of questions that can be added is basically unlimited with the limitation of the ammount of rows excel can handle within a single spreadsheet.

MultipleChoiceTestTemplate-Howitwor.jpg
 

Attachments

  • Multiple Choice Test Template.xls
    666.7 KB · Views: 0
Last edited:
Well now I'm kinda upset... finished the code and everything to get it the way I wanted. No errors at design time and no exceptions thrown at runtime. Would have waited but I didn't want any errors/crashes outside the program to occur. I click the button in the app to run the code to do all the randomnizing for the test and it disappears with nothing displayed on the screen... doesn't appear to be doing anything.

So how I have it setup in case anyone can think of a better way...

- I have a for loop to go through each question
- I have 7 do until loops inside.
-- The first loop inside keeps going until it finally selects a question that hasn't been previously selected.
-- The second and the rest of the loops inside get an answer for the question at random and keeps going until it gets an answer that hasn't been grabbed yet
- At the end of the outside loop I do validation to determine which answer as presented to the user is the correct response for later use.

I redim preserve the array inside the loops until it has reached the max number of questions which is what I think is causing this. Seems as if vb chokes on repetitive loops unless you give it a definite range right away.

Your method might work gold CG but this application is to be able to time the user on a test when they are not using "study mode" so I need it to pull each question and thier answers but in random order.

Addendum:

Ok. On the 7th inside loop it keeps returning 0 everytime though I use Randomnize() each time before Rnd() is ever called to assign a value. The code is the same for all the loops and the others are working when I go line by line, so I think some validation for the rest of the loops it tends to hang on will probably be needed...
 
Last edited:
Alright. I just thought got an idea for the problems I'm having. My application allows the user to put in 6 answers for a question, but only requires 2. I think the loop keeps going cause a question in the sample test I've created has less than that and since the first 5 loops grab all the possible answers for a 5 answer question, the last guy is left with nothing...

Thanks me for solving the problem :smile: :smile: :smile:
 
This just keeps getting more frustrating by the moment. I went ahead and did a test project to test whether the code above actually generated random numbers within the range and it did. I totally rewrote the code so it would follow however many answers a question had, but its still getting stuck on a loop where it keeps getting the same thing no matter what... seems to happen near the last loop for answers d-f where there are a good 5 or 6 conditions it must meet before discontinuing looping to ensure that the outcome isnt equal to any answer before it.

I'm going to work on getting questions first before I get the answers. One step at a time is how I need to approach this. This is the second re-write to the whole project, after the first one grew too overwhelming to maintain/troubleshoot (had a boatload of additional features I had wanted, but I want to get this working).

This isn't a cry for help at the moment or anything like that. Just thought since you've been so great about helping me I would keep everyone updated on where the project is going. Can you recommend a good programming forum where someone there might be able to help me?


Addendum:


you could have an array of possible answers in the question class, and an integer indicating which is the right one.

the same method you use to pick questions in random order can be used to show the answers in random order, too.

Man I feel like an idiot. I read this post earlier for sure, but now I get what you're saying and went ahead and tried out that method. Still haven't got all the code written yet, but it doesn't constantly repeat anymore... a good sign for sure :smile:

Of course, though I think I'm starting to get somwhere any other good links/forums anyone can link to programming for me to do additional research wouldn't be a bad idea :smile:
 
Last edited:
The ability to write simple and reusable code that doesn't repeat itself comes from practice. I remember the first code I wrote - and shudder to think of coding like that these days!

With practice comes perfect :smile:
 
Status
Not open for further replies.
Back
Top