Welcome to Dream.In.Code
Become a VB Expert!

Join 150,155 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,367 people online right now. Registration is fast and FREE... Join Now!




Casting-functions to variables.

2 Pages V  1 2 >  
Reply to this topicStart new topic

Casting-functions to variables., How can i write the code for security-checking the casting to variable

susanne
11 Jul, 2008 - 08:35 AM
Post #1

New D.I.C Head
*

Joined: 11 Jul, 2008
Posts: 7


My Contributions
Hello! I'm new here, this is my first "shot" topic. smile.gif
Im study'ng programming in vb, html & asp. But the VB-part is wery hard, maybe i can get some good advise from the people here:-)
Im Norwegian, and my english is not the best, but i'll try my best, and i'll hope you understand me.

I need a little help to understand and to code Global constants & Global variables. Altso casting.

To make to global constants i have this code. The constants name shoud be BR & CLR like you se below. The datatype is String & the value is after the =(equal-sign)
Is it right to put the declaration to Global constants on the top of the code?
When is Public, is it then Global?
CODE

Public Class frmMain

      Public BR As String = vbCrLf
      Public CLR As String = ""


To make the global variables i have this code:
Is that right, and what is the difference between variables & constants? In the code i cant see any difference. Both is Public ... As String/Integer.
Is Integer variable & String constant?
CODE

        Public Min As Integer = 0
        Public Max As Integer = 100


In the load-event of my code im trying to make the tbx(textbox).Text & lbl.Text to the same values that i declared in the first of the code, the constatants BR & CLR.
So, mye code is like this:

CODE

        tbxMin.Text = Min
        tbxMax.Text = Max


And...I have to use a casting-function to be sure that the variables treats like a string-types. How do i do that? I realy need some advise here...
User is offlineProfile CardPM
+Quote Post

jacobjordan
RE: Casting-functions To Variables.
11 Jul, 2008 - 09:46 AM
Post #2

class Me : Perfection
Group Icon

Joined: 11 Jun, 2008
Posts: 1,262



Thanked: 39 times
Dream Kudos: 1675
My Contributions
Public means the variable can be read by anyone using this code. A normal Dim means it is only visible in that class or module (Dim is the same a Private). Lastly, Friend means it is only visible to code files in the same assembly.

A normal variable can be changed anywhere. Whereas, a constant, must have an initial value and cannot be changed. An example would be
vb

'This variable is a constant, meaning it is Read Only
Const Pi As Double = 3.14159265358979

If you want to cast one data type to another, there is a variety of functions that will do that:
vb

CStr(Object) 'Converts to a String
CInt(Object) 'Converts to an Integer
CDbl(Object) 'Converts to a Double
CSng(Object) 'Converts to a Single
CBool(Object) 'Converts to a Boolean

There is also a function that will try to convert one object to any other data type
vb

CType(Object, Type)

Hope that helps.

This post has been edited by jacobjordan: 11 Jul, 2008 - 09:49 AM
User is offlineProfile CardPM
+Quote Post

susanne
RE: Casting-functions To Variables.
11 Jul, 2008 - 10:08 AM
Post #3

New D.I.C Head
*

Joined: 11 Jul, 2008
Posts: 7


My Contributions
[quote name='jacobjordan' date='11 Jul, 2008 - 10:46 AM' post='382617']
Public means the variable can be read by anyone using this code. A normal Dim means it is only visible in that class or module (Dim is the same a Private). Lastly, Friend means it is only visible to code files in the same assembly.

A normal variable can be changed anywhere. Whereas, a constant, must have an initial value and cannot be changed. An example would be
vb

'This variable is a constant, meaning it is Read Only
Const Pi As Double = 3.14159265358979

If you want to cast one data type to another, there is a variety of functions that will do that:
vb

CStr(Object) 'Converts to a String
CInt(Object) 'Converts to an Integer
CDbl(Object) 'Converts to a Double
CSng(Object) 'Converts to a Single
CBool(Object) 'Converts to a Boolean

There is also a function that will try to convert one object to any other data type
vb

CType(Object, Type)

Hope that helps.
[/quote]


[/quote]
Thanks for your answear.
So, i was thinking about something. When im writing the code for a global constant. Is it possible to write this code instead of the one i wrote in the last post?
CODE

Const BR As String =vbCrLf

Whats the difference between the code above and this code?
CODE

Public BR As String = vbCrLf


Is public the same as Global?
User is offlineProfile CardPM
+Quote Post

jacobjordan
RE: Casting-functions To Variables.
11 Jul, 2008 - 10:18 AM
Post #4

class Me : Perfection
Group Icon

Joined: 11 Jun, 2008
Posts: 1,262



Thanked: 39 times
Dream Kudos: 1675
My Contributions
Ok, Public is a visibility level identifier, like Private and Friend. If a variable is declared Public, anyone who is accessing your code from another program can also see that variable. I don't exactly know what your talking about when you say "Global". The only way i have heard Global used is when you are referring to a variable that can be seen from any method in a class or module.

The difference between a Public and Const identifier is that the Public is a normal variable, and Const is a read only variable. If you want a Public Constant, declare a variable like this
vb

Public Const BR As String =vbCrLf


This post has been edited by jacobjordan: 11 Jul, 2008 - 10:21 AM
User is offlineProfile CardPM
+Quote Post

