Property. What am I doing wrong? Going crazy ...

S

shapper

Hello,

I have a custom control under namespace MyNameSpace.WebControls with a
property of type validation:

' Validation
Private _Validation As Validation
< _
Bindable(True), _
Category("Behavior"), _
DefaultValue(""), _
Localizable(True) _
Public Property Validation() As Validation
Get
Return _Validation
End Get
Set(ByVal value As Validation)
_Validation = value
End Set
End Property ' Validation

Validation is class with various properties under the namespace
MyNameSpace:

' Validation
Public Class Validation

' IsRequired
Private _IsRequired As Boolean
Public Property IsRequired() As Boolean
Get
If _IsRequired = Nothing Then
_IsRequired = False
End If
Return _IsRequired
End Get
Set(ByVal value As Boolean)
_IsRequired = value
End Set
End Property ' IsRequired

..............

End Class

I am using my control in a page as follows:

Dim a As New MyControl

MyControl.Property1 = "Something"

MyControl.Property2 = True

It works!

When I add the following property:

MyControl.Validation.IsRequired = False

I get the following error:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

On code line:
MyControl.Validation.IsRequired = False

What am I doing wrong?

Thanks,

Miguel
 
I

IfThenElse

Validation Class instantiated how?


shapper said:
Hello,

I have a custom control under namespace MyNameSpace.WebControls with a
property of type validation:

' Validation
Private _Validation As Validation
< _
Bindable(True), _
Category("Behavior"), _
DefaultValue(""), _
Localizable(True) _
Public Property Validation() As Validation
Get
Return _Validation
End Get
Set(ByVal value As Validation)
_Validation = value
End Set
End Property ' Validation

Validation is class with various properties under the namespace
MyNameSpace:

' Validation
Public Class Validation

' IsRequired
Private _IsRequired As Boolean
Public Property IsRequired() As Boolean
Get
If _IsRequired = Nothing Then
_IsRequired = False
End If
Return _IsRequired
End Get
Set(ByVal value As Boolean)
_IsRequired = value
End Set
End Property ' IsRequired

..............

End Class

I am using my control in a page as follows:

Dim a As New MyControl

MyControl.Property1 = "Something"

MyControl.Property2 = True

It works!

When I add the following property:

MyControl.Validation.IsRequired = False

I get the following error:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

On code line:
MyControl.Validation.IsRequired = False

What am I doing wrong?

Thanks,

Miguel
 
S

shapper

Validation Class instantiated how?

Well, I am not using something in my code as follows:
Dim val As New Validation
val.IsRequired = True
val. ...

MyControl.Validation = val

Is this what you mean?

I am not doing this.

Do I need to do this?

But can't I declare a property of type class and then define it?

Thanks,
Miguel
 
G

Guest

Well, I am not using something in my code as follows:
Dim val As New Validation
val.IsRequired = True
val. ...

MyControl.Validation = val

Is this what you mean?

I am not doing this.

Do I need to do this?

But can't I declare a property of type class and then define it?

Thanks,
Miguel- Hide quoted text -

- Show quoted text -

Miguel, he means that Validation Class is null and this is the reason
why you got "object reference not set to an instance of an object".
You need to have something like this

Private _Validation As Validation = New Validation
 
S

shapper

Miguel, he means that Validation Class is null and this is the reason
why you got "object reference not set to an instance of an object".
You need to have something like this

Private _Validation As Validation = New Validation

Bah, it is not the first time that this happens to my.
I copy paste a property from another another which is of type string
and I miss the New.

I wonder if Microsoft is going to implement the following features in
VB.NET as there is in C# in .NET Framework 3.5?
Does anyone knows?

Thanks,
Miguel
 
G

Guest

Bah, it is not the first time that this happens to my.
I copy paste a property from another another which is of type string
and I miss the New.

I wonder if Microsoft is going to implement the following features in
VB.NET as there is in C# in .NET Framework 3.5?
Does anyone knows?

Thanks,
Miguel- Hide quoted text -

- Show quoted text -

What features?

When you declared Shared class or method then you would not need to
create a New instance of a class/method. It's up to you and depends on
design
 
G

Guest


Not in the same way. In C# 3.5 you could use "unassigned" properties
like here

Person p = new Person();
p.FirstName = "Miguel";
Console.Write(p.FirstName); // "Miguel"

In VB.NET such construction would give you an empty string.

But unlike C# you can use "unassigned" GET properties in .NET 2, e.g.

Public Class Person

Public Property FirstName() As String
Get
End Get
Set(ByVal value As String)
End Set
End Property
....
End Class

would work without any problem. And in C# you cannot do it.

BTW, you can get Orcas Beta 2 and test all new things by yourself
http://msdn2.microsoft.com/vstudio/aa700831.aspx
 
S

shapper

Not in the same way. In C# 3.5 you could use "unassigned" properties
like here

Person p = new Person();
p.FirstName = "Miguel";
Console.Write(p.FirstName); // "Miguel"

In VB.NET such construction would give you an empty string.

But unlike C# you can use "unassigned" GET properties in .NET 2, e.g.

Public Class Person

Public Property FirstName() As String
Get
End Get
Set(ByVal value As String)
End Set
End Property
...
End Class

would work without any problem. And in C# you cannot do it.

BTW, you can get Orcas Beta 2 and test all new things by yourselfhttp://msdn2.microsoft.com/vstudio/aa700831.aspx

Thanks Alexey,
Miguel
 
J

John MJ Gorter

Why not instantiate the variabele with the definition, in c# i would
do something like:

private Validation _validation = new Validation();

It probably would be something like this in VB.Net:

Private _Validation as new Validation

That way you would always have a valid object instance....

Greets.
 
G

Guest

Why not instantiate the variabele with the definition, in c# i would
do something like:

private Validation _validation = new Validation();

It probably would be something like this in VB.Net:

Private _Validation as new Validation

That way you would always have a valid object instance....

Greets.












- Show quoted text -

we already found it some days ago
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top