Class ' cannot be indexed because it has no default property - vb.net

I get the following error Class 'PropGenie_WebService.Branch' cannot be indexed because it has no default property. And I am not sure why. I have googled but don't get a proper explanation or fix. C# help welcome.
My code in the branch.vb class:
Public Function Update() As Branch
Return Update(Me, Path) 'error at update.
End Function
And in my Base class (Resources.vb) I have:
Public Shared Function Update(Of T As {Resources, New})(resource As T, path As String) As T
Dim request = CreateRequest(path & "/{id}", Method.PATCH)
request.AddUrlSegment("id", resource.Id.ToString(CultureInfo.InvariantCulture))
request.AddBody(resource)
Dim Client = CreateClient()
Dim responce = Client.Execute(Of T)(request)
If responce.StatusCode <> HttpStatusCode.OK Then
Throw New InvalidOperationException("Update Failed" & Convert.ToString(responce.StatusCode))
End If
Return responce.Data
End Function

You need to specify the class in which the shared function is also, or it will try to use the Update function in the object you are in.
Public Function Update() As Branch
Return Resources.Update(Me, Path)
End Function

Related

Visual Basic API returns an exception error

I am trying to build a new leg in a WebAPI that I have written in visual basic. The code I am trying to execute is this:
Public Class getDatabaseDataController
Inherits ApiController
Private product_information As getDatabaseData() = New getDatabaseData() _
{New getDatabaseData() With {
.return_response = "return response"
}}
Public Function GetCars(<FromUri> ByVal dbRequest As String) As IEnumerable(Of getDatabaseData)
Dim return_string As String = ""
Dim passedData As String = dbRequest
return_string = passedData
Dim mqResponse As getDatabaseData() = New getDatabaseData() _
{New getDatabaseData() With {
.return_response = Str(return_string)
}}
'Return product_information
Return mqResponse
End Function
End Class
I have a public class called getDatabaseData defined as follows:
Public Class getDatabaseData
Private _Return_Response As String
Public Property return_response() As String
Get
Return _Return_Response
End Get
Set(value As String)
_Return_Response = value
End Set
End Property
End Class
I call the API with this call:
http://localhost:12976/api/getDatabaseData?dbRequest=A
All I am trying to do at this point is have the API return the value A. When I debug the API and send in the request, this is what is returned:
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Argument 'Number' cannot be converted to a numeric value.
</ExceptionMessage>
<ExceptionType>System.InvalidCastException</ExceptionType>
I dont see anything in the code that would indicate a 'Numeric'. What am I missing?
Thanks in advance for any assistance.

Map Virtual path to physical path

I have an issue with the method Server.MapPath.
I am currently using the method HttpContext.Current.Server.MapPath("MyPath") to obtain the physical path of "MyPath" in an ApiController, get method.
If I try to use the same method in an ApiController which returns an HttpResponse, it gives the error:
Current is not a member of System.Net.Http.HttpContent
How can I use the method Server.MapPath in the context of an HttpResponseMessage?
(I am working in Visual Basic)
Edit: I am using it like this:
<HttpGet>
Public Function MyFunc() As HttpResponseMessage
Try
Dim streamContent = New PushStreamContent(_
Function(outputStream, httpContext, transportContent)
Try
Dim lPath = httpContext.Current.Server.MapPath(MyPath)
.... some code
End Try
End Function)
Dim lResult = New HttpResponseMessage(HttpStatusCode.OK)
lResult.Content = streamContent
Return lResult
End Try
End Function
The second argument passed to your callback function is of type HttpContent. You're effectively hiding HttpContext inside your lambda because you're named that parameter HttpContext. Try:
Dim streamContent = New PushStreamContent(_
Function(outputStream, content, transportContent) 'Renamed parameter here
Try
Dim lPath = HttpContext.Current.Server.MapPath(MyPath)
'.... some code
Catch ex As Exception
Finally
outputStream.Close()
End Try
End Function)
The PushStreamContent constructor definition is:
Public Sub New ( _
onStreamAvailable As Action(Of Stream, HttpContent, TransportContext) _
)

Unable to cast object of type 'System.Data.Linq.Table`1[]' to type 'System.Collections.Generic.IEnumerable`1[]

