5 Replies - 6705 Views - Last Post: 12 August 2011 - 12:02 PM Rate Topic: -----

#1 jasser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-July 09

created object can't be seen outside the if statement !

Posted 12 August 2011 - 01:05 AM

the problem is as following .
i need to call this function saveBase from more than one place
so i decided to send a number in variable x to know of what type to create the object named Model.
however when it's within the if statemet i keep getting the error that model is not declared !!!!!
how can i build the object i want of type depending on the incoming variable x ?????

thx
 Public Function SaveBase(ByVal x As Integer) As BaseHumanModel
        If x = 99 Then
            Dim Model As New StudentModel
        End If
        If x = 99 Then
            Dim Model As New UserModel
        End If
        Model.Code = TxtCode.Text
        Model.Name = TxtName.Text
        Model.Name2 = TxtName2.Text
        Model.IDNumber = TxtID.Text
        Model.Tel = TxtTel.Text
        Model.Mobile = TxtMobile.Text
        Model.BirthDate = DTPBdate.Text
        'Model.BirthLocation.Code = CmbBaseHuman.SelectedValue
        Model.Address = TxtAddress.Text
        Model.Username = TxtUserName.Text
        Model.Password = TxtPassword.Text

        Return Model
    End Function


Is This A Good Question/Topic? 0
  • +

Replies To: created object can't be seen outside the if statement !

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: created object can't be seen outside the if statement !

Posted 12 August 2011 - 01:31 AM

You will need something like:
         Dim Model
        If x = 99 Then
            Model = New StudentModel
        End If
        If x = 99 Then
            Model = New UserModel
        End If 

Was This Post Helpful? 0
  • +
  • -

#3 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 977
  • Joined: 09-May 09

Re: created object can't be seen outside the if statement !

Posted 12 August 2011 - 04:11 AM

Hey mate

ive spotted a simple error.

Q. if x = 99 then which class should the object model be from?


If x = 99 Then

           Dim Model As New StudentModel

       End If


       If x = 99 Then

           Dim Model As New UserModel

       End If



When declaring variables inside an if statement they are local to that statement. Look at the code in previous post to correct error and revise the x=99. The object " model " be always be of the class UesrModel

This post has been edited by deery5000: 12 August 2011 - 04:18 AM

Was This Post Helpful? 0
  • +
  • -

#4 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,507
  • Joined: 16-March 11

Re: created object can't be seen outside the if statement !

Posted 12 August 2011 - 09:07 AM

You could always call a public method if the conditions are true within your If statement. :) So then you'd be declaring your objects publicly. Anyone correct me if I'm wrong.

This post has been edited by trevster344: 12 August 2011 - 09:08 AM

Was This Post Helpful? 0
  • +
  • -

#5 jasser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-July 09

Re: created object can't be seen outside the if statement !

Posted 12 August 2011 - 11:49 AM

thx everyone :)

View Postsmohd, on 12 August 2011 - 02:31 AM, said:

You will need something like:
         Dim Model
        If x = 99 Then
            Model = New StudentModel
        End If
        If x = 99 Then
            Model = New UserModel
        End If 


this method seems to work
i have some further bugs i'm working on :)
thanks
Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: created object can't be seen outside the if statement !

Posted 12 August 2011 - 12:02 PM

We are glad to help with those also ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1