VB.net CA1062 validate parameter - asp.net

Working on an old .net 3.5 vb web application
Getting over 1000 warnings on public property methods such as the following
CA1062 : Microsoft.Design : In externally visible method 'TheClass.TheMethod.Set(String)', validate parameter 'value' before using it.
Original:
Public Property DealerBMRName() As String
Get
Return hdBMRName.Value
End Get
Set(ByVal value As String)
hdBMRName.Value = value.Trim()
End Set
End Property
modified yet still throwing the error:
Public Property DealerBMRName() As String
Get
Return hdBMRName.Value
End Get
Set(ByVal value As String)
If value Is Nothing Then
hdBMRName.Value = ""
Else
hdBMRName.Value = CStr(value)
End If
End Set
End Property
Pretty sure I am following MSDN suggested workaround:
http://msdn.microsoft.com/en-us/library/ms182182(v=vs.100).aspx
Any ideas what I might be missing apart from the fact that the code itself is ugly?
I can't remove the error even with something as basic as:
Set(ByVal value As String)
hdBMRName.Value = "SomeValue"
End Set
Using VS2010 and resharper.

The issue resolved itself when I fixed a bad line entry in my web.config. It had nothing to do with the error but may have been throwing the issue.
PS: I needed to use .Value as the object is an asp:hiddenField.

Related

How to set visibility on public property in VB.NET

I am maintaining some old code, I am a c# developer.
We have a usercontrol with a tablecell that I want to hide with my code on my other aspx page. It is reference and all that good stuff.
But how do you write a property to set visibility in vb.net.
Something like this, but not working.
Public Property vis As TableCell
Get
LogoArea.Visible = False
End Get
Set(value As TableCell)
LogoArea.Visible = True
End Set
End Property
Use this instead:
Public Property LogoVisible As Boolean
Get
Return LogoArea.Visible
End Get
Set(value As Boolean)
LogoArea.Visible = value
End Set
End Property
This wraps around the Visible property of the LogoArea cell. Assuming it's a TableCell from your previous example, which is the web controls version, not the HtmlControls version.

Understanding how Public Properties are set in VB.NET

I am trying to understand how a value in a Public property is set. There does not seem to be anything passed or any kind of lookup.
Public Shared Property IsUser() As String
Get
If HttpContext.Current.Session("IsUser") IsNot Nothing Then
Return HttpContext.Current.Session("IsUser").ToString()
Else
Return Nothing
End If
End Get
Set(value As String)
HttpContext.Current.Session("IsUser") = value
End Set
End Property
I have tried searching for the ClassName i.e. UserSession.IsUser = and also the only place the session value is set is in the property. HttpContext.Current.Session("IsUser") = value
What do I need to understand better.. I mean at the end of the day the User has to be True or False so where is the Lookup or passed parameter?

ASP.net 2010 (VB) Object reference not set to an instance of an object

