Solved

Dim and set variable on same line

Posted on 2007-04-02
9
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 143

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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
 

Author Comment

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

Error = Compile Error - Invalid outside procedure

???
0
 
LVL 143

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

729 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