Function Returning An Object With Type Specified In The Argument
Mar 22, 2011
How can I create a function that returns an object of a certain type, which the user of the function specify in the argument (using vb.net 2010) ?
[Code]...
The code above doesn't work, but maybe it can explain what I want to achieve. Using this feature, I want to hydrate my data transfer objects from datatable. Client will just call this function, specifying which DTO the client wants, and this function will just create that DTO and populating the properties with reflection GetType.GetProperties().
If an object is created inside a function and the function returns that type of oject how is the memory handled.
Example:
Public Function GetEmployee(employeeid as integer) as employee Dim oEmployee as new employee oEmployee.FirstName="Bob"
[code]....
Does the variable that receive the object still a pointer to the memory location that was used inside the function?What about when you do a oEmployee2=oEmployee.Is oEmployee2 just a pointer? And any changes to oEmployee will now affect the other. Just trying to understand it from a memory perspective and how that scope works?
I've got this MustInherit class called ViewerElement, from which derive classes like ViewerLine, ViewerImage, etc.Then, in the main program I have a collection of different elements (Images, Lines, etc.). What I want to do is something like the following:
[Code]...
I'm getting desperate to implement this Clone method without getting errors. How should this be de done?
I've spent quite a bit of time reading up on generics, covariance, etc., and I am failing to understand why I get the aforementioned compiler error in this type of codeLet's say I have a base "Bill" class made up of a collection of Lines..
Public Class Bill(Of L As Line) Private _lines As List(Of L) Public Property Lines() As List(Of L)
My brain may just be fried since it's the end of the semester, but my teacher kicked this back to fix and resubmit...she said that I am "returning the name of your function rather than your variable name".
Here is my code for all 3 forms.
code:
These are forms to calculate the cost of getting a cell phone package.
I have the below lines of code :I am getting the error as mentioned in the Title above.This error has come after converting a C# Code to VB.NET Code which is mentioned below:
C#: public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context) { if (context == null)
I have some ActiveX that was written for VB6. They all seem to return Variant types. Ex.
[Code]....
In this case the GetPosition control is returning a robot axis position. It looks to me like the values coming back from the function is not getting converted to object type properly.
I have got this error No default member found for type 'VB$AnonymousDelegate_0(Of SqlDataReader,String,Object)'.
My Code is below
dsBranch.Tables.Add(GetDataTableFromSQLReader(dr, "")) - Calling Private Function GetDataTableFromSQLDataReader(ByVal dr As SqlDataReader, ByVal TableName As String)
CREATE TABLE [LogLevel] ( [Id] int primary key ,[Name] nvarchar(50) not null
[code]....
After creating a fresh endity model, I add the two tables above. When I try to build I get the following errors...
Type argument 'Inxsol.CommandPlan.Data.Model.Log.LogLevel' does not satisfy the 'Class' constraint for type parameter 'TEntity'. Value of type 'System.Data.Objects.DataClasses.EntityReference(Of Inxsol.CommandPlan.Data.Model.LogLevel)' cannot be converted to 'System.Data.Objects.DataClasses.EntityReference(Of Inxsol.CommandPlan.Data.Model.Log.LogLevel)'.
I'm using VB.NET 2003 with .NET Framework v1.1. I know it's getting a bit old now but I'm stuck with an existing program and don't have the time/knowhow to convert it to anything newer.
My question would probably be solved very easily with Generics in .NET 2.0 but as I can't use that right now, I'd like to know if the following is possible:
I have a load of manager classes which inherit from DictionaryBase and hold collections of my custom classes. For example, I have a Employee class, a Batch class, a Supplier class, etc... and each of these has a Manager classwhich holds a collection of them - eg: my PlantTrayMgr class holds collections of PlantTrays, etc..
I have one particular form where I have to populate loads of comboboxes with the contents of various Manager objects. What I currently do in my form load event to populate comboboxes is this:
Code: Option Strict On Option Explicit On Private _plantTrayMgr As PlantTrayMgr
Unable to cast object of type 'DynamicTreeNode' to type 'DynamicTreeNode'.
I have an inherited TreeView with an inherited class called DynamicTreeNode inside it. I am trying to loop through the DynamicTreeNodes in the TreeView.Nodes property which is causing this error.
I am using visual basic and trying to create function. I am getting this error: "Argument not specified for parameter '_IsDayRateCheckBox' of private function CalculateParikingCharges(_HoursDecimal As Decimal, _IsDayRateCheckBox As System.Windows.Forms.CheckBox) As Decimal'.
This is the code. Private Function CalculateParkingCharges(ByVal _HoursDecimal As Decimal, ByVal _IsDayRateCheckBox As CheckBox) As Decimal
This is how I call the function. Dim HoursDecimal As Decimal Dim AmountOwedDecimal As Decimal If Not Decimal.TryParse(HoursTextBox.Text, HoursDecimal) OrElse HoursDecimal < 0D Then ErrorLabel.Visible = True HoursTextBox.Focus() [Code] .....
I have to create a program that finds the total cost for pizza, fries, and soft drinks, and places that in a list box. So far, though, I'm stuck. I have this code so far:
Public Class frmBill Private Sub btnTotal_Click(ByVal sender As System.Object, [Code] .....
The errors that I am getting are: "Argument not specified for parameter 'D' of Public Function totalBill(P As Double, F As Double, D As Double) "Argument not specified for parameter 'F' of Public Function totalBill(P As Double, F As Double, D As Double) "Argument not specified for parameter 'P' of Public Function totalBill(P As Double, F As Double, D As Double)"
I need to access a DLL function which has a void pointer argument (probably written in C/C++). In VB6 it could be accessed by declaring the argument as 'Any'. VB2008/VB2010 does not know this variable type 'Any'.
I tried instead: Object, IntPtr, UIntPtr, Int32(), Long e.t.c. but there always occurs a compile error or a runtime error.I did a lot of Internet investigation but could not find any answer.
I have several parameters consisting of separate System.Byte() arrays. What I need is the syntax for a function declaration where the parameter is an array of arrays - I do not want to delve into System.Collections, and it's my preference not to CType each array into an Object and then back into a Byte().
Problem I'm having here is that:
Public Function MyFunc(arrayparam() As System.Byte) As System.Byte()
accepts a single-dimensional Array of individual Bytes.
Then Public Function MyFunc(arrayparam() As System.Byte()) As System.Byte()
VB.NET is not letting me dance with both feet, or piss with both hands. I've got one foot and one arm tied behind my back at every turn. I want to use this statement in a function: Return CType(Formatter.Deserialize(FS), DS) Seems doable, right?
Function ReadFileStream(ByRef FS As FileStream, ByRef DS As Object) Dim Formatter As New BinaryFormatter FS.Position = 0 Return CType(Formatter.Deserialize(FS), DS) End Function
Right? I mean, CType only accepts a class name as the second argument. I can't give it an instance of a class, or it croaks. Yet if I try to pass the class name as the argument to that function: DataInfo = ReadFileStream(FileStream1, DataStruct)
VB.NET croaks on this, too, complaining that "'DataStruct' is a type and cannot be used as an expression.". It's used as an expression by CType just fine outside of the function. Does the VB.NET legislature provide a clause that allows for a 'Type' to be passed as an argument in a function? Writing all the supporting code ten million times to deserialize a file using CType ten million times in a large application will get very complicated and tricky very quickly. It should be handled by a routine so that any cases and adjustments that come up can be dealt with in one spot.
returning a function value? I have a simple function that returns an integer. This function works fine when executed in SQL Developer but, when called from my apps, it always returns 2 regardless of anything. I have checked it over and over again, I cant seems to find where I am going wrong.
what changes I need to make to the code so that when no value for intPosition is found the textbox txtRoomNumber remains blank? At the moment a value is being entered into the textbox when no value for intPosition is found.
Code: Private Sub btnFindRoom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindRoom.Click Dim strRoomType As String Dim intRoomNumber As Integer
I have an Excel VBA function that takes a number of optional parameters, including an optional Range: Function DazBeta(A As Range, Z As Range, _ B As Integer, _ Optional Freq As Integer = 1, _ Optional c As Double = 0, _ Optional r As Range, _ Optional Pct As Boolean = True, _ Optional Label As Integer = 1)
I am translating to VB.NET, and it's the optional Range that is giving me grief because VB.NET does allow optional Ranges. Or rather, optional parameters must provide a default value. What is the recommended way to change the VB.NET function signature so that the code is callable from an Excel cell as a UDF? (The VB.NET implements a UDF, the assembly is registered as a COM server, and the Excel spreadsheet is told of this server and type library, allowing the VB.NET code to be called from an Excel spreadsheet cell.) I have other compilation problems, so I have not been able to explore this. I am thinking that accepting an optional Object (default value Nothing) might work and then I could cast the Object to a Range. Alternatively, if there were a default value that could be specified with an optional Range, that would work, too.
Someone (w69rdy) in Stack Overflow helped me out with a great example to handle DB output, that could potentially be NULL, passed into a function. The problem is I can understand the method as written in C# but I am having a problem understanding how to rewrite the method in VB.NET. The method uses generics and I am lost. Here is the method written in C# ..
public T ParseValue<T>(System.Data.SqlClient.SqlDataReader reader, string column) { T result = default(T); if (!reader.IsDBNull(reader.GetOrdinal(column)))
[code].....
How is this written in VB.NET? How does the method signature change when returning a generic type?