VS 2005 Function Return Types And Error Conditions
Jun 8, 2012
Lets say I click a button on a form that calls a function in a data access class. This function inserts a record into a table in a database. The function returns an integer value for the record id of the newly inserted record. Now lets say the insert failed (syntax error, timeout, whatever). I can return a zero to the calling routine to indicate the failure, but not the error message. So in general, what's the most appropriate way to handle getting information back to a calling routine when the return parameter data type doesn't allow it?
So you know how PHP internal functions usually return a boolean FALSE when a function fails, or some other data type when the function succeeds? Is this possible in VB .Net?For instance, lets say this simple code here Public Function TrySomething(ByVal Param As String) \what would go here??
[Code]...
You see I want to return a BOOLEAN false when a function fails, and a string when the function works. I've looked everywhere, and when I search for "return multiple types" all i find is Tuple.
I want my function to return a value of a structure. The function is supposed to change the values and then return the answers in one structured value. Here is my code... as you can see it does not work... it needs a LOT of things. I placed question marks were i am really lost.
I have a function that returns a double. If in the function I decide the value is invalid, is there any way to return "error" instead of a double? Of course I could pass a boolean by reference and set it false and then check for that back in the calling function, but that seems awkward.
if i got a function with a try cath block inside and the function should return an integer.how would i do it that the function can return me a the error message?public function test() as Integer
I'm using VB6 and trying to get an instance of a Type Library object.After i retrieved the object and i'm trying to invoke a method. I'm getting this exception. However, i've checked with the parameters and its type. it is correct.I found something fishy, that method doesnt have return value. But it is throwing a compiler error whenever i'm trying to call the function and the compiler error went off when i get a return value from that method.I don't know,
Assuming Anonymous Types are tailored to be used with LINQ, I'd like to use them for simply and fast data aggregation without an explicit class definition, like in this snippet[code]....
im trying to get mac adddress from this code but i get eerror on END FUNCTION line it says this "function 'getRemoteMac' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used." what am i missing here?
I have two Parser classes that inherit from a base class, BaseParser. I want to use either class as a parameter in another Monitor class. The Parser classes, CS600 and TCH600, both have two properties, RawDataList and SummaryDataList. The CS600 class's RawDataList returns a List(of CS600Data); the TCH600 RawDataList returns a List(of TCH600Data). The SummaryDataList returns similar classes in each Parser class. CS600Data and TCH600Data derive from a base class, BaseData.BaseParser also has RawDataList (List(of BaseData)) and SummaryDataList (List(of BaseSummaryData))The Monitor class has a private field, _thisParser which can be either of the two concrete Parsers above. I want to be able to call and use RawDataList and SummaryDataList of _thisParser within Monitor class, but when I construct the concrete Parser classes, Visual Studio notes that the RawDataList property of CS600 cannot override the RawDataList property of the BaseParser because they differ in their return types.
I thought that since CS600Data derived from BaseData (but also adds some new properties of its own) that I could use CS600Data wherever I use BaseData. What am I misunderstanding? How can I correctly construct these classes?
Public MustInherit Class BaseParser Protected _rawDataList As List(Of RawGasData.BaseData) Public MustOverride ReadOnly Property RawDataList() As List(Of RawGasData.BaseData)
I have a very specific problem concerning Dns.GetHostEntry.A service uses Dns.GetHostEntry to retrieve all IP Adresses of a host, using the name of of the host. This has always worked fine. At a specific customer, Dns.GetHostEntry throws the "no such host is known" error when querying specific hosts. The problem only occurs when trying to resolve hosts that are on a different domain than the machine the service is installed on. The service has worked for quite some time, but recently is stopped working, throwing the "no such host is known" error (sadly, no stack trace is available). Nslookup works though, no problems there.The service in question is written in VB.NET, targetting the .NET Framwork 2.0.The comments in the MSDN entry for .NET 3.0 (http:[url]....aspx indictate that there may be a problem with the reverse DNS entries for the hosts, but I was not able to reproduce the problem on a test network, even with all reverse lookup zones deleted (there are more comments for other .NET Versions, all having similiar problems). [EDIT:] Even deliberatly addind a wrong PTR record does not make the problem occur on my test machine.[EDIT2:] The only thing that made the error come up was disconnecting the network adapter, and thereby making the DNS Server unavailable, even though the forward resolving still worked due to caching.So, my questions are: Under which conditions does GetHostEntry throw this specific error? Which resolution methods does it use? If I'm not mistaken it uses the unmanaged WinSock function getnameinfo (http:[url]....: "Name resolution can be by the Domain Name System (DNS), a local hosts file, or by other naming mechanisms".Any ideas why this suddenly fails for machines on the other domain (but not for machines on the same domain)?
I've just been burned again by the fact that there is no compiler warning when you fail to return a result in a Function that returns a Value type. I wrote the function:
Public Function CompareTo(ByVal other As MessageIndex) As Integer Implements System.IComparable(Of MessageIndex).CompareTo Me._messageIndex.CompareTo(other._messageIndex) End Function
which performs the CompareTo Function for two integers, then throws the result away and returns 0, because I forgot to either assign a value to CompareTo, or use Return, and that isn't flagged by the compiler because it's valid VB for a Value type.
I happen never to use "FunctionName = ReturnValue" because it hides information from maintenance programmers and also because I think it's insane .
Is there any way I can get the compiler to issue a warning whenever my Functions are missing a Return, whether it's a Value type or not?
Just so it's known, this question is mostly academic, even though I tried to use the concept in a real-world solution. I realize the example is contrived, but I believe the concept is valid.I want to write some fluent code like this:
[code]...
I realize that I can't force an anonymous type into a specific type (like implementing an interface or some other class), and I don't want the overhead of defining a specific class just to match my desired fluent name with the actual method name. So I was able to make the code work like this:
copy(my_first_file).to.Invoke(my_second_file)So there is no IntelliSense or type awareness there, and I have to include the Invoke in order to have the method run. How can I get more type safety and exclude the Invoke method, under these constraints: Anonymous Type returned from Method No additional classes or interfaces Preferably, I do not want to pass in another parameter to the copy() method that tells what type to return, unless copy becomes a generic method (but I think that means defining another class/interface, which I don't want to do)
The code below is used in a Linq to SQL solution, and is in the Load event of a form. It works fine to display an ordered list of people's names, and set the value member to that person's ID. However, I'm going to be using these 'people' comboboxes a lot, so I'd like to put a sub into my Utility code there is only one line in the form's Load event.
I want to call something like: Call ComboboBoxPeople(cbo, tblTurnbackMain, ReportedByID)
The sub would begin like: Public Sub ComboboxPeople(cbo as Combobox, tbl as 'sometype', fld as 'someothertype')
What can I use as the parameter types for tbl and fld?
I am having problems with my code, where I get the "Object cannot be cast from DBNull to other types" message when Insert(). I get the message on every Insert to this table, even though the data is successfully inserted. If I Update the same data after Insert(), it does so without error. I have checked my values and none of them are DBNull. Even when the same values are passed on Insert that are passed on Update.
My code is : Private Sub Save() Dim bo As tbl_GL bo = New tbl_GL() bo.ConnectionString = AppConfig.ConnectString [Code] .....
Suppose I want to write a function like the following (as usual, a trivial example for illustrative purposes):
Public Function calcSqSum(Of T)(ByVal list As IEnumerable(Of T)) As T Dim sumSq As T For Each item As T In list
[Code]....
As you can probably guess, this function causes an error because a generic object is not guaranteed to implement the + operator. As far as I know, though, any numerical type (Integer, Double, Decimal, etc.) will.
Is there a way to write a (quasi-)generic function that can accept any numerical type, without having to explicitly overload the function for every such type yourself?
Alternatively, I suppose an equally acceptable solution would be to somehow check if a type implements the '+' operator (or any operator generally associated with numerical types and used by the function).
Possible Duplicate: VB.NET Function Return If I have a function that returns a boolean, what is the difference between:Return False and Function = False
how return four value from a function. In general function will return single value but here i am going to return four different values. If possible please tell me how to store the function return values.
I've created my own version of HexToDec() to properly handle the negative flag. I saw other versions online that used "Not(value)" to do the Pos/Neg inversion, but that does not generate the proper value... No, it's not the most elegant my any means. But I couldn't find anything online that actually worked.
Long story short, I want to return NaN in the case that the function is passed a string that is not a valid hex string...
How do I do that? Everything I've tried generates a compiler error... (assign return value to double.nan, assign return value to non-numeric, etc)
Existing function is below:
Function HexToDec(ByVal hexStr As String, Optional ByVal signed As Boolean = False) As Long Dim lngFinal As Long
Here's what I'm looking to do. I have an array of objects that all implement a particular interface. What I want to do is search that array of objects for a particular object type.I have to do this in several places, so I'm trying to create a function where I pass in the object array and the type that I'm searching for, and have that function return the object that matches that type - or nothing if no match is found.I wrote the following, but it isn't working, so I'm missing something: