5 Replies - 13721 Views - Last Post: 19 September 2007 - 03:57 AM Rate Topic: -----

#1 pandahuge  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-September 07

How do I check if a variable is empty

Posted 19 September 2007 - 01:17 AM

hi i'm steve, new member guys/gurls. i really like vb.net thats why i decided to familiar it with it. i know this is very simple for you guys. i've been thinking and trying what to do how to check a variable if it has value. i would like to ask you guys how do u check a variable if there is a value in it? using if else in vb.net 2003..thnz guys/gals in advance.


here's what im trying to enchance

Dim flag As Integer
	Dim dr As DataRow
	Dim dt As DataTable
	Dim custid As String
	Dim bm As BindingManagerBase

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		OleDbDACust.Fill(DsCust)
		bm = Me.BindingContext(DsCust, "CustomerTracking")
		bm.Position = 0

	End Sub

	Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
		txtCustID.Text = ""
		txtFName.Text = ""
		txtLName.Text = ""
		txtAddress.Text = ""
		txtPhone.Text = ""
		txtEmail.Text = ""
		flag = 1

		Dim ctr As Integer
		Dim len As Integer
		Dim custid As String
		Dim custidval As String


		dt = DsCust.Tables("CustomerTracking")
		len = (dt.Rows.Count - 1)
		dr = dt.Rows(len)
		custid = dr("custID")
		custidval = Mid(custid, 2, 3)
		ctr = CInt(custidval)
		If ctr >= 1 And ctr < 9 Then
			ctr = ctr + 1
			txtCustID.Text = "C00" & ctr
		ElseIf ctr >= 9 And ctr <= 99 Then
			ctr = ctr + 1
			txtCustID.Text = "C0" & ctr
		Else
			ctr = ctr + 1
			txtCustID.Text = "C" & ctr
		End If
		txtCustID.Enabled = False
		  


	End Sub


* PsychoCoder added code blocks

This post has been edited by PsychoCoder: 19 September 2007 - 01:36 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How do I check if a variable is empty

#2 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: How do I check if a variable is empty

Posted 19 September 2007 - 01:26 AM

"how to check a variable if it has value", depends what you mean?
*numveric value
if val(myvariable)<> 0 then msgbox("it has numeric value")
*string value
if myvariable.lenght > 0 then msgbox("it's not empty")
*value at all
if myvariable is not nothing then msgbox("value exists")

Might be more, but i hope you got my point :D
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How do I check if a variable is empty

Posted 19 September 2007 - 01:35 AM

Below are the changes I would make in this code, that being the code in cmdAdd_Click. You are using VB6 Methods in .Net, though they are still available, its more efficient to use the VB.Net methods as opposed to the old VB6 ones. The changes I made are commented

Dim flag As Integer
	Dim dr As DataRow
	Dim dt As DataTable
	Dim custid As String
	Dim bm As BindingManagerBase

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		OleDbDACust.Fill(DsCust)
		bm = Me.BindingContext(DsCust, "CustomerTracking")
		bm.Position = 0

	End Sub

	Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
		txtCustID.Text = ""
		txtFName.Text = ""
		txtLName.Text = ""
		txtAddress.Text = ""
		txtPhone.Text = ""
		txtEmail.Text = ""
		flag = 1

		Dim ctr As Integer
		Dim len As Integer
		Dim custid As String
		Dim custidval As String


		dt = DsCust.Tables("CustomerTracking")
		len = (dt.Rows.Count - 1)
		dr = dt.Rows(len)
		custid = dr("custID")

	   '********************************************
		'Dont use Mid(), thats a VB6 expression
		'The VB.Net replacement is SubString(start,length)
		 custidval = custid.SubString(2,3)
		'********************************************

		'********************************************
		 'Dont use Cint, thats VB6, .Net's version is
		 'CType(variable,DataType)
		 ctr = Ctype(custidval,Integer)
		 '********************************************

		 '********************************************
		  'I would use a Select Case as opposed to 
		  'those IF...ELSEIF....ELSE statements
		  '********************************************		   
		   Select Case ctr
			 Case 1 To 9
					  ctr += 1
					  txtCustID.Text = "C00" & ctr
			  Case 10 To 99
					  ctr += 1
					  txtCustID.Text = "C0" & ctr
			  Case Is >= 100
					  ctr += 1
					  txtCustID.Text = "C" & ctr
		   End Select
		   txtCustID.Enabled = False
		   '********************************************
	  End Sub 



Here is a list of items from VB6 and their replacements in VB.Net.

Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#4 pandahuge  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-September 07

Re: How do I check if a variable is empty

Posted 19 September 2007 - 01:42 AM

thanks again PsychoCode
panda

thanks m2s87
panda
Was This Post Helpful? 0
  • +
  • -

#5 pandahuge  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-September 07

Re: How do I check if a variable is empty

Posted 19 September 2007 - 01:57 AM

PsychoCoder
how would i check and then add for the case statement intead of using if then else? here's i i'm having a hard time thinking of it. i'm planning to do is example there are 10 inputed custID then if i delete the custID "C008" and when im going to add a new one it should go back to "C008" and then save. i've tried using the looping and the if statement can't catch it. what do u think i should do?
steve
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How do I check if a variable is empty

Posted 19 September 2007 - 03:57 AM

Well in my opinion I wouldn't reassign a customer's ID to a new customer. What if that customer wants to do business with you again, they call up and give their old Customer ID, but now someone already has it.

Instead of doing that you should give an ID thats the next available number, lets say you have up C099, dont look for a number missing in that range, just assign the next new customer C100, unless you're limited to a 4 digit ID (C999), then thats a bad business setup if you ask me.

Can I ask what is your reasoning for wanting to do it this way?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1