Object returning from Function vb.net - asp.net

I'm trying to return an object from a function.
E.g. Ive got a function populateDog that returns Dog
So in my aspx class I want to be able to be able to pass in Lassie as the name of the dog(I have a dog class) and have the function return the object with the data it populated.
So that in my aspx class i can go lassie.color, lassie.breed
Main Goal is: lbl.txt = Lassie.Color
Thanks
EDIT
Public Function populateDog(ByVal dName As String) As dog
dbConnection()
Dim ObjDog As New dog(dName)
ObjDog.sBreed = "Collie"
Return ObjDog
End Function
The idea was to have a database and I would eventually pass in an ID to query results and return it. For now though I just wanna get this understanding and move forward.

Public Function populateDog(ByVal dName As String) As dog
dbConnection()
Dim ObjDog As New dog(dName)
ObjDog.sBreed = "Collie"
ObjDog.Color = "White"
Return ObjDog
End Function
and
Dim Lassie as dog
Lassie = populateDog("Lassie")
lbl.Text = Lassie.Color
assuming your dog class is something like
Class dog
Public sBreed As String
Public Color As String
' other properties and functions
End Class

Related

Using Objects in a dictionary in vb.net

This is how I Check if my dictionary occurences already contains a key and a value and assign keys and values to it... but I need another value, so how do I use the object here?
Occurences.Add(xRows.Cells(4).Value.ToString(), Object)
Somehow like this:
occurences(xRows.Cells(4).Value.ToString()) = Double.Parse(occurences(xRows.Cells(4).Value.ToString()).ToString()) + <"1st object value here">)
But this is how I currently do it which only has a key and a value but I need something like key, object which has (value, value), something like that:
Actual code:
If (occurences.ContainsKey(xRows.Cells(4).Value.ToString())) Then
occurences(xRows.Cells(4).Value.ToString()) = Double.Parse(occurences(xRows.Cells(4).Value.ToString()).ToString()) + Double.Parse(xRows.Cells(7).Value.ToString())
Else
occurences.Add(xRows.Cells(4).Value.ToString(), Double.Parse(xRows.Cells(7).Value.ToString()))
End If
Next
Then i have another code for insert where i need to use the 2nd value of the object.
Using commm As New MySqlCommand()
With commm
.Parameters.Clear()
.Parameters.AddWithValue("#iBrnchCde", cmbBrnchCode.Text)
.Parameters.AddWithValue("#iFCode", pair.Key)
.Parameters.AddWithValue("#iDesc", <"2nd OBJECT VALUE HERE"> )
.Parameters.AddWithValue("#iQty", pair.Value)
.CommandText = oInsertString
.Connection = _conn
.CommandType = CommandType.Text
End With
commm.ExecuteNonQuery()
End Using
Do the following:
Class CustomRowObject
Public Property Name() As String
Public Property Quantity() As Double
Public Property Description() As String
End Class
If (occurences.ContainsKey(xRows.Cells(4).Value.ToString())) Then
occurences(xRows.Cells(4).Value.ToString()).Quantity = Double.Parse(occurences(xRows.Cells(4).Value.ToString()).ToString()) + Double.Parse(xRows.Cells(7).Value.ToString())
Else
occurences.Add(xRows.Cells(4).Value.ToString(),New CustomRowObject With { .Name = a.Cells("ProductName").Value.ToString(),.Description = .Cells("ProductDesc").Value.ToString(), .Quantity = a.Cells("Quantity").Value.ToString()})
End If
Next
Then reference it via occurences("keynamegoeshere").Quantity for use in your SQL.
I'm not sure I understand your question fully, but if I do I would suggest a different approach:
Why not make a class that holds your values, then add your class to the dictionary - or - create a dictionary of the class type:
Public myClass
Public Code As String
Public Desc as String
....
End Class
Dim myClassInstance as New myClass
'initialize fields
occurences.Add(key, myClassInstance)
To retrieve get the value as an object and use if typeof value is myclass then - if you choose object as type for the dictionary.
If you choose object you can store different classes and types of course, but the cost is the casting. If possible make a dictionary of the type you will use to avoid casting.
Private myDictionary as New Dictionary(Of String, myClass)

How to write a contains statement to match a member of a class?

If I have the following structure:
Public Class UserData
Public ID As String
Public Name As String
End Class
How can I select it in a conditional like this?
Dim myUsers As New System.Collections.Generic.List(Of UserData)
If myUsers.Contains(.ID = "1") = True Then
...
I know that myUsers.Contains(.ID = "1") is totally wrong, but I am curious how to do something like that? Is it possible? Is this a job for LINQ?
How about this:
If myUsers.Any(Function(u) u.ID = "1") Then
...
Of course, if you're going to do this more than once, you'll probably be better off creating a set to search in:
Dim myUserSet = New HashSet(Of String)(myUsers.[Select](Function(u) u.ID))
For Each userId In selectedUserIds
If myUserSet.Contains(userId) Then
...
My vb is rusty. Please forgive syntax errors
Here's the VB version:
If myUsers.Any(Function(i) i.ID = "1") Then ...
Why don't you use a Dictionary
var myUserDict = new Dictionary<String, UserData>
if(myUserDict.ContainsKey("1"))..
Makes only sense of course if you are going to do anything with the selected user.

Looks like a query string, but won't act as a query string

