Generic Function With Special Case For String

Jun 4, 2012

What's the best way to handle this situation: I have this generic function
Function FieldValue(Of T)(row As DataRow,fieldName As String) As T
Return If(row.IsNull(fieldName),Nothing,CType(row(fieldName),T))
End Function
In the special case where the field value is null and T is String, I want to return String.Empty instead of Nothing.

View 1 Replies


ADVERTISEMENT

VS 2010 Generic Type Converting Function, With A Special Case

Aug 1, 2011

I wrote my own database handling code (actually, I write T4 text templating files that generate my database code for me) and part of it takes care of converting values from a database (as Objects) to the desired types.

I have been using this generic function successfully:

vb.net
Public Overridable Function ConvertType(Of T)(value As Object) As T
Try
Return If(value IsNot Nothing AndAlso value <> DBNull.Value, DirectCast(value, T), Nothing)

[Code]....

Simple enough, but it doesn't work... The return type of the function is (and must be) T, so I cannot return a Boolean because a Boolean cannot be converted to T!

Well... It can in this case, because I specifically check that T is Boolean, but the compiler doesn't know this so it doesn't allow me to return a Boolean. I cannot cast the boolean to T either.

View 8 Replies

VS 2010 Generic Type Converting Function, With A Special Case?

Aug 19, 2009

I wrote my own database handling code (actually, I write T4 text templating files that generate my database code for me) and part of it takes care of converting values from a database (as Objects) to the desired types. I have been using this generic function successfully:

vb.net
Public Overridable Function ConvertType(Of T)(value As Object) As T
Try

[code].....

View 13 Replies

String Function(s) To Change Only First Letter Of String To Upper Case?

Dec 20, 2010

string function(s) to change only first letter of string to Upper Case?

View 7 Replies

Function Which Take String (alfanumeric+special Char) As Input And Return Serial Key

May 6, 2010

i want to generate a serial key which depend on system harddisk manufacture id ...

so Is any function which take string of alfanumeric + special char as input and after encrypted return output as unique serial key / product key...

View 10 Replies

String Function To Fix Names Like O'Neill After Proper Case Conversion?

Feb 2, 2011

If I use code such as NewName = StrConv(OrigName, vbProperCase) to convert names to proper case, i.e. with initial capitals, I get unwanted results with Irish names like the following:

O'neill instead of O'Neill
O'grady instead of O'Grady

[code].....

View 7 Replies

.net - Value Of 'String' Cannot Be Converted To Type 'T Generic Function For Getting Query Strings?

Jul 21, 2011

I've got this function:

Public Shared Function GetQueryStringValue(Of T As Structure)(ByVal queryStringVariable As String) As T
Dim queryStringObject As Nullable(Of T) = Nothing
If queryStringVariable <> Nothing Then
If HttpContext.Current.Request.QueryString(queryStringVariable) IsNot Nothing Then
queryStringObject = DirectCast(HttpContext.Current.Request.QueryString(queryStringVariable), T)
End If

[Code]...

View 2 Replies

Code For "Case Keys.[special Character]"

Nov 2, 2009

In this thread I posted a problem regarding getting buttons to click when you hit certian keys. It was resolved like this:

Case Keys.A
btnA.PerformClick()

or

Case Keys.Num8
btn8.PerformClick()

But I want to use special characters (asterisk, slash, ampersand, etc) for certain buttons, and I can't find the code for that.

View 9 Replies

Sql - Special VB String Escape Character Like @'String' In C#

Dec 21, 2011

Does VB have an equivalent to the @ symbol in C# to quickly and efficiently escape a SQL command line string?

View 1 Replies

Select Case Shared Function?

Mar 29, 2010

xplain to me or give me an example of using a select case in a shared function

View 2 Replies

.net - Generic Sorter Function C# To VB

May 31, 2011

Anyone want to try converting this to VB?

[Code]...

It is supposed to sort any type of object. Taken from here

View 1 Replies

Calling A Generic Function In .NET / C#?

Jun 2, 2010

Question: I want to call a generic function, defined as:

Public Shared Function DeserializeFromXML(Of T)(Optional ByRef strFileNameAndPath As String = Nothing) As T

Now when I call it, I wanted to do it with any of the variants below:

Dim x As New XMLserialization.cConfiguration
x = XMLserialization.XMLserializeLDAPconfig.DeserializeFromXML(Of x)()
x = XMLserialization.XMLserializeLDAPconfig.DeserializeFromXML(GetType(x))()
x = XMLserialization.XMLserializeLDAPconfig.DeserializeFromXML(Of GetType(x))()

But it doesn't work.I find it very annoying and unreadable having to type

x = XMLserialization.XMLserializeLDAPconfig.DeserializeFromXML(Of XMLserialization.cConfiguration)()

Is there a way to call a generic function by getting the type from the instance ?

View 5 Replies

Generic OrderBy Function?

Jun 7, 2012

I have a grid view with a lot of columns, all need to be sorted on. I have seen a few snippets out there, but I cannot get any of them to work with my example. Here is what I have so far.

Protected Sub gvSearch_OnSorting(ByVal sender As Object, _
ByVal e As GridViewSortEventArgs)
If Not Session("sort") Is Nothing Then

[code].....

View 1 Replies

Non Case-Sensitive Searches Within ExecuteReader Function

Mar 24, 2010

could someone help me with some simple coding methodology for doing database searches within the ExecuteReader function that will return results regardless of case?For example, if the variable to search for within the database is John Smith,I would want the search to be successful if john smith is the user-provided variable.

View 8 Replies

VS 2008 - Ignore Case In Search Function

Jul 11, 2009

I am having problums with Ignore Case in searching. I got a checked listview and a listbox, it searches and fines just the exact word.
For i As Integer = 0 To (Me.Special_Search_Histroy_CheckedListview.CheckedItems.Count - 1)
'Dim itemString As String = Me.Load_Search_name_Listbox.Items(item).ToString
Dim strr As String = (Me.Special_Search_TextBox.Text)
Dim path As String = (Me.Special_Search_Histroy_CheckedListview.CheckedItems.Item(i).SubItems(0).Text)
Dim readText() As String = File.ReadAllLines(path.ToString)
[Code] .....

View 5 Replies

Write Case Sensitive Function In SQL Query?

Feb 25, 2009

How to write Case Sensitive Function In SQL Query?

View 3 Replies

Asp.net - Fix Special Characters In String?

Aug 3, 2010

I've got a program that in a nutshell reads values from a SQL database and writes them to a tab-delimited text file.The issue is that some of the values in the database have special characters (TM, dash, ellipsis, etc.) When written to the text file, the formatting is lost and they come across as junk "â„ or â€" etc"

When the value is viewed in the immediate window, before it is written to the txt file, everything looks fine. My guess is that this is an issue of encoding. But, I'm not real sure how to proceed, where to look, or what to look for.Is this ASCII or UTF-8? If it's one of those how do I correct it before it's written to the text file.Here's how I build the text file (where feedStr is a StringBuilder)

objReader = New StreamWriter(filePath)
objReader.Write(feedStr)
objReader.Close()

View 1 Replies

.net - Generic Open Form Function?

Oct 12, 2011

written a generic "LaunchForm" function? For all the menu items I have that open a form, I would like to write one function that will launch the form as opposed to writing the same code several times.

View 2 Replies

.net - How To Make Function Return Generic Value

Sep 16, 2011

I have an abstract class in VB.NET. I want all classes that inherit from this class to return whatever value makes sense. For example, it could be an Decimal, Integer, String. How can I delcare the function in the abstract base class to allow for this? Is this even possible?

[Code]...

View 2 Replies

.NET : Generic Function To Return A Control?

Oct 14, 2010

I have created a couple of simple functions in VB.NET that simply return a control of a certain type that I already know, such as HtmlInputHidden, Label, etc. That means that each of the functions is created only for that special purpose. What I'd like to do is combine all those functions into one function using generics. The common things shared by each of the functions is a control Id and a control type.What I have got so far is:

Public Function GetControl(Of T)(ByVal ctrlId As String) As T
Dim ctrl As Control = Me.FindControl(ctrlId)
If (Not ctrl Is Nothing) Then

[code]....

But the line " GetControl = CType(ctrl, T)" is giving me a compile error:

Value of type 'System.Web.UI.Control' cannot be converted to 'T'

This is in .NET Framework 2.0.

View 2 Replies

Create A Function That Uses A Generic Structure?

Apr 1, 2009

I am attempting to create a generic function that the students in my introductory VB .NET course can use to search a single dimension array of a structure.[code]...

My question is: Is there a way to reference the individual structure fields in the array from inside the function? I was trying to make it generic so that the student could simply pass their array into the function - their structure may be named differently than mine and the field names may be different.

I suspect there are other ways to deal with this situation, but I was trying to keep it to just a simple single-dimension array of a structure. I don't think it is possible to do what I want, but I wondered what others thought.

