Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

 

I would like to have a snippet of code from another file in my project, like for example a .vb file, to be included or inherited into my code for certain forms.

 

in visual c++ for example, you can include a header file like this:

 

#include "myFile.cpp"

 

My question is, how do i do this for vb.net?

 

what kind of file do i create to include it (ie should i right click and select "add new item" and choose what?), and what is syntax to inherit or include it into my code so it is as though it is written into the page at runtime. thanks.

 

-JBD

Posted
Imports TheClassName

 

Hi, unfortunately this only works at the very top, and I guess i need to insert the code down inside of the class for the current page.

 

also for example, if I create a class file that says this:

 

Public Class Class1

Dim strjt As String = "this is jts string"

End Class

 

or this:

 

Public Class Class1

Public strjt As String = "this is jts string"

End Class

 

and then on winform1, i put this in at top:

 

imports myProj.Class1

 

and down in my class I have this:

 

Label4.Text = myProj.Class1.strjt

 

it doesnt work...neither to variations of that.

 

What i would like to do, is not even necessarily import a class, but to just insert my code from a codefile or whatever into my form wherever i want, is this possible?

 

I suppose i could do it by importing the class, and making a function in the class that gives me the code i want and just return it...would that be best way?

 

I did experiment with that a bit as well, but I cant get the function to work...i tried this:

 

Public Class Class1

Dim strjt As String = "this is jts string"

Function getString()

getString = strjt

End Function

End Class

 

in its own class file, then on my win form:

 

imports myProj.class1

 

Label4.Text = Class1.getString()

 

 

this and its variations don't work. let me know if you can, thanks :)

 

-JBD

Posted

if in the same project no need to Imports

Instead, do as below:

 

Public Class Class1
  Dim strjt As String = "this is jts string"

  Function getString()
     getString = strjt
  End Function
End Class

Public Class Form1
  Private Sub Form_Load(...) Form.Load
     Dim cls as Class1
     Label4.Text  = cls.getString()
  End Sub
End Class

George C.K. Low

Posted

hmm, i guess i spoke too soon...

 

Im getting this error with the above code:

 

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

 

Source Error:

 

 

Line 41:

Line 42: Dim cls As Class1

Line 43: Label4.Text = cls.getString()

Line 44:

 

I am trying this in a web project now, think that has somthing to do with it? If so how should i alter this so it work? thanks.

Posted
Dim cls as new Class

 

will solve it.

 

woot you are fast thanks :) that fixed it.

 

I am looking for a way as well to put variables on my web forms at the form level...so i want to declare them right above the page_load sub. Is there a way I can call a class there to declare variables for that page?

Posted

Hi, that is sorta what im doing. I have a long list of sessionvariables, and on each page I want to set the sessionvariables to form level variables, like this:

 

MyVar1 = Session("MyVar1")

MyVar2 = Session("MyVar2")

MyVar3 = Session("MyVar3")

MyVar4 = Session("MyVar4")

etc.

 

there is alot of them.

 

What I was hoping, was instead of going through every single page of my web project, is that I could just do an "include" and the code would appear there on everypage where i need it. That way, if i ever add another one, I just change one file, and it will be included on all my pages. Is there a way to do this that you know of? thanks.

Posted

Good idea..

 

Create a class named VariableClass

 

Public Class VariableClass
  'declare all the variable here
  Dim MyVar1 as String
  Dim MyVar2 as String
  Dim MyVar3 as String

  Public Sub SetMyVar1(ByVal str As String)
     MyVar1 = str
  End Sub

  Public Function GetMyVar1() As String
     GetMyVar1 = MyVar1
  End Function
End Class

Public Class Form1
  Dim AllVariable as New VariableClass

  Private Sub Form_Load(...) MyBase.Load
     If AllVariable.GetMyVar1 <> "" Then
        'Display it out or anything
     Else
        AllVariable.SetMyVar1("Hello World")
        'The display it
     End If

  End Sub
End Class

George C.K. Low

Posted

Thanks...

 

I just noticed that if I declare on the form level like this:

 

Dim AllVariable as New VariableClass

 

and I have all my variables declared, I can access them if they are Public, but not if I just declare them like "dim MyVar1 as string = "some string"

 

So, if I declare them as Public....will that change the variables for the application or just for the session they are in?

 

The code that only calls them from functions and keeps them private is great, but it puts them in the form_load only instead of making them form level variables so I can use them in other subs besides the page load....does this make sense?

 

I either have to have the variables private and declared using a class like we have been doing, or i need to find a way to write the code, or include the code somehow above the page_load sub.

 

Thanks again, you are teaching me alot, I think Im close :)

Posted

when u want to read the variable,

 

you should not read as text1.text = AllVariable.MyVar1

Instead, you got to read as

 

text1.text = AllVariable.GetMyVar1()

in this way, you are asking from the function, not direct from the variable.

 

Got my idea??

George C.K. Low

Posted

yes I get that.

 

So you are saying I should change all my code to call the function instead of just the variable. I was just hoping there was a way to call a function above the page_load so i could declare variables on the form level from another file.

 

I wish there was a was to insert a snippet of code whereever i want on the page.

  • *Experts*
Posted
when u want to read the variable,

 

you should not read as text1.text = AllVariable.MyVar1

Instead, you got to read as

 

text1.text = AllVariable.GetMyVar1()

in this way, you are asking from the function, not direct from the variable.

 

Got my idea??

This method is not incorrect, but in .NET languages it is generally more acceptable to

use a property to get and set variable values. If the value is going to be modified

before it is set or retrieved, then it might be more proper to use a method, but if you're

strictly getting and setting the value, stick to a public property.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted

Hi Bob,

yeah, my problem is that if i use a public variable then it sets it application wide and I want it for only each user session of my web project. what im trying to do is write all the session variables to local form level variables above the page_load sub of each page, and I want to just like put the declarations there based off the contents of one file so i dont have to change every single page whenever i add or remove a new variable. I guess I just have to keep them in session state cause i guess there is no way to do this.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...