Currently I am receiving the error unable to cast table to IEnumerable, which I am finding very perplexing as at no time am I specifying a table.
I will explain step by step what is being done and please do point out where I went wrong.
DAL:
Public Function GetCategories(ByVal DB As WholeSaleDataDataContext, Optional CategoryID? As Integer = Nothing) As IEnumerable(Of Category)
If CategoryID Is Nothing Then
Return DB.Categories.AsEnumerable()
Else
Return DB.Categories.Where(Function(Cat) Cat.CategoryID.Equals(CategoryID)).AsEnumerable
End If
End Function
WCF:
<DataContract(), KnownType(GetType(WholeSalesDAL.Category))>
Public Class Category2
End Class
Public Function GetCategory() As IEnumerable(Of Category2) Implements IWholeSaleData.GetCategory
Return WholeSalesDAL.GetCategories(_DB).AsEnumerable
End Function
Website:
Private WholeSaleServices As WholeSaleService.WholeSaleDataClient
Public Sub New()
WholeSaleServices = New WholeSaleService.WholeSaleDataClient("BasicHttpBinding_IWholeSaleData", "http://localhost:16722/WholeSaleService.svc")
GetCategories()
End Sub
Public Sub GetCategories()
Dim z = WholeSaleServices.GetCategory
End Sub
I see two problems with your code:
EDIT: #1 explained...
1) In your WCF section, WholeSalesDAL.GetCategories(_DB) is IEnumerable(Of Category), but your function is supposed to return `IEnumerable(Of Category2). I don't see how that type conversion is being done.
2) I believe you are using AsEnumerable unnecessarily, and that is probably what is causing the exception. Do you know if you need to call AsEnumerable in either spot you have it? I would try removing it.

VB: Problems with using variable from another class + what to do with not used interface`s functions

I have a problem with getting variable from another class and cannot understand what to do with interface`s functions which have already existed in another class.
What I have:
Form where clicking on a button I should see reversed string:
(I want to call pooraja.StringReverse which is below)
Private Sub btnPoora1_Click(sender As System.Object, e As System.EventArgs) _
Handles btnPoora1.Click
'Dim text As PrjTekstiPooraja.ITeisendused = New PrjTekstiPooraja.CtekstiPooraja
Dim text As PrjTekstiPooraja.ITeisendused = New PrjTekstiPooraja.CtekstiPooraja
Dim pooraja As PrjTekstiPooraja.ITeisendused = New PrjTekstiPooraja.CAlgrotimilinePooraja
text.strText = txtSisendTekst.Text
txtValjundTekst1.Text = pooraja.stringReverse
text.intStart = 1
text.intEnd = Len(txtSisendTekst.Text)
ascFSymbol.Text = text.ascFirstSymbol
ascLSymbol.Text = text.ascLastSymbol()
End Sub
CtekstiPooraja:
(Thiss class will be used to store data.Under data I mean strPooratavText. Data will be used in CAlgoritmilinePooraja)
Public Class CtekstiPooraja
Implements ITeisendused
Public intStartSymbol As Integer
Public intEndSymbol As Integer
Public strPooratavText As String
Private Property intEnd As Integer Implements ITeisendused.intEnd
Get
Return intEndSymbol
End Get
Set(ByVal value As Integer)
intEndSymbol = value
End Set
End Property
Private Property intStart As Integer Implements ITeisendused.intStart
Get
Return intStartSymbol
End Get
Set(ByVal value As Integer)
intStartSymbol = value
End Set
End Property
Public Function pooraText() As String Implements ITeisendused.pooraText
Return StrReverse(strPooratavText)
End Function
Public Property strText As String Implements ITeisendused.strText
Get
Return strPooratavText
End Get
Set(ByVal value As String)
strPooratavText = value
MsgBox(strPooratavText)
End Set
End Property
Public Sub teisendaText(ByRef strSisendText As String) Implements ITeisendused.teisendaText
strPooratavText = StrReverse(strSisendText)
End Sub
Public Function ascFirstSymbol() As String Implements ITeisendused.ascFirstSymbol
Return Asc(GetChar(strPooratavText, intStartSymbol))
End Function
Public Function ascLastSymbol() As String Implements ITeisendused.ascLastSymbol
Return Asc(GetChar(strPooratavText, intEndSymbol))
End Function
Public Function stringReverse() As String Implements ITeisendused.stringReverse
Return Nothing
End Function
End Class
CAlgrotimilinePooraja:
(This class will be called by form button. There I need to use stringReverse function with data from CtekstiPooraja. The problem is that everywhere is used the same interface and there is some functions and procedures from this interface which isnt necessary. I dont know what value should return these unused functions/procedures. Just using "return Nothing or return 0/ "" is bad idea, may be there is possible somehow referenceto to CTekstiPooraja functions/procedures variables")
Public Class CAlgrotimilinePooraja
Implements ITeisendused
Private x As New PrjTekstiPooraja.CtekstiPooraja
Public Function stringReverse() As String Implements ITeisendused.stringReverse
MsgBox(x.strPooratavText)
Dim i As Integer = 0
Dim j As Integer
Dim characters(j) As Char
Dim newString(j) As Char
characters = x.strPooratavText.ToCharArray()
newString = x.strPooratavText.ToCharArray()
Do While i <= j - 1
newString(i) = characters(j - 1)
newString(j - 1) = characters(i)
i += 1
j -= 1
Loop
Return newString
End Function
Public Function ascFirstSymbol() As String Implements ITeisendused.ascFirstSymbol
Return x.ascFirstSymbol()
End Function
Public Function ascLastSymbol() As String Implements ITeisendused.ascLastSymbol
Return Nothing
End Function
Public Property intEnd As Integer Implements ITeisendused.intEnd
Get
Return x.intEndSymbol
End Get
Set(ByVal value As Integer)
End Set
End Property
Public Property intStart As Integer Implements ITeisendused.intStart
Get
Return x.intStartSymbol
End Get
Set(ByVal value As Integer)
End Set
End Property
Public Function pooraText() As String Implements ITeisendused.pooraText
Return x.pooraText()
End Function
Public Property strText As String Implements ITeisendused.strText
Get
Return x.strPooratavText
End Get
Set(ByVal value As String)
End Set
End Property
Public Sub teisendaText(ByRef strSisendText As String) Implements ITeisendused.teisendaText
x.strPooratavText = StrReverse(strSisendText)
End Sub
End Class
MyInterface:
Public Interface ITeisendused
Property intStart As Integer
Property intEnd As Integer
Property strText As String
Function pooraText() As String
Function ascFirstSymbol() As String
Function ascLastSymbol() As String
Function stringReverse() As String
Sub teisendaText(ByRef strSisendText As String)
End Interface
I cannot understand how to get variable strPooratavText from CTekstiPooraja to CAlgrotimilinePooraja. Usually that instancewhich I create worked but not now. And I cannot understand what to do with already existed function and procedures in CAlgoritmilinePooraja when the same function and procedures has in another class. Maybe, it is possible to reference them somehow to existed functions/procedures in CTekstiPooraja? Could you explain me how to id, already tired to surf Internet to find a solution for it, have already try a lot.
Well, I think you have a fundamental problem with understanding interfaces. They describe data and behavior, it should be extremely rare to want to implement part of an interface.
That said, if you do want to implement part of an interface, instead of returning bogus data, throw an exception for behavior you don't implement.
Your specific problem is that CAlgoritmilinePooraja works on an instance of CtekstiPooraja, but it creates a new instance instead of using an existing one. Add
Sub New(incomingX as CtekstiPooraja)
x = incomingX
End Sub
to CAlgoritmilinePooraja. And then in your event, use....
Dim text As PrjTekstiPooraja.CtekstiPooraja = New PrjTekstiPooraja.CtekstiPooraja
text.strText = txtSisendTekst.Text
Dim pooraja As PrjTekstiPooraja.ITeisendused = New PrjTekstiPooraja.CAlgrotimilinePooraja(text)
That is the minimum change to your design that gets what you want to happen to happen but it's problably not what you should do. Other than implementing strReverse, CtekstiPooraja seems to be what you want, CAlgrotimilinePooraja looks to do just one thing, the actual string reversal.
I would move the implementation of strReverse into CtekstiPooraja, and then eliminate CAlgrotimilinePooraja.
PS I would try to stick to English for class names as well as functions and variables.

Create a new instance of a type given as parameter

I've searched for an answer and found some c#-examples, but could not get this running in vb.net:
I thought of something like the following:
public function f(ByVal t as System.Type)
dim obj as t
dim a(2) as t
obj = new t
obj.someProperty = 1
a(0) = obj
obj = new t
obj.someProperty = 2
a(1) = obj
return a
End Function
I know, I can create a new instance with the Activator.Create... methods, but how to create an array of this type or just declare a new variable? (dim)
Thanks in advance!
It really depends on the type itself. If the type is a reference type and has an empty constructor (a constructor accepting zero arguments), the following code should create an insance of it:
Using Generics:
Public Function f(Of T)() As T
Dim tmp As T = GetType(T).GetConstructor(New System.Type() {}).Invoke(New Object() {})
Return tmp
End Function
Using a type parameter:
Public Function f(ByVal t As System.Type) As Object
Return t.GetConstructor(New System.Type() {}).Invoke(New Object() {})
End Function
Personaly I like this syntax much more.
Public Class Test(Of T As {New})
Public Shared Function GetInstance() As T
Return New T
End Function
End Class
Or if you want to limit the possible types:
Public Class Test(Of T As {New, MyObjectBase})
Public Shared Function GetInstance() As T
Return New T
End Function
End Class