I am working with VB in asp.net,
The basic problem is, I want to pair up the elements in a string, exactly like request.QueryString() will do to the elements in the query string of the web page.
However instead of the function looking at the current webpage query string I want it to look at a string (that is in the exact form of a query string) stored as a variable.
So if I define a string such as:
Dim LooksLikeAQueryString As String = "?category1=answer1&category2=answer2"
I want a function that if I input LooksLikeAQueryString and "category1" it outputs "answer1" etc.
Is there anything that can already do this or do I have to build my own function? If I have to build my own, any tips?
I should add that in this case I won't be able to append the string to the url and then run request.QueryString.
You can use the HttpUtility.ParseQueryString method - MSDN link
ParseQueryString will do it for you - something along these lines:
Private Function QueryStringValue(queryString As String, key As String) As String
Dim qscoll As NameValueCollection = HttpUtility.ParseQueryString(queryString)
For Each s As String In qscoll.AllKeys
If s = key Then Return qscoll(s)
Next
Return Nothing
End Function
Usage:
Dim LooksLikeAQueryString As String = "?category1=answer1&category2=answer2"
Response.Write(QueryStringValue(LooksLikeAQueryString, "category2"))
If you dont want the dependancy of System.Web, of the top of my head
public string GetValue(string fakeQueryString,string key)
{
return fakeQueryString.Replace("?",String.Empty).Split('&')
.FirstOrDefault(item=>item.Split('=')[0] == key);
}

VB.net "'Me' is valid only within an instance method" inside PageMethods

I'm getting "Me' is valid only within an instance method" error when I convert function to a PageMethods.
Private Shared objDT As System.Data.DataTable
Private Shared objDR As System.Data.DataRow
<WebMethod()> Public Shared Function addstn(itemID As String) As String
For Each Me.objDR In objDT.Rows
If objDR("ProductID") = ProductID Then
If objDR("Options") = desc Then
objDR("Quantity") += 1
blnMatch = True
Exit For
End If
End If
Next
End Function
If I remove Me, I get different error.
I got this shopping cart script somewhere online and I'm not sure what to replace "Me.objDR" with something else.
Thank you in advance
The Me keyword provides a way to refer to the specific instance of a class or structure in which the code is currently executing.
In a shared context there is no instance. You can reference your variable directly or via ClassName.VariableName.
This could work(i'm not sure wherefrom the other variables are):
For Each objDR In objDT.Rows
If ProductID.Equals(objDR("ProductID")) _
AndAlso desc.Equals(objDR("Options"))Then
Dim q = CInt(objDR("Quantity"))
objDR("Quantity") = q + 1
blnMatch = True
Exit For
End If
Next
http://msdn.microsoft.com/en-us/library/20fy88e0%28v=vs.80%29.aspx
Me cannot be used since its a shared method, shared methods are not accessed through an instance.
What is the other error you receive?
Public Shared Function addstn(...
The "Shared" means it's not an instance method. If you want to use "Me", you can't use "Shared". Use the name of the class/module instead.

ClearCanvas DicomFile.DataSet -- How to add a new Tag?

im trying to add a new tag to my DicomFile.DataSet in ClearCanvas.
I notice there is the method "DicomFile.DataSet.RemoveAttribute" but no "AddAtribute" method. So I have been looking at the method "LoadDicomFields" & "SaveDicomFields" but so far can't seem to get them to work. Ive tried to pass in a "DicomFieldAttribute" to these methods, but to no avail.
What am I missing here? Or what do I need to do to add a new tag to the DataSet.
DicomFieldAttribute c = new DicomFieldAttribute(tag);
List<DicomFieldAttribute> cs = new List<DicomFieldAttribute>();
cs.Add(c);
DicomFile.DataSet.LoadDicomFields(cs);
DicomFile.DataSet.SaveDicomFields(cs);
if(DicomFile.DataSet.Contains(tag))
{
tag = 0; //BreakPoint never reached here
}
Or I tried this as well::
DicomFieldAttribute c = new DicomFieldAttribute(tag);
DicomFile.DataSet.LoadDicomFields(c);
DicomFile.DataSet.SaveDicomFields(c);
if(DicomFile.DataSet.Contains(tag))
{
tag = 0; //BreakPoint never reached here
}
Ive been stuck on what would seem to be a trivial task.
You're confusing a bit the use of attributes. The DicomFiledAttribute is a .NET attribute that can be placed on members of a class so that the class is automatically populated with values from a DicomAttributeCollection or or to have the class automatically populated with values from the DicomAttribute Collection. Ie, given a test class like this:
public class TestClass
{
[DicomField(DicomTags.SopClassUid, DefaultValue = DicomFieldDefault.Default)]
public DicomUid SopClassUid = null;
[DicomField(DicomTags.SopInstanceUid, DefaultValue = DicomFieldDefault.Default)]
public DicomUid SOPInstanceUID = null;
[DicomField(DicomTags.StudyDate, DefaultValue = DicomFieldDefault.Default)]
public DateTime StudyDate;
}
You could populate an instance of the class like this:
DicomFile file = new DicomFile("filename.dcm");
file.Load();
TestClass testInstance = new TestClass();
file.DataSet.LoadDicomFields(testInstance);
// testInstance should now be populated with the values from file
If you're interested in just populating some DICOM tags, the DicomAttributeCollection has an indexer in it. The indexer will automatically create a DicomAttribute instance if it doesn't already exist, for the tag requested via the indexer. So, to populate a value, you can do soemthing like this:
DicomFile file = new DicomFile("filename.dcm");
file.DataSet[DicomTags.SopInstanceUid].SetStringValue("1.1.1");
If you want to create the DicomAttribute yourself, you can do something like this:
DicomAttribute attrib = new DicomAttributeUI(DicomTags.SopInstanceUid);
attrib.SetStringValue("1.1.1");
DicomFile file = new DicomFile("filename.dcm");
file.DataSet[DicomTags.SopInstanceUid] = attrib;

Resources