Morning All,
I am using VS2010 with VB and im trying to get a ping test working in my web application. In order to do this and test that it works i have simply created a button that when clicks should ping a specified IP address.
I believe that the code for the button should work fine. The only issue i have is the following error message on my web page...
System.NullReferenceException: Object reference not set to an instance of an object.
It bugs on the cole line...
Console.WriteLine("Address: {0}", vPingReply.Address)
I thought that this was due to 'Properties' needing to be set up for the .Address and .Status objects. Im not too sure if i have added these correctly as i have added some properties but i still have the same issue when i run the page?
Can someone please take a look and advise?
Here is my full code...
Imports Microsoft.VisualBasic
Imports System.Text
Imports System.Net.NetworkInformation
Imports System.Net.NetworkInformation.PingReply
Partial Class Ping
Inherits System.Web.UI.Page
Private mSend As PingReply
Private Property Send(p1 As String) As PingReply
Get
Return mSend
End Get
Set(value As PingReply)
mSend = value
End Set
End Property
Private mAddress As PingReply
Private Property Address(p2 As String) As PingReply
Get
Return mAddress
End Get
Set(value As PingReply)
mAddress = value
End Set
End Property
Private mStatus As PingReply
Private Property Status(p3 As String) As PingReply
Get
Return mStatus
End Get
Set(value As PingReply)
mStatus = value
End Set
End Property
Protected Sub btnPing_Click(sender As Object, e As System.EventArgs) Handles btnPing.Click
Dim vPing As New Ping
Dim vPingReply As PingReply = vPing.Send("xxx.xx.xxx.xx")
Console.WriteLine("Address: {0}", vPingReply.Address)
Console.WriteLine("Status: {0}", vPingReply.Status)
End Sub
End Class
Any help is much appriechiated.
Betty.
You cannot access the content of Address property if the Status is not Success
Dim vPing As New Ping
Dim vPingReply As PingReply = vPing.Send("xxx.xx.xxx.xx")
if vPingReply.Status = IPStatus.Success Then
Console.WriteLine("Address: {0}", vPingReply.Address)
End If
Console.WriteLine("Status: {0}", vPingReply.Status)
The docs says
If the value of Status is not Success, you should not use the values
returned by the RoundtripTime, Options or Buffer properties. The
RoundtripTime property will return zero, the Buffer property will
return an empty array, and the Options property will return null.
but I found that the Address property is null (Nothing in VB) also if the Send is for a non-existant ip address or DNS name
However, looking better at your code, it is clear that all the calls made inside the btnPing_Click method are handled by your class Ping not to the framework class Ping. And your class uses variables not correctly initialized. I suggest to remove those methods from your class or just rename the class with something different.
Another option (not recommended) is this
Private Property Send(p1 As String) As PingReply
Get
' Specify the full namespace to remove ambiguity between this class and framework class
Dim p = new System.Net.NetworkInformation.Ping()
mSend = p.Send(p1)
Return mSend
End Get
Set(value As PingReply)
mSend = value
End Set
End Property
Private Property Address() As String
Get
if mSend IsNot Nothing Then
Return mSend.Address
else
Return string.Empty
End If
End Get
' Meaningless ???
'Set(value As PingReply)
' mAddress = value
'End Set
End Property
Private mStatus As PingReply
Private Property Status() As String
Get
if mSend IsNot Nothing Then
Return mSend.Status.ToString()
else
Return string.Empty
End If
End Get
' Meaningless ???
'Set(value As PingReply)
' mStatus = value
'End Set
End Property

updating properties of a class with dynamic references

Sorry for what is probably a very basic question. I have a vb.net class with properties defined as follows:
Private m_Property1 As String
Public Property Property1() As String
Get
Return m_Property1
End Get
Set(ByVal value As String)
If IsNothing(value) Then
m_Property1 = String.Empty
Else
m_Property1 = value
End If
End Set
End Property
I can then set the values as follows:
classname.Property1 = "myvalue"
How do I set the value of a property that is defined dynmically eg
Dim strPropertyName As String = "Property1"
Hope that makes sense.
Thanks,
Josh
You would use reflection
Dim strPropertyName as string = "Property1"
Dim pi As PropertyInfo = myClass.GetType().GetProperty(strPropertyName)
pi.SetValue(myClass.GetType(), "some string", Nothing)
You want to use Reflection in order to do this. VB.NET provides a way to do this if you know the value at compile-time, but for run-time operations, you need to use the GetType keyword in order to get the type of your class (or, use the GetType method on an instance of it if you don't know it).
Then, with that Type instance, you would call GetProperty, passing the string with the name of the property. It will return an PropertyInfo instance which you then call GetValue on, passing the instance of the object in, which will return an Object which you have to cast back to a type you wish to use (if you are).
VB.NET makes a lot of this easier with the CallByName function.
Also, if you know at compile-time what the name of the property is, you can always cast to object and use VB.NET's inherent late binding:
Dim o As Object = <your object>
o.Property1 = ...
VB.NET will perform the late-binding for you.

Expression Expected

Im getting a build error with the following code...
Private Property GridViewSortDirection() As String
Get
Return If(TryCast(ViewState("SortDirection"), String), "ASC")
End Get
Set(ByVal value As String)
ViewState("SortDirection") = value
End Set
End Property
It is happening on the following line...
Return If(TryCast(ViewState("SortDirection"), String), "ASC")
Error returns...
Error 11 C:\inetpub\wwwroot\TPSupport\main\UserControls\grid.ascx.vb(192): error BC30201: Expression expected.
Any Idea's
Thanks
Its a convert from c#
private string GridViewSortDirection
{
get { return ViewState["SortDirection"] as string ?? "ASC"; }
set { ViewState["SortDirection"] = value; }
}
I am not familiar with VB but use of If keyword shows that you are doing comparison between result of TryCast and "ASC",
Try this way, by using appropriate comparison operator like
Return If(TryCast(ViewState("SortDirection"), String) <> "ASC")
What compiler version are you using? I suspect that you inadvertently switched to a version of VB (< 9.0) that doesn’t yet support the conditional operator.

Resources