Zhalix
RE: Casting-functions To Variables.
11 Jul, 2008 - 10:47 AM
Post #5

D.I.C Head
**

Joined: 7 May, 2008
Posts: 218



Thanked: 9 times
My Contributions
QUOTE(jacobjordan @ 11 Jul, 2008 - 12:18 PM) *
If a variable is declared Public, anyone who is accessing your code from another program can also see that variable.

Um, I don't think so.

I'm pretty sure that Global and Public do the same thing. Global can only be used in a module, however.

They allow the variable to be accessed from all procedures throughout the scope of the project.

There are multiple levels that you can declare a variable to. I don't know if I know them all but I figure the main ones are:

Procedure Level (can only be accessed from that procedure)

Form Level (Declared at the top of all the code in a form. Can be accessed from all procedures in the form)

Project Level (Declared using the Public keyword, either at the top of a form or in a module. Can be used throughout the project)

This post has been edited by Zhalix: 11 Jul, 2008 - 10:56 AM
User is offlineProfile CardPM
+Quote Post

jacobjordan
RE: Casting-functions To Variables.
11 Jul, 2008 - 11:26 AM
Post #6

class Me : Perfection
Group Icon

Joined: 11 Jun, 2008
Posts: 1,262



Thanked: 39 times
Dream Kudos: 1675
My Contributions
QUOTE(Zhalix @ 11 Jul, 2008 - 01:47 PM) *

QUOTE(jacobjordan @ 11 Jul, 2008 - 12:18 PM) *
If a variable is declared Public, anyone who is accessing your code from another program can also see that variable.

Um, I don't think so.

Your right. Sorry, i was getting my VB.NET and VB stuff mixed up.
User is offlineProfile CardPM
+Quote Post

susanne
RE: Casting-functions To Variables.
11 Jul, 2008 - 11:42 AM
Post #7

New D.I.C Head
*

Joined: 11 Jul, 2008
Posts: 7


My Contributions
QUOTE

Thanks for good information, it's helps a lot to get advice and info about how things works. It's a hard to learn programming biggrin.gif
I try to use the code you wrote

User is offlineProfile CardPM
+Quote Post

susanne
RE: Casting-functions To Variables.
11 Jul, 2008 - 12:55 PM
Post #8

New D.I.C Head
*

Joined: 11 Jul, 2008
Posts: 7


My Contributions
[quote]

How can i write the code to be sure that the varibles get treated like there stringtypes? Like a casting-function on the variables?

On the code below i want to write the casting-function on the variables.
How can i do this?
The code's like this:
CODE

    'Casting on the variables to be sure they get treated as right stringtypes
            tbxMin.Text = Min
            tbxMax.Text = Max


I was thinking about...

[code]
tbxMin.Text = Min.CInt ...But...Hmmm...i'm realy comfused here, can somebody please give me a tip.

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Casting-functions To Variables.
11 Jul, 2008 - 12:58 PM
Post #9

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
You have this question asked in a thread already, please don't start duplicate threads asking the same question as it's not going to get you an answer any faster. You're just going to need some patience, sometimes it can take a while for a response as we all have lives as well as trying to help here smile.gif Topics merged
User is offlineProfile CardPM
+Quote Post

susanne
RE: Casting-functions To Variables.
13 Jul, 2008 - 02:47 AM
Post #10

New D.I.C Head
*

Joined: 11 Jul, 2008
Posts: 7


My Contributions
Sorry, i'll will wait for answears!
Im stuck in my programming...
User is offlineProfile CardPM
+Quote Post

Zhalix
RE: Casting-functions To Variables.
13 Jul, 2008 - 03:46 AM
Post #11

D.I.C Head
**

Joined: 7 May, 2008
Posts: 218



Thanked: 9 times
My Contributions
1) Are you using VB 6.0 (or earlier), or VB.NET?

2) What exactly do you need help with?
User is offlineProfile CardPM
+Quote Post

susanne
RE: Casting-functions To Variables.
13 Jul, 2008 - 07:30 AM
Post #12

New D.I.C Head
*

Joined: 11 Jul, 2008
Posts: 7


My Contributions
[quote][quote name='Zhalix' date='13 Jul, 2008 - 04:46 AM' post='383420']
1) Are you using VB 6.0 (or earlier), or VB.NET?

2) What exactly do you need help with?
[/quote]


[quote]
1. Im using VB 2005 and i write the code in Visual studio.

2. I need help with how to code the casting-function on the variables and constants.
How to be sure that the varibles & constants get treated like there stringtypes? Like a casting-function on the variables?
I got all the casting-function from a person here in this forum, but i dont now how to use them.
[/quote]
[quote]
The code is like this:
[/quote]
CODE

tbxMax.Text = Max

[quote]
The datatype is integer, and then its CInt?If i got it right?
I'll tryed everything, and the last try was the code below. Is that working?
[/quote]
CODE

tbxMax.Text = CInt(Max)

[quote]
3. Another question is how can i check if the number is a "wholenumber" , i dont now the english word. I hope you understand what i mean.
Is like a number without decimal i think. How can i check that with using casting?
I have to be sure that the tbx.Text is a "wholenumber"...Hmm?
The code is like this:
[/quote]
CODE

    Dim num As Integer = tbx.Text


Maybe is stupid question but i learn a lot from this site!

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:37AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month