Solved

Dim and set variable on same line

Posted on 2007-04-02
9
311 Views
Last Modified: 2008-02-01
Is there a way to DIM a variable and set it on the same line?

For example:
Dim test as String = "Hello"

smithmrk
0
Comment
Question by:smithmrk
  • 4
  • 3
  • 2
9 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 18838811
Dim test: test="Hello"
0
 

Author Comment

by:smithmrk
ID: 18838842
OK, I guess my real problem is that I'm trying to do this under the Option Explicit so the entire Module can use the same variable and its value.

What I'm trying to do is the variable IPAddress and set it, so all my connections us the same variable instead of having to set it within each function.

Thanks,
smithmrk
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18838844
in vb6 or lower:
Dim test as string: test="Hello"

in vb.net:

Dim test as string = "Hello"
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18838854
Not sure I understand you there- why not make it a global variable?

[Public] Dim IPAddress As String
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:smithmrk
ID: 18838920
Public IPAddress As String: IPAddress = "10.31.30.26"

Error = Compile Error - Invalid outside procedure

???
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 18838968
you cannot initialize a variable outside a procedure.

what are you using the variable for?
possibly, you only want a constant?

Public Const IPAddress As String = "10.31.30.26"

If you want to be able to change it:


Private gIPAddress As String

Public property Get IPAddress as string
  If gIpAddress = empty
    gIpAddress = "10.31.30.26"
  end if
  IPAddress = gIpAddress
End property

public Property Let IPAddress ( nv as string )
  gIpAddress = nv
End Property
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18838995
vb6 or vb.net?
0
 

Author Comment

by:smithmrk
ID: 18838998
Public Const IPAddress As String = "10.31.30.26"

That was what I was looking for!!!!!!!!!!!!

Thanks so much!
smithmrk
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18839003
Ouch. : P
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now