View 5 Replies

Generic Function For Checking For A Value Of Null?

Aug 24, 2010

I am trying to write a generic function that will check each database parameter to see if it is null, and if so, return DBNull; if not, return the object.So here is my function:

Public Shared Function CheckForNull(ByVal obj As Object) As Object
If obj <> Nothing Then Return obj Else Return DBNull.Value
End Function

[code].....

View 1 Replies

Have A Static Generic Function In .net 2.0 VB Code?

Sep 2, 2009

I must be doing something wrong here (because really, what are the chances of me tripping over another bug in the Vb.net compiler?)I have a static generic function in .net 2.0 Vb code, I thought it was time to "upgrade" it to be an extension method, but the compiler complains with Extension method 'AddIfUnqiue' has type constraints that can never be satisfied.

Here's a trivial example that displays the same problem. The old static version (which works fine) followed by the extension method

Public Class MyStaticClass
Public Shared Sub AddIfUnqiue(Of T, L As {List(Of T)})(ByVal this As L, ByVal item As T)
If this.IndexOf(item) < 0 Then

[code].....

View 1 Replies

Make A Shared Function Generic?

Feb 16, 2011

I can create classes that use generics, however I was wondering if it is possible to apply this to a shared method rather than rely on the caller casting to the correct type.This is the method:

Public Shared Function DeserializeObject(ByVal serializedXml As String, ByVal givenType As System.Type) As Object
Dim serializer As New XmlSerializer(givenType)
Return serializer.Deserialize(New IO.StringReader(serializedXml))
End Function

I'm pretty sure this can't be done, but thought I'd check (if so Extra points will be awarded if someone can technically explain why the compiler can't do this)..

View 3 Replies

Negative Effects Of Using A Generic Function

Oct 26, 2011

My question here is, what are the negative effects of using a generic function such as this? Calling this function does work, and in a test console module, it compiles perfectly fine. I do know this is not a strongly typed function, and is %100 bad practice. But it works perfectly. The purpose of a function like this, would be to handle string input that needs to be inserted in a particular format depending on the type. I also read some other questions on this here on stackoverflow, and suggestions pointed to using (Of T) functions, and variations like that. Why not do it this way? or is there another simple way of accomplishing this without creating a whole nothing class or larger amounts of code.[code]

View 1 Replies

Write The Generic Version Of This Function?

Jan 18, 2011

I like to turn Option Strict On I cant figure out how to write the generic version of this function?

Public
Shared
Sub UpdateItem(ByRef
oldValue As

[code]....

View 8 Replies

Asp.net - Remove Special Character From String In .net?

Apr 14, 2012

this would be my string Panipat,Patna,Result should be Panipat,Patna,Panipat,Patna,Result should be Panipat,Patna Panipat,Result should be Panipat,Panipat,,Result should be Panipat How can i do it

View 3 Replies

Escaping Special Charecters In A String?

Jun 6, 2010

how to escape all special charecters.in this string there must be more than one escape charecters,for example

Q:95932010_03_02_Design_internal_work�1-ARCHPRESENTATION20100510Linkcad2010-05-192010_05_09_Townhouses Model (1)

in this there is "" , "("

is there any way to find out all this special ch like wise i wanted to escape all the special charecters in a string at once rather than finding and replaceing it one by one

View 7 Replies

Match Special Character Anywhere In String?

Feb 21, 2012

I am not a regex guy, so I need a little help, just cant get my head around this...

I am writing registration page and using asp.net validators. I need a regex validator to match a special character anywhere within a word, so it will satisfy requirement which says that "ID must have at least one special character." Allowed characters are[code]...

Once again, I am a junior, and having problems with wrapping my head around this stuff at this moment..

View 4 Replies

C# - Make A Function With Return Type Generic?

Apr 29, 2010

Currently I have written a function to deserialize XML as seen below.How do I change it so I don't have to replace the type every time I want to serialize another object type ? The current object type is cToolConfig. How do I make this function generic ?

Public Shared Function DeserializeFromXML(ByRef strFileNameAndPath As String) As XMLhandler.XMLserialization.cToolConfig
Dim deserializer As New System.Xml.Serialization.XmlSerializer(GetType(cToolConfig))
Dim srEncodingReader As IO.StreamReader = New IO.StreamReader(strFileNameAndPath, System.Text.Encoding.UTF8)
Dim ThisFacility As cToolConfig

[code]....

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved