how to declare a variable

Tom3885

Member
Joined
Sep 23, 2006
Messages
5
Programming Experience
Beginner
Hi again,

Thanks to all who helped last time!

Now, I have been in my class on VB.net for about 4 weeks now and I'm really pulling my hair out because I just don't get the declaring variable thing at all. Everyweek that goes by I'm losing 10% on my grade and I'm down to 65% now. So I'm looking for an easy explanation of how to declare a variable using a Dim statement and relating it to whatever the event is supposed to do.

This week we're doing a salary calculator for a period of a lifetime but I haven't been able to get the simple task of declaring a variable correctly so I've really fallen fast in this class.I've been busting my ass to understand this but the only thing I keep getting from my classmates or instructor is read the book (which doesn't explain it clearlyl) or look to the web. I'm really past the point where I can do this alone because I'm so frustrated and confused on where to start. I haven't gotten the help from the start so I'm really confused about how to do the basic of the basic.
***********************************************
This is where I want to start. * option strict is set to on as a requirement.*

I need to start step by step, like this: *First I do know how to do the design part* (that's the easy part)

If I had a simple text box (which would be TextBox1) and wanted someone to only enter a name or show it will show a message on the background of the program area if they enter a number.

I was told to declare the variable as:*at the top of the form*
Dim NameBox As Char

So next I put:
If NameBox <> Char then MessageBox.Show "Please enter a name"
*I put this in the sub for the textbox1.

I get all kinds of error messages at the bottom of the screen. This is how basic I need to start. I also have no idea what to do if someone enters a number.

What am I doing wrong????

thanks
Tom
 
take a look at the attachment for this post, it's got two powerpoint presentations from when i took the vb.net intro course at my local college, they explain how to declare variables and do calculations. i think they'll be of use for ya
 
I know this is not an answer to your question, but if you can get a different teacher - get one. If all it can tell you is look in a book or the web it has no business teaching! That is the worst thing a teacher can say!! I got off a course because of the same thing and have learned more from here.
 
hey bro, I know exactly how you feel, I have been privelaged enough i guess that i was able to understand by seeing other examples and what have you. But here is my suggestion...

you have a textbox with a name (such as textbox, or whatever the default is) as well as having a button.

when you are in your form mode double click on the button, this is where ALL of your code will go.

you declare a variable by doing something like
dim namebox as string (i chose string because you are getting a name not a single character)

then you say something like
namebox = textbox.text
messagebox.show(namebox)

what textbox.text does is pull the information from your textbox that the user puts in and then we are setting it to namebox that we had declared ealier

so our logic says

what ever is in textbox.text, put that information in namebox then give us a messagebox that shows us whatever is in namebox. Please let me know if this helps you at all...if it does not i will write you the code so that you can see it.

Lead by example...thats how i learn things best so let me know.
 
How to declare a variable.

Hello Tom.
Do not worry about declaring a variable. When you type in the word ‘Dim’ you are telling the computer that the Dimension of the ‘thing’ (variable) you name has to be as you tell it. Thus the computer reserves an area of memory for keeping that info. The area of the memory takes up space that is called, ‘bytes’. Some variables need loads of space, some just a little - so you tell the computer in advance with the Dim statement. Let’s say you are going to have a routine for the distance between one town and another but you are not bothered about too much accuracy so a whole number would do. Therefore you would declare as this:
Dim distance As Integer
The computer reserves 4 bytes for that variable called ‘distance’
If you wanted much more accuracy as having the distance with several places of decimal you may declare with this statement:
Dim distance As Long
The computer reserves 8 bytes of memory space for this.
Look in that link given by JuggaloBrotha and you will see all the different types and their values. That info is very good.
Don’t let this declaring variables get you lost, because you are looking deep into it? All you have to be concerned about is that after declaring a variable all use of it has to be in the guidelines you put. The computer tells you in error messages when you have not done so. Just remember that you are the master - not the computer!
I cannot go through it all but let me just tell you a little about Strings.
These are declared as follows:
Dim myName As String
There is no ‘set’ amount of space reserved, as I think of, for a string? But these are different variables as they are put in quotes.
See the following code: if ‘fred’ didn’t have “” “” around it, the computer will tell you!
VB.NET:
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim name AsString
name = "fred"
MsgBox("Name is " & name)
EndSub
Try This : -
Put a Button and TextBox on form; double-click button; put this code in; run by pressing F5; put something in textbox and press button.
VB.NET:
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim name AsString
name = TextBox1.Text
MsgBox("Name = " & name)
EndSub
Good Luck with VB.
 
I pasted that - got lost in middle? What I meant is that I think you are delving deeper than what you should do at this stage. That is a very good aptitude to have.
Don't forget! If in doubt - shout out!
Ask more if you want.
 
TwoWing.. a tip for posting code.. Switch the text editor into Basic mode by using the
switchmode.gif
button before you paste code..

The advanced editor makes a proper mess of code when you paste it in, running words together as can be seen in the code you posted (PublicSub).. Basic Edotor can cope with this better
 
I was told to declare the variable as:*at the top of the form*
Dim NameBox As Char

Well, at the top of the form i.e. at the top of the Code View, underneath the line that reads Public Class Form1

I tend to use Public, Private or protected instead of Dim in this section.. Dim can be used however, but ts more typical to assign access permissions in this section.

However, we shall persist with Dim


Dim NAME as TYPE

name and type can be varied by you. Name is what you will call your variable. Endeavour to avoid repeating names - the rules for which one is used if two variables share a name can get tricky.
Type must be one of the existing types or a type written by you. Anything you see when you press F2 can be used as a type, but not all of them can be turned into objects in memory. This may become important late rin your course, for now jsut bear it in mind

Picture an apple, a peice of rope and a paper name tag that is blank. Write tomsApple on the tag and attach it to the rope. Attach the rope to the apple. You now have what we get in computer memory when we have a named object. You can give this entire thing to your friend and he will know it's tomsApple, and he might also be able to see the colour, size etc

Now lets turn the image into computing:

Dim tomsApple as Apple
This means "prepare a named tag in memory, make the tag capable of being tied to an Apple type of object, and write the name tomsApple on the tag"

No apple exists at the end of the rope yet, but we have told VB tha tthe only thing we can tied to it, is an apple. Attempting to tie an orange to it will fail.

Now:
Dim tomsApple as New Apple
Same as before except notice the NEW keyword. This causes a new apple to come into existence, and be tied to the named tag

Dim tomsApple as Apple = New Apple
You might see this way written - it's shorthand for the above


It is important to note in Object Orientation, that a Class is not an instance of an object. It is a blueprint for creating objects.

Dim redOne as New Apple("red")
Dim greenOne as New Apple("green")
redOne.Equals(greenOne)
--> false

Here we have two objects, both of type apple but they have different properties. One is red, one is green. They are both instances of an apple, but they are found at different locations in memory becuase they are separate entities

That's pretty much it for variables - you make them, they have a type (that the tag and rope can be attached to) and thats it! We'll talk about scope later

If NameBox <> Char then MessageBox.Show "Please enter a name"
*I put this in the sub for the textbox1.
I'm not sure what is going on here, but youre comparing namebox, which is a variable of type Char, to the class Char, - the type of this variable

in real world terms its like saying:
If doubleChocolateChunk <> Cookie then

or
If myBrother <> Person

can you see the difference between a variable pointing to an object that exists and the type of the variable?

I get all kinds of error messages at the bottom of the screen. This is how basic I need to start. I also have no idea what to do if someone enters a number.

What am I doing wrong????
Not taking enough time/having access to enough resources to make the course wasy.. Can you talk to the course hosts?
 
thanks Cjard!

I really appreciate the effort you put forth to help. Yes I got it now. It's funny because when I took my class the teacher seems to go along like everyone just knows how to program. I am just a beginner, that's why i'm taking "Introduction to programming."

I like the , tag, rope apple analogy. It was as basic as I needed it.

Here's my email , I may need your help in the future!

distortedvoices@mail.com

thanks again
Tom :)
 
Back
Top