This might be a basic question, but I would like to know best practice.
I have a public property which takes in a value as an Integer. If that
value by accident would be a String, could I in my property do validation
and convertion on the fly, so the output becomes an Integer, without
my script failing? Or is it best to make shure to operate with the
right datatype before passing it in the property?
This is my property:
Public Property Quantity() As Integer
Get
Return m_Quantity
End Get
Set(value As Integer)
m_Quantity = value
End Set
End Property
Best regards!
If that value by accident would be a String,
Such accident cannot happen in a strongly typed language because the compiler will tell you that you cannot assign a string value to an integer property. Actually you could shorten your code a little by using an Auto-Implemented Property:
Property Quantity As Integer
The property cannot be a string. Either the code will not compile, or if you don't have Option Strict/Infer on (and you really should!) the runtime conversion to Integer will fail, causing an exception.
Related
I usually make user controls containing forms for adding and editing data for a particular table in my database. I then show or hide these controls as the user clicks "edit" buttons, etc. It's common practice (for me) to put properties in the code-behind, that are used for setting the ID of the item being edited, into a hidden label on the page, and of course leaving it blank for new items being inserted. I usually only use C#, however, this time around I have to use VB.NET.
So in C# I would do the following:
public static int EditID
{
get
{
return Convert.ToInt32(lblEditID.Text);
}
set
{
lblEditID.Text = value;
}
}
..and then when the user, say, clicks an "edit" link from a gridview, I would
//set the ID of the corresponding record, something like this:
MyUserControl.EditID = MyGridView.SelectedDataKey[0];
Cool. So now I need to do this in VB.NET, and here's my code:
Public Shared Property EditID As Integer
Get
Return Convert.ToInt32(lblEditID.Text)
End Get
Set(value As Integer)
lblEditID.Text = value
End Set
End Property
but I get a syntax error that says: "Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.", highlighting the lblEditID for both the getter and setter.
I can't find any other SO questions about this, and I have Google'd just about every permutation of keywords I can think of, so this must be something really stupid.
What am I doing wrong here?
EDIT: Yes I realize I could just use a Session variable instead of the label, but I would still like to know why this doesn't work and how I could make it work with a label.
You don't want a Shared property for this. lblEditID is a label that exists in an instance of a WebForm class:- it can't exist until an instance of this class has been created, hence the error.
I don't really understand how the C# worked as this should be the same but I'm not a C# expert.
If you remove the Shared keyword it will work as you want I believe
So I'm messing around a bit with Caliburn.Micro, and suddenly I notice something interesting.
I have a ViewModel property called Maximum of type int, auto bound with CM via the naming convention, to a TextBox.
When I enter something that is not and integer, i.e. a character, the textbox' border turns red, and the setter of the property is not called.
Is this an auto-feature of CM?
No, this is the behaviour of WPF. One option is to bind to a string property on your view model, and then perform the validation within the view model (i.e. parse to an int, and provide a default value if the parse fails).
I have some web pages that include other pages, and I need to check if a variable (a string) has been declared in the page or not.
I was exploring try catch and finally, but im always getting a compiler error saying the variable doesnt exits.
syntax in my head is:
if variable(exists) then
do something
else
do nothing
end if
From what im finding is this wont even compile if the variable wasnt defined anywhere. I kinda knew that, I was just hoping to find some kind of work around. :/
The only way I know of is to use reflection...
This will not work for variables defined within subs/functions...
Friend Function VariableExists(ByVal variableName As String) As Boolean
For Each tField As FieldInfo In Me.GetType.GetFields
If tField.Name.ToLower() = variableName.ToLower() Then
Return True
End If
Next
Return False
End Function
Cose here is untested and may contain minor errors. Think of it more like pseudocode.
Instead of declaring a variable, how about adding a simple Interface that you can use in the pages that need to be processed a specific way, then you can test whether or not the page implements the Interface?
For example:
Public Interface IMySpecialInterface
End Interface
In the pages that you want special behavior for:
Public Page MySpecialPage
Implements IMySpecialInterface
End Page
In the code that processes the pages:
If TypeOf Me.Page Is IMySpecialInterface Then
What about using an interface:
Public Interface ISpecialProp
Property SpecialProp() As String
End Interface
Then you can test, if a class implements the interface or not using this code:
Dim spec = TryCast(obj, ISpecialProp)
If spec IsNot Nothing Then
Console.WriteLine(spec.SpecialProp)
End If
Quick question which I fear has a short and disappointing answer but alas I shall ask anyway..
In the C++ Dictionary method TryGetValue() is there any way to change the default value that will be returned for an integer (to -1 instead for example) when the key is not present? The problem is that 0 is the default and this is not suitable because a value of 0 would make sense in the context of my program.
If not, is the ContainsKey() method that much slower? Or is it splitting hairs and nothing to worry about seeing as in all likelihood I have no choice..
Many thanks
PS I don't need to perform any hashing function (though this might be in the implementation for Dictionary anyway!), nor have any particular ordering to my collection, I just want lookup and adding to be as fast as possible. Is Dictionary a sound choice?
Why don't you create a class that inherits from Dictionary(Of TKey, TValue) and override the TryGetValue function. You could then use your own class and it would behave just as you would want it to...
I'm using a compiled .dll provided by someone else -- I know little about it, other than it has a session variable that I must access in a way that is strange to me. Not sure what to call it -- have googled for words that I thought might be right, but so far no success. Here is what it looks like:
Session("receipt").username
It's the .username part that I don't understand. What is it? How is it created?
Thanks for any help.
Session is probably a global object which has a default property which returns a SessionItem object. The SessionItem object is loaded from the browser-session (probably) by the Session object. The SessionItem object has a property username, which is a value stored somewhere in the browser-session.
Some code to clear things up:
Public Class Session
Private Items As SessionItemCollection
Default Public ReadOnly Property SessionItem(ByVal id As String) As Object
Get
Return Me.Items.Find(id)
End Get
End Property
End Class
And you calling the code (Which searches for the "receipt" item in the SessionItemCollection Items from Session):
Session("receipt")
My first guess (since there isn't much other code to go off of) is that the object being stored in the session variable and accessed via Session("receipt") is of a class that contains a property or member called username which you are accessing in that fashion.
The basic idea is that Session("receipt") will pull back whatever this object is (for the sake of instruction we will say it is a receipt object) and the .username is referencing the username property of that receipt object.