TechnoTone Posted May 13, 2004 Posted May 13, 2004 I want to create my own object type called SWIFTBIC. This object will essentially be a String but it will have built in validation to ensure that the assigned value can only be a valid SWIFTBIC. Valid SWIFTBIC's are 6 characters followed by either 2 or 5 alphanumerics (Regular Expression "[A-Z]{6}([A-Z0-9]{5}|[A-Z0-9]{2})"). This in itself is fairly straight forward. However, my problem comes from the way I want to be able to use my SWIFTBIC. I want it to behave in the same way a String variable does, so that reading and writing to the objects value can be done directly as follows: Dim mySB as SWIFTBIC mySB = "ABCDEFGH" myTextBox.Text = mySB I realise that the direct assignment may not be possible without using a constructor: mySB = new SWIFTBIC("ABCDEFGH")That I can live with, but how can I get access to the objects value directly without having to specify a property? I've tried using default properties but that won't work without a parameter. I've also tried inheriting a String but that isn't allowed either. Is there any way to do what I want (using VB.NET preferably)? Quote TT (*_*) There are 10 types of people in this world; those that understand binary and those that don't.
Arch4ngel Posted May 13, 2004 Posted May 13, 2004 You'll have to overload every operator that you'll need. You might use the "+" and "=". http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconoperatoroverloadingusageguidelines.asp This is the link of Microsoft help on that. It'll give all syntax in VB.NET to do a correct operator overloading. The overload will obviously be made in your class. Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
Administrators PlausiblyDamp Posted May 13, 2004 Administrators Posted May 13, 2004 VB.Net cannot do operator overloading. The link suggests names to use that could be called from other languages if you are using operator overloading in C#. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Arch4ngel Posted May 13, 2004 Posted May 13, 2004 damn... I thought it could be possible... Forget it... My error... VB really sux.... not even a small operator overloading... Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
TechnoTone Posted May 13, 2004 Author Posted May 13, 2004 Cheers people. Looks like it's finally time I got stuck into C#. Quote TT (*_*) There are 10 types of people in this world; those that understand binary and those that don't.
Arch4ngel Posted May 13, 2004 Posted May 13, 2004 C# not a bad choice ;) but C++ is good also (I don't have time learn it... but if I get a week or two... I'll get my head on that) Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
JABE Posted May 13, 2004 Posted May 13, 2004 Dim mySB as SWIFTBIC mySB = "ABCDEFGH" myTextBox.Text = mySB A good (and standard) alternative is fleshing out your custom ToString() method so that you can do: myTextBox.Text = mySB.ToString() Not exactly what you're looking for but makes your class consistent w/ the .NET stock classes. Quote
Joe Mamma Posted May 13, 2004 Posted May 13, 2004 damn... I thought it could be possible... Forget it... My error... VB really sux.... not even a small operator overloading... I really feel your pain. . . Been spending the last six weeks on a VB contract working with a legacy app. The client wants it in VB because he can read VB. VB is the worst thing that ever happened to software development!!!! Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
*Experts* Nerseus Posted May 13, 2004 *Experts* Posted May 13, 2004 You don't necessarily have to do C# for everything. You could put the new type in a separate C# project and just reference it from your VB.NET projects. Of course for what you want (a new type with a lot of operator overloading), you'll need to know a bit of semi-advanced C#. If you're just starting out at programming or willing to make the move, maybe a move to C# is warranted. But, if you have a lot of time spent learning the ins and outs of VB.NET, you might try doing the minimum to get your class implemented in C#. For what you want (a direct assignment as if your type were a string) you'll need the implicit operator overloads (when you look up operator overloading look for the topics on "implicit"). -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
TechnoTone Posted May 14, 2004 Author Posted May 14, 2004 Thanks. I am an experienced VB.NET developer (been using VB since version 4) but have been considering learning C# too. It seems now is as good a time as any to get started. By the way - is it possible to mix C# and VB within a single project/assembly? Quote TT (*_*) There are 10 types of people in this world; those that understand binary and those that don't.
JABE Posted May 14, 2004 Posted May 14, 2004 By the way - is it possible to mix C# and VB within a single project/assembly? You can't mix raw .cs and .vb files in a single project/assembly but you can add references to other .NET assemblies regardless of w/c language they were written (as long as CLS rules have been dutifully conformed with). So you can instantiate (and even subclass) the public types of a compiled C# assembly in a VB.NET project. Quote
TechnoTone Posted May 14, 2004 Author Posted May 14, 2004 I realise that, but what I'm worried about is having to re-work the rest of my project into C# just because I need C# for one particular object. I was hoping I could incorporate a C# class into the existing project without having to re-do all of it. To clarify, the project is a common library that we use within our projects which encapsulates commonly used objects and functions. We are reluctant to split it into more than one library so it looks like we will have to convert all of it to C#. Quote TT (*_*) There are 10 types of people in this world; those that understand binary and those that don't.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.