I Want to make a class accept string variable like this

Dim mystring as custstring
mystring = "test var"

like how we declare string classes

I tried to use property as such

Public class cust
dim inputstr as string = ""
Public Property cust() As String
            Get
                Return inputstr.ToString()
            End Get
            Set(ByVal value As String)
                inputstr = value
            End Set
        End Property
End Class

But it doesnt seem to work

thnx in advnc

You can't use the class name as a variable. You have to declare the class as follows:

Public Class cust

    Private m_inputstr As String

    Public Property inputstr() As String
        Get
            Return m_inputstr
        End Get
        Set(ByVal value As String)
            m_inputstr = value
        End Set
    End Property

End Class

Then you can use it as follows:

Dim test As New cust
test.inputstr = "splunge"
Debug.WriteLine(test.inputstr)

At least that is how I would do it. I'm still not that well versed in the finer points of OOP.

Edited 5 Years Ago by Reverend Jim: n/a

Yes, Jim is right, you cannot assign a variable (value) to a reference (when you create an instance of some class, this is called a reference to this created class instance).
So, the closest you can get it to do like Jim showed.

Then how does the

Dim mystr as string

work? I want to get something like that,

maybe you misunderstood me.
The Dim..as String <--- That word string is a class right? I want to make a class simillar to the string class where I can then delcare variables like

Dim mycust as myclass
mycust = "test data"

Edited 5 Years Ago by raaif: n/a

"String" isn't a class. It's a data type like Integer. Using the cust class that we defined above, we declare a variable that can hold an instance of the class as follows:

Dim mycust as cust

This does not create an instance of the class cust. It declares a variable that can hold a reference to (address of) an instance of cust. However, when you then say

mycust = New cust

the New keyword creates an instance of cust and places the address of it into mycust. Now you can use mycust to get at the properties and methods defined in cust. You could do the above two steps in one step by

Dim mycust as New cust

But when you do

Dim mystring as String

You do not have to use the New keyword before doing

mystring = "splunge"

That is the main difference between a class and a primitive data type. Now back to your example. Since you only want to store a string literal, why are you so determined to create a string-like class? What is it that you want to do that you cannot do with a string variable?

Thnx Ive understood now :)

Since you only want to store a string literal, why are you so determined to create a string-like class? What is it that you want to do that you cannot do with a string variable?

It is that Im a native Maldivian, and the writing script is like in image (attached)

So string handling of those data are very difficult so I am trying to simplify it so that a string class simillar to the actual string class can be implemented for easier native script string handling :) If you guys have any idea pls let me know

Attachments Capture.JPG 21.79 KB

How do you handle those diacritics or glyphs? As a Latin letters or some other way (like images)? I mean what kind of keyboard you use to write Maldivian?

If Maldivian glyphs can be represented as Latin letters (just like Cyrillic letters can be represent), it shouldn't be hard to write a class MaldivianString which handles strings as ... well, strings internally.

The diacritics are handled as Unicode characters along with the letters(U+0780–U+07BF) and can be represented as Latin like Cyrillic, But how to do it?

Here's how I would try to do it. First, you can't inherit String class. Meaning that you have to rewrite every method you need. Here's the class

Public Class MaldivianString

    ' Holds this instance's value
    Private thisString As String = String.Empty

    ' Constructor. NB. String class do not have this kind of constructor!!!
    Public Sub New(ByVal value As String)
        thisString = value
    End Sub

    ' Properties
    ' Chars
    ' Length

    Default Public Property assignValue(ByVal varName As Object) As String
        Get
            Return thisString
        End Get
        Set(ByVal Value As String)
            thisString = value
        End Set

    End Property

    ' Methods

    Public Overrides Function ToString() As String
        ' No conversion in this overload
        Return thisString
    End Function

End Class

I used Public Sub New(ByVal value As String) as a constructor. String class doesn't have it so I suggest removing it (unless you find it handy).

Only method I added is ToString(). It overrides Object.ToString method. It returns MaldivianString as a Latin letter string (you'll do need that conversion). Default Public Property assignValue(ByVal varName As Object) As String this is for assigning MaldivianString values. It's named assignValue but you'll never have to use that name because it's a default property. It also has ByVal varName As Object parameter because VB.NET requires default properties to have an argument.

I would have made this class as a static class, but then I would have used C# instead.

Here's an example of the usage

' Declare variables
Dim s As New MaldivianString("Test")
Dim s2 As MaldivianString

' Assign a MaldivianString to another MaldivianString
s2 = s

' Notice: You have to use ToString method because MessageBox.Show() takes a string, not MaldivianString
MessageBox.Show(s2.ToString())

' Set a new value (from Latin letters)
s2 = New MaldivianString("foobar")

' Show string
MessageBox.Show(s2.ToString())

This (usage I mean) is very close to string class. You have to remember that you're not dealing with Strings which are first class citizens in VB.NET, but MaldivianString (class instances) instead. That's why you always need a conversion back and forth with "normal" strings. Like New MaldivianString("Test") or MessageBox.Show(s2.ToString()) Finally here's a pointer to String Class in MSDN. It shows constructors, properties and methods you need to implement. I suggest doing a minimal implementation first and testing that. After all, you may not need the full String class.

HTH

yes unicode keyboard is used to type them (U+0780–U+07BF) they can be represented in latin but how to make the class?

Thanq teme64 thats a head start ive got ill post as I progress and again thanq very much for the help u guys are the best :)

This article has been dead for over six months. Start a new discussion instead.