CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2013

    I hate using literal strings. Options?

    I often need to use literal strings throughout my code. But there are some places where I feel like I shouldn't need to. Problems with consistence, readability, and spelling errors.

    Case in point, data binding.

    Today I type
    Grid.ValueField = "id"

    But here's the thing, I have classes (some I have manually created) and the EntityFramwork (with models and classes auto-generated). So I have already typed into Visual Studio the name for the field I want. Then, when referencing that field, I have to type the literal again. And again. and again.

    Tomorrow I would like to type
    Or perhaps
    Grid.TextField= GetNameFunction(object.property)
    Or even better

    Today, I use classes...
    Public class fieldstrings
    Public class SomeTable
    Public const FooFieldName as string = “foo”
    End class
    End class
    Grid.TextValue = fieldstrings.SomeTable.fooFieldName

    I prefer classes over enums becuase I can nest classes, making navigation easier.
    I am not thrilled about this approach, because it separates the relevant information from the original class.

    I tried to do this
    Public Class someobject
    Public Property foo
    public function Name as string
    return "Foo"
    end function
    end property
    end class

    I thought about reflection, but I can only use GetType on an object, not the property.

    So this time I am trying
    Public class someobject
    public property foo as string
    publish shared foo_name as string = "foo"
    end class

    Anyone with other suggestions?

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008

    Re: I hate using literal strings. Options?

    I am not quite sure what it is you are trying to do?
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jan 2006
    Fox Lake, IL

    Re: I hate using literal strings. Options?

    Look up RESOURCE FILES. That's where you put strings for different languages, and select which one you use when you install the app.

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Jul 2000
    Milano, Italy

    Re: I hate using literal strings. Options?

    there sre some solutions.
    you could have
    Constants (as typed in a public Module or in a resource file or even in a table of DB), objects (even in conjunction with Reflection, to get the name of a property as a string value, for example).
    Each solution has pro and cont. You will have to see what you feel more easy to use and to change when needed. You might want to take a look slso at Enum and the way to get not only the value, but also the name of enumeration as a string.
    Once I tried the reflection trick. That might be funny but slower than the old plain module of consts. However, once your software is sealed, the module cannot be easily changed. That is why once i made a mix of the two: a class to load a module dinamically...but nowaday, you have configuration files where you can store sections and key pair values.
    The are the evolution of INI files. Take a look
    ...at present time, using mainly Net 4.0, Vs 2010

    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Oct 2013

    Re: I hate using literal strings. Options?

    Wow, has it been this long since I looked at this?

    I ended up doing a combination of 2 things:
    1) a class, with subclasses, containing string constants. So the main class is the generic 'MyStrings' and then broken down under there.
    2) In one project, I am doing a model-first Entity Framework model. In there, I modified the TT file such that each class spits out two constants for each property: The property name, and the property name surrounded by []. Here's an example output...

    Class Foo
    Public Property Id as Int64
    Public Const Id_Fieldname as string = "Id"
    Public Const Id_SqlFieldname as string = "[Id]"
    End Class

    So all my generated objects get these created.

    3) I still use Enum occassionally. But these are for storing INT values....

    FYI: I decided not to go further with reflection.
    FYI: I decided not to use the Resource File. Simply put, any change to the model would require me to manually update the Resource file. Using the TT generator, the constants are immediately updated and so all old 'where I used the constant' comes up as an error, so I find the code to fix